Beispiel #1
0
        private void DrawConvexHull(PointD[] points)
        {
            const double radius = 4.0;

            // generate new random point set if desired
            if (points == null)
            {
                Size  scale  = new Size(OutputBox.Width - 4 * radius, OutputBox.Height - 4 * radius);
                int   count  = MersenneTwister.Default.Next(4, 40);
                RectD bounds = new RectD(2 * radius, 2 * radius, scale.Width, scale.Height);
                points = GeoAlgorithms.RandomPoints(
                    count, bounds, new PointDComparerY(), 2 * radius);
            }

            _points = points;
            PointD[] polygon = GeoAlgorithms.ConvexHull(points);
            OutputBox.Children.Clear();

            // draw hull vertices filled, other points hollow
            for (int i = 0; i < points.Length; i++)
            {
                bool isVertex = polygon.Contains(points[i]);

                var circle = new Ellipse()
                {
                    Width  = 2 * radius, Height = 2 * radius,
                    Stroke = Brushes.Black,
                    Fill   = isVertex ? Brushes.Black : null
                };

                Canvas.SetLeft(circle, points[i].X - radius);
                Canvas.SetTop(circle, points[i].Y - radius);

                OutputBox.Children.Add(circle);
            }

            // draw edges of convex hull
            OutputBox.Children.Add(new Polygon()
            {
                Points = polygon.ToWpfPoints(),
                Stroke = Brushes.Red
            });
        }
        public void ConvexHull()
        {
            PointD p0 = new PointD(0, 0), p1 = new PointD(1, 1), p2 = new PointD(2, 0);

            CollectionAssert.AreEqual(new[] { p0 }, GeoAlgorithms.ConvexHull(p0));
            CollectionAssert.AreEqual(new[] { p0 }, GeoAlgorithms.ConvexHull(p0, p0));
            CollectionAssert.AreEqual(new[] { p0 }, GeoAlgorithms.ConvexHull(p0, p0, p0));

            CollectionAssert.AreEqual(new[] { p0, p1 }, GeoAlgorithms.ConvexHull(p0, p1));
            CollectionAssert.AreEqual(new[] { p1, p0 }, GeoAlgorithms.ConvexHull(p0, p1, p0));
            CollectionAssert.AreEqual(new[] { p1, p0 }, GeoAlgorithms.ConvexHull(p0, p0, p1));
            CollectionAssert.AreEqual(new[] { p1, p0 }, GeoAlgorithms.ConvexHull(p0, p1, p1));

            CollectionAssert.AreEqual(new[] { p1, p2, p0 }, GeoAlgorithms.ConvexHull(p0, p1, p2));
            CollectionAssert.AreEqual(new[] { p1, p2, p0 }, GeoAlgorithms.ConvexHull(p1, p0, p2));

            PointD p3 = new PointD(1, 0);

            CollectionAssert.AreEqual(new[] { p1, p2, p0 }, GeoAlgorithms.ConvexHull(p3, p1, p0, p2));
        }