Example #1
0
 public void WriteTo(List<CloudTag> tags, Color[] colors = null)
 {
     string outputSourceName = "out.png";
     float width;
     float height;
     tags.GetBounds(out width, out height);
     var random = new Random();
     using (var image = new Bitmap((int)width+1, (int)height+1))
     using (var g = Graphics.FromImage(image))
     {
         foreach (var tag in tags)
             g.DrawString(tag.Word, new Font("Times New Roman", tag.Frequency),
                 new SolidBrush(GetRandomColor(random, colors)), tag.X, tag.Y);
         image.Save(outputSourceName, ImageFormat.Png);
     }
 }
		private void RebuildUI()
		{
			if (DataSource == null)
			{
				//todo: clear
				return;
			}

			panel.Children.Clear();

			int width = DataSource.Width;
			int height = DataSource.Height;
			bounds = DataSource.Grid.GetGridBounds();
			fieldWrapper = new UniformFieldWrapper(DataSource.Data, width, height);

			List<Point[]> tracks = new List<Point[]>(pointsNum);
			for (int i = 0; i < pointsNum; i++)
			{
				var track = new List<Point>();

				var start = rnd.NextPoint(bounds.XMin, bounds.XMax, bounds.YMin, bounds.YMax);
				track.Add(start);

				int maxLength = Math.Max(width, height);
				var position = start;
				double length = 0;
				do
				{
					var K1 = fieldWrapper.GetVector(position);
					K1.Normalize();
					//var shift = K1;
					var K2 = fieldWrapper.GetVector(position + (K1 / 2).DecreaseLength(width, height));
					K2.Normalize();
					var K3 = fieldWrapper.GetVector(position + (K2 / 2).DecreaseLength(width, height));
					K3.Normalize();
					var K4 = fieldWrapper.GetVector(position + K3.DecreaseLength(width, height));
					K4.Normalize();

					var shift = ((K1 + 2 * K2 + 2 * K3 + K4) / 6);
					//shift.Normalize();
					if (shift.X.IsNaN() || shift.Y.IsNaN())
						break;

					var next = position + shift;
					track.Add(next);

					if (!OccupyCells(position, next, bounds))
						break;

					position = next;
					length += shift.Length;
				} while (length < maxLength);

				Polyline line = new Polyline
				{
					Stroke = Brushes.Orange,
					StrokeThickness = 1,
					Stretch = Stretch.Fill,
					Points = new PointCollection(track),
					Effect = new DropShadowEffect { Color = Colors.Black, Direction = 245, BlurRadius = 3 }
				};
				ViewportPanel.SetViewportBounds(line, track.GetBounds());
				panel.Children.Add(line);
			}
		}
		protected void DrawLine(Point point)
		{
			double maxLength = LineLengthFactor * Math.Max(DataSource.Width, DataSource.Height);
			const int maxIterations = 300;

			Action<double, List<Point>> pointTracking = (directionSign, track) =>
			{
				int i = 0;
				var position = point;
				double length = 0;
				bool finished = false;
				do
				{
					double x = position.X;
					double y = position.Y;
					if (x < 0 || x > 1 || y < 0 || y > 1 || x.IsNaN() || y.IsNaN())
						break;

					double xFactor = 1.0 / width;
					double yFactor = 1.0 / height;

					var K1 = fieldWrapper.GetVector(position).ChangeLength(xFactor, yFactor);
					var K2 = fieldWrapper.GetVector(position + K1 / 2).ChangeLength(xFactor, yFactor);
					var K3 = fieldWrapper.GetVector(position + K2 / 2).ChangeLength(xFactor, yFactor);
					var K4 = fieldWrapper.GetVector(position + K3).ChangeLength(xFactor, yFactor);

					var shift = (K1 + 2 * K2 + 2 * K3 + K4) / 6;
					if (shift.X.IsNaN() || shift.Y.IsNaN())
						break;

					//shift /= 10;
					Point next = position + directionSign * shift;
					Point viewportPoint = PointToViewport(next);
					track.Add(viewportPoint);

					position = next;
					length += shift.Length;
					i++;

					finished = !(length < maxLength && i < maxIterations);
				} while (!finished);
			};

			var forwardTrack = new List<Point>();
			forwardTrack.Add(PointToViewport(point));
			pointTracking(+1, forwardTrack);
			Polyline forwardLine = CreateLine(forwardTrack);
			ViewportPanel.SetViewportBounds(forwardLine, forwardTrack.GetBounds());
			if (forwardTrack.Count > 1)
				panel.Children.Add(forwardLine);

			var backwardTrack = new List<Point>();
			backwardTrack.Add(PointToViewport(point));
			pointTracking(-1, backwardTrack);
			var backwardLine = CreateLine(backwardTrack);
			ViewportPanel.SetViewportBounds(backwardLine, backwardTrack.GetBounds());
			if (backwardTrack.Count > 1)
				panel.Children.Add(backwardLine);
		}