Exemplo n.º 1
0
        public void IsConvexTest()
        {
            var region = PolygonMethods.Rectangle(new KtPoint2D(2, 2), 40, 30, true);

            Assert.AreEqual(region.IsConvex, false);
            KtRegion region2 = (CreateArbitraryRegion());

            Assert.AreEqual(region2.IsConvex, true);
            KtRegion reg = (new List <KtPoint2D>
            {
                new KtPoint2D(0, 0),
                new KtPoint2D(0, 10),
                new KtPoint2D(5, 10),
                new KtPoint2D(0, 20),
                new KtPoint2D(10, 20),
                new KtPoint2D(10, 0)
            });

            Assert.AreEqual(reg.IsConvex, true);
            KtRegion rec = (new List <KtPoint2D>
            {
                new KtPoint2D(0, 0),
                new KtPoint2D(20, 0),
                new KtPoint2D(20, 5),
                new KtPoint2D(0, 5)
            });

            Assert.AreEqual(rec.IsConvex, false);
        }
Exemplo n.º 2
0
        public void AreaTest()
        {
            var region = PolygonMethods.Rectangle(new KtPoint2D(2, 2), 40, 30, true);

            Assert.AreEqual(region.Area(), 40 * 30);
            KtRegion rec = (new List <KtPoint2D>
            {
                new KtPoint2D(0, 0),
                new KtPoint2D(20, 0),
                new KtPoint2D(20, 5),
                new KtPoint2D(0, 5)
            });

            Assert.AreEqual(rec.Area(), 20 * 5.0);
            KtRegion triangle = (new List <KtPoint2D>
            {
                new KtPoint2D(0, 0),
                new KtPoint2D(20, 5),
                new KtPoint2D(0, 10)
            });

            Assert.AreEqual(triangle.Area(), 20 * 10.0 / 2);
            KtRegion triangle2 = (new List <KtPoint2D>
            {
                new KtPoint2D(0, 0),
                new KtPoint2D(20, 0),
                new KtPoint2D(0, 10)
            });

            Assert.AreEqual(triangle2.Area(), 20 * 10.0 / 2);
        }
Exemplo n.º 3
0
        public static string AddToPolygonText(this KtRegion region, string polygonName)
        {
            var builder = new StringBuilder();

            builder.Append(polygonName).Append(".Add(");
            builder.Append(region.CreateARegionInstance());
            builder.Append("); \n");
            return(builder.ToString());
        }
Exemplo n.º 4
0
 public void Remove(KtRegion region)
 {
     var(intersected, notintersected) = _regions.Fork(reg => region.IsIntersecting(reg));
     if (!intersected.IsEmpty())
     {
         _regions = notintersected.ToList();
         foreach (var reg in intersected)
         {
             foreach (var cut in reg.Remove(region))
             {
                 _regions.Add(cut);
             }
         }
     }
 }
Exemplo n.º 5
0
        public void Add(KtRegion region)
        {
            switch (region)
            {
            case KtHollowRegion hollow:
                Add(hollow);
                break;

            case KtSolidRegion solid:
                Add(solid);
                break;

            default:
                throw new Exception("");
            }
        }
Exemplo n.º 6
0
        public static string CreateARegionInstance(this KtRegion region)
        {
            var builder = new StringBuilder();

            builder.Append(region is KtSolidRegion ? "new KtSolidRegion" : "new KtHollowRegion");
            if (region == null || region.Count == 0)
            {
                builder.Append("()");
            }
            else
            {
                builder.Append("( new KtPoint2D[]{")
                .Append('(').Append(region.First().X).Append(',').Append(region.First().Y).Append(')');
                foreach (var corner in region.Skip(1))
                {
                    builder.Append(",(").Append(corner.X).Append(',').Append(corner.Y).Append(')');
                }
                builder.Append("})");
            }
            return(builder.ToString());
        }
Exemplo n.º 7
0
        public static void DrawRegion(this Graphics gr, KtRegion reg, Color color)
        {
            if (reg.Count < 3)
            {
                return;
            }
            var regconvert = reg.Corners.Select(corner => new Point((int)corner.X, (int)corner.Y)).ToArray();

            using (var pen = new Pen(!reg.IsClockwise ? color : Color.FromArgb(color.ToArgb() ^ 0xffffff)))
            {
                gr.FillPolygon(pen.Brush, regconvert);
                gr.DrawPolygon(Pens.WhiteSmoke, regconvert);
            }
            if (reg is KtSolidRegion solid)
            {
                foreach (var hole in solid.Holes)
                {
                    gr.DrawRegion(hole, color);
                }
            }
        }
Exemplo n.º 8
0
        public void CentroidTest()
        {
            var region = PolygonMethods.Rectangle(new KtPoint2D(2, 2), 40, 30, true);

            Assert.AreEqual(region.Centroid(), new KtPoint2D(2, 2));
            KtRegion rec = (new List <KtPoint2D>
            {
                new KtPoint2D(0, 0),
                new KtPoint2D(20, 0),
                new KtPoint2D(20, 5),
                new KtPoint2D(0, 5)
            });

            Assert.AreEqual(rec.Centroid(), new KtPoint2D(10, 2.5));
            KtRegion triangle = (new List <KtPoint2D>
            {
                new KtPoint2D(0, 0),
                new KtPoint2D(20, 0),
                new KtPoint2D(0, 10)
            });

            Assert.AreEqual(triangle.Centroid(), new KtPoint2D(20.0 / 3, 10.0 / 3));
        }
Exemplo n.º 9
0
 public static Point[] ToArrayOfPoints(this KtRegion region)
 {
     return(region.Corners.Select(corner => new Point((int)corner.X, (int)corner.Y)).ToArray());
 }
Exemplo n.º 10
0
 internal static (EnumerableSolids solid, EnumerableHollows hollow) Clip(this EnumerableRegions subjects, KtRegion clip, ClipType clipType, bool reversed = false) => Clip(subjects, new[] { clip }, clipType, reversed);