Example #1
0
        public void TestMethod1()
        {
            int nBuildings = 3;

            Point[] buildings = new Point[nBuildings];

            //buildings[0] = new Point(-5,-3);
            //buildings[1] = new Point(-9, 2);
            //buildings[2] = new Point(3, -4);

            buildings[0] = new Point(1, 2);
            buildings[1] = new Point(0, 0);
            buildings[2] = new Point(2, 2);

            int widthDistance = buildings.Max(_ => _.X) - buildings.Min(_ => _.X);

            var average = buildings.Average(_ => _.Y);

            var yPos = (long) buildings.Select(p => new Tuple<int, double>(p.Y, Math.Abs(p.Y - average))).OrderBy(_ => _.Item2).First().Item1;
            var dist = buildings.Select(p => Math.Abs((p.Y - yPos))).Sum();

             var totalDistance = (long)widthDistance + dist;

            Assert.AreEqual(4,totalDistance);
        }
        private static long calculateMainCableLength(Point[] locations)
        {
            long largestXCoordinate = locations.Max(location => location.X);
            long smallestXCoordinate = locations.Min(location => location.X);

            return largestXCoordinate - smallestXCoordinate;
        }
Example #3
0
 /// <summary>
 /// Creates a polygon shape from the specified points.
 /// </summary>
 /// <param name="points">The points.</param>
 public Polygon(Point[] points)
 {
     // Set the location.
     this.location = points.Min();
     // Set the size.
     this.size = new Size(points.Max().Subtract(this.location));
     // Set the points.
     this.points = new Point[points.Length];
     for (int index = 0; index < points.Length; index++)
     {
         this.points[index] = points[index].Subtract(this.location);
     }
 }
        public static Bitmap ToBitmap(Point[] sequence)
        {
            if (sequence.Length == 0)
                return null;

            int xmax = (int)sequence.Max(x => x.X);
            int xmin = (int)sequence.Min(x => x.X);

            int ymax = (int)sequence.Max(x => x.Y);
            int ymin = (int)sequence.Min(x => x.Y);

            int width = xmax - xmin;
            int height = ymax - ymin;

            Bitmap bmp = new Bitmap(width + 16, height + 16);

            Graphics g = Graphics.FromImage(bmp);

            for (int i = 1; i < sequence.Length; i++)
            {
                int x = (int)sequence[i].X - xmin;
                int y = (int)sequence[i].Y - ymin;
                int p = (int)Accord.Math.Tools.Scale(0, sequence.Length, 0, 255, i);

                int prevX = (int)sequence[i - 1].X - xmin;
                int prevY = (int)sequence[i - 1].Y - ymin;

                using (Brush brush = new SolidBrush(Color.FromArgb(255 - p, 0, p)))
                using (Pen pen = new Pen(brush, 16))
                {
                    pen.StartCap = LineCap.Round;
                    pen.EndCap = LineCap.Round;
                    g.DrawLine(pen, prevX, prevY, x, y);
                }
            }

            return bmp;
        }
Example #5
0
		private IEnumerable<Shape> _addSerieAsBezier(Point[] points, bool animate = true)
		{
            if (points.Length < 2) return Enumerable.Empty<Shape>();
			var addedFigures = new List<Shape>();

			Point[] cp1, cp2;
			BezierSpline.GetCurveControlPoints(points, out cp1, out cp2);

			var lines = new PathSegmentCollection();
			var areaLines = new PathSegmentCollection {new LineSegment(points[0], true)};
			var l = 0d;
			for (var i = 0; i < cp1.Length; ++i)
			{
				lines.Add(new BezierSegment(cp1[i], cp2[i], points[i + 1], true));
				areaLines.Add(new BezierSegment(cp1[i], cp2[i], points[i + 1], true));
				//it would be awesome to use a better formula to calculate bezier lenght
				l += Math.Sqrt(
				               Math.Pow(Math.Abs(cp1[i].X - cp2[i].X), 2) +
				               Math.Pow(Math.Abs(cp1[i].Y - cp2[i].Y), 2));
				l += Math.Sqrt(
				               Math.Pow(Math.Abs(cp2[i].X - points[i + 1].X), 2) +
				               Math.Pow(Math.Abs(cp2[i].Y - points[i + 1].Y), 2));
			}
			//aprox factor, it was calculated by aproximation.
			//the more line is curved, the more it fails.
			l = l * .65;
			areaLines.Add(new LineSegment(new Point(points.Max(x => x.X), ToPlotArea(Chart.Min.Y, AxisTags.Y)), true));
			var f = new PathFigure(points[0], lines, false);
			var fa = new PathFigure(new Point(points.Min(x => x.X), ToPlotArea(Chart.Min.Y, AxisTags.Y)), areaLines, false);
			var g = new PathGeometry(new[] {f});
			var ga = new PathGeometry(new[] {fa});

		    var path = new Path
		    {
		        Stroke = Stroke,
		        StrokeThickness = StrokeThickness,
		        Data = g,
		        StrokeEndLineCap = PenLineCap.Round,
		        StrokeStartLineCap = PenLineCap.Round,
		        StrokeDashOffset = l,
		        StrokeDashArray = new DoubleCollection {l, l},
		        ClipToBounds = true
		    };
		    var patha = new Path
		    {
		        StrokeThickness = 0,
		        Data = ga,
		        Fill = Fill,
		        ClipToBounds = true
		    };

			Chart.Canvas.Children.Add(path);
			addedFigures.Add(path);
            
		    Chart.Canvas.Children.Add(patha);
		    addedFigures.Add(patha);

		    var draw = new DoubleAnimationUsingKeyFrames
		    {
		        BeginTime = TimeSpan.FromSeconds(0),
		        KeyFrames = new DoubleKeyFrameCollection
		        {
		            new SplineDoubleKeyFrame
		            {
		                KeyTime = TimeSpan.FromMilliseconds(1),
		                Value = l
		            },
		            new SplineDoubleKeyFrame
		            {
		                KeyTime = TimeSpan.FromMilliseconds(750),
		                Value = 0
		            }
		        }
		    };

			Storyboard.SetTarget(draw, path);
			Storyboard.SetTargetProperty(draw, new PropertyPath(Shape.StrokeDashOffsetProperty));
			var sbDraw = new Storyboard();
			sbDraw.Children.Add(draw);
			var animated = false;
			if (!Chart.DisableAnimation)
			{
				if (animate)
				{
					sbDraw.Begin();
					animated = true;
				}
			}
			if (!animated) path.StrokeDashOffset = 0;
			return addedFigures;
		}
Example #6
0
        private IEnumerable<Shape> _addSerieAsBezier(Point[] points, bool animate = true)
        {
            if (points.Length < 2) return Enumerable.Empty<Shape>();
            var addedFigures = new List<Shape>();

            Point[] cp1, cp2;
            BezierSpline.GetCurveControlPoints(points, out cp1, out cp2);

            var lines = new PathSegmentCollection();
            var areaLines = new PathSegmentCollection { new LineSegment(points[0], true) };
            var l = 0d;
            for (var i = 0; i < cp1.Length; ++i)
            {
                lines.Add(new BezierSegment(cp1[i], cp2[i], points[i + 1], true));
                areaLines.Add(new BezierSegment(cp1[i], cp2[i], points[i + 1], true));
                l += GetBezierLength(new [] { points[i], cp1[i], cp2[i], points[i + 1] });
            }
            l *= 1.05;
            l /= StrokeThickness;
            var lastP = Chart.Invert
                ? new Point(ToDrawMargin(Chart.Min.X, AxisTags.X), points.Min(x => x.Y))
                : new Point(points.Max(x => x.X), ToDrawMargin(Chart.Min.Y, AxisTags.Y));
            areaLines.Add(new LineSegment(lastP, true));
            var f = new PathFigure(points[0], lines, false);
            var aOrigin = Chart.Invert
                ? new Point(ToDrawMargin(Chart.Min.X, AxisTags.X), points.Max(x => x.Y))
                : new Point(points.Min(x => x.X), ToDrawMargin(Chart.Min.Y, AxisTags.Y));
            var fa = new PathFigure(aOrigin, areaLines, false);
            var g = new PathGeometry(new[] { f });
            var ga = new PathGeometry(new[] { fa });

            var path = new Path
            {
                Stroke = Stroke,
                StrokeThickness = StrokeThickness,
                Data = g,
                StrokeEndLineCap = PenLineCap.Round,
                StrokeStartLineCap = PenLineCap.Round,
                StrokeDashOffset = l,
                StrokeDashArray = new DoubleCollection { l, l },
                ClipToBounds = true
            };
            var patha = new Path
            {
                StrokeThickness = 0,
                Data = ga,
                Fill = Fill,
                ClipToBounds = true
            };

            Chart.DrawMargin.Children.Add(path);
            addedFigures.Add(path);

            Chart.DrawMargin.Children.Add(patha);
            addedFigures.Add(patha);

            var draw = new DoubleAnimationUsingKeyFrames
            {
                BeginTime = TimeSpan.FromSeconds(0),
                KeyFrames = new DoubleKeyFrameCollection
                {
                    new SplineDoubleKeyFrame
                    {
                        KeyTime = TimeSpan.FromMilliseconds(1),
                        Value = l
                    },
                    new SplineDoubleKeyFrame
                    {
                        KeyTime = TimeSpan.FromMilliseconds(750),
                        Value = 0
                    }
                }
            };

            Storyboard.SetTarget(draw, path);
            Storyboard.SetTargetProperty(draw, new PropertyPath(Shape.StrokeDashOffsetProperty));
            var sbDraw = new Storyboard();
            sbDraw.Children.Add(draw);
            var animated = false;
            if (!Chart.DisableAnimation)
            {
                if (animate)
                {
                    sbDraw.Begin();
                    animated = true;
                }
            }
            if (!animated) path.StrokeDashOffset = 0;
            return addedFigures;
        }
Example #7
0
        public static Point[] ConvexHull(Point[] points)
        {
            if (points.Length < 3)
            {
                throw new ArgumentException("At least 3 points reqired", "points");
            }

            List<Point> hull = new List<Point>();

            // get leftmost point
            Point vPointOnHull = points.Where(p => p.X == points.Min(min => min.X)).First();

            Point vEndpoint;
            do
            {
                hull.Add(vPointOnHull);
                vEndpoint = points[0];

                for (int i = 1; i < points.Length; i++)
                {
                    if ((vPointOnHull == vEndpoint)
                        || (Orientation(vPointOnHull, vEndpoint, points[i]) == -1))
                    {
                        vEndpoint = points[i];
                    }
                }

                vPointOnHull = vEndpoint;
            }
            while (vEndpoint != hull[0]);

            return hull.ToArray();
        }