예제 #1
0
        public void triangle_inside_square()
        {
            List <xy> p1 = ut.MakePoly(new xy(0, 0), new xy(4, 0), new xy(4, 4), new xy(0, 4));
            List <xy> p2 = ut.MakePoly(new xy(1, 1), new xy(3, 1), new xy(2, 3));

            ppi2d pi = new ppi2d(p1, p2);

            Assert.AreEqual(ut.PolygonArea2d(p2), ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetIntersection())));
            Assert.AreEqual(4 * 4, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetUnion())));
            Assert.AreEqual(4 * 4 - ut.PolygonArea2d(p2), ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference1())));
            Assert.AreEqual(0, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference2())));
        }
예제 #2
0
파일: ppi2d.cs 프로젝트: thild/sawdust
        /*
         * This method is only used by the unit tests.  It exists to provide
         * a wrapper for the new ppi2d code to present the API from the old
         * ppi2d code.  Without this wrapper, I would need to rewrite all
         * the ppi2d unit tests.
         * */
        public static List <List <xy> > Polygon2d_Intersection(List <xy> poly1, List <xy> poly2, PolyPolyOp op)
        {
            List <List <xy> > result = new List <List <xy> >();

            Debug.Assert(
                (op == PolyPolyOp.Intersection) ||
                (op == PolyPolyOp.Union) ||
                (op == PolyPolyOp.Difference)
                );
            ppi2d pi = new ppi2d(poly1, poly2);

            switch (op)
            {
            case PolyPolyOp.Intersection:
            {
                ppi2d.FindTheLoops(result, pi.GetIntersection());
                break;
            }

            case PolyPolyOp.Union:
            {
                ppi2d.FindTheLoops(result, pi.GetUnion());
                break;
            }

            case PolyPolyOp.Difference:
            {
                ppi2d.FindTheLoops(result, pi.GetDifference1());
                break;
            }
            }
            if (
                (result == null) ||
                (result.Count == 0)
                )
            {
                return(null);
            }
            else
            {
                return(result);
            }
        }
예제 #3
0
        public void nasty_case()
        {
            List <xy> p1 = ut.MakePoly(new xy(0, 0), new xy(10, 0), new xy(10, 1), new xy(0, 1), new xy(0, 2), new xy(5, 2), new xy(5, 3), new xy(0, 3), new xy(0, 4), new xy(10, 4), new xy(10, 5), new xy(0, 5), new xy(0, 6), new xy(5, 6), new xy(5, 7), new xy(0, 7), new xy(0, 8), new xy(10, 8), new xy(10, 9), new xy(0, 9), new xy(0, 10), new xy(10, 10), new xy(10, 11), new xy(-1, 11), new xy(-1, 0));
            List <xy> p2 = ut.MakePoly(new xy(5, 0), new xy(6, 0), new xy(6, 11), new xy(5, 11));

            ppi2d pi = new ppi2d(p1, p2);

            Assert.AreEqual(11, ut.PolygonArea2d(p2));
            Assert.AreEqual(11 + 1 + 6 + 1 + 11 + 1 + 6 + 1 + 11 + 1 + 11, ut.PolygonArea2d(p1));

            Assert.AreEqual(4, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetIntersection())));
            Assert.AreEqual(ut.PolygonArea2d(p1) + 3 + 3 + 1, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetUnion())));
            Assert.AreEqual(ut.PolygonArea2d(p1) - 4, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference1())));
            Assert.AreEqual(7, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference2())));
        }
예제 #4
0
        public void triangle_touching_square_at_vertex()
        {
            List <xy> p1 = ut.MakePoly(new xy(0, 0), new xy(4, 0), new xy(4, 4), new xy(0, 4));
            List <xy> p2 = ut.MakePoly(new xy(3, 3), new xy(5, 5), new xy(3, 7));

            ppi2d pi = new ppi2d(p1, p2);

            Assert.AreEqual(0.5, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetIntersection())));
            Assert.AreEqual(4 * 4 + 2 * 4 / 2 - 0.5, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetUnion())));
            Assert.AreEqual(4 * 4 - 0.5, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference1())));
            Assert.AreEqual(2 * 4 / 2 - 0.5, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference2())));
        }
예제 #5
0
        public void simple()
        {
            // two 4x4 squares, overlap area is 2x2
            List <xy> p1 = ut.MakePoly(new xy(0, 0), new xy(4, 0), new xy(4, 4), new xy(0, 4));
            List <xy> p2 = ut.MakePoly(new xy(2, 2), new xy(6, 2), new xy(6, 6), new xy(2, 6));

            ppi2d pi = new ppi2d(p1, p2);

            Assert.AreEqual(2 * 2, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetIntersection())));
            Assert.AreEqual(4 * 4 + 4 * 4 - 2 * 2, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetUnion())));
            Assert.AreEqual(4 * 4 - 2 * 2, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference1())));
            Assert.AreEqual(4 * 4 - 2 * 2, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference2())));
        }