Example #1
0
 public LineSeg[] GetLineSegments()
 {
     LineSeg[] segs = new LineSeg[point.Length];
     for (int i = 0; i < point.Length; i++)
     {
         segs[i] = new LineSeg(point[i], point[(i + 1) % point.Length]);
     }
     return(segs);
 }
Example #2
0
        public void TestLineSegClass()
        {
            LineSeg l1 = new LineSeg(2, 2, 11, 5);
            LineSeg l2 = new LineSeg(3, 7, 5, 3);
            LineSeg l3 = new LineSeg(6, 0, 9, 6);
            LineSeg l4 = new LineSeg(10, 2, 10, 8);
            LineSeg l5 = new LineSeg(12, 1, 13, 3);
            LineSeg l6 = new LineSeg(0, 7, 12, 7);
            Point   p;

            Assert.IsTrue(Line.IsParallel(l3, l5));
            Assert.IsFalse(Line.IsParallel(l2, l4));
            Assert.IsTrue(Line.Intersect(l2, l6, out p));
            Assert.IsTrue(p.Equals(new Point(3, 7)));
            Assert.IsTrue(Line.Intersect(l3, l1, out p));
            Assert.IsTrue(p.Equals(new Point(8, 4)));
            Assert.IsTrue(Line.Intersect(l1, l4, out p));
            Assert.IsNull(p);
            Assert.IsTrue(Line.Intersect(l4, l6, out p));
            Assert.IsTrue(p.Equals(new Point(10, 7)));
            Assert.IsFalse(Line.Intersect(l5, l1));
            Assert.IsFalse(Line.Intersect(l3, l5));
            Assert.IsFalse(Line.Intersect(l3, l6));
            Assert.IsFalse(Line.Intersect(l6, l3));

            Assert.IsTrue(l2.LiesOn(new Point(4, 5)));
            Assert.IsTrue(l5.LiesOn(new Point(12, 1)));
            Assert.IsFalse(l1.LiesOn(new Point(-1, 1)));
            Assert.IsFalse(l1.LiesOn(new Point(14, 6)));
            Assert.IsTrue(l1.LiesOn(new Point(5, 3)));
            Assert.IsFalse(l1.LiesOn(new Point(5, 2)));
            double d1 = l1.Distance(new Point(4, 2));

            Assert.IsTrue(Math.Abs(d1 - 0.63245553203367588) < 1e-10);
            double d2 = l3.Distance(new Point(-23, -7));

            Assert.IsTrue(Math.Abs(d2 - 29.832867780352597) < 1e-10);
            double d3 = l6.Distance(new Point(14832, 7));

            Assert.IsTrue(Math.Abs(d3 - 14820) < 1e-10);

            Assert.IsTrue(Math.Abs(l5.Length - Math.Sqrt(5)) < 1e-10);

            Assert.IsNull(LineSeg.Merge(l3, l5));
            LineSeg l7 = new LineSeg(8, 4, 7, 2);

            Assert.IsTrue(LineSeg.Merge(l3, l7).Equals(l3));
            LineSeg l8 = new LineSeg(4, -4, 7, 2);

            Assert.IsTrue(LineSeg.Merge(l8, l3).Equals(new LineSeg(9, 6, 4, -4)));
            Assert.IsTrue(LineSeg.Merge(new LineSeg(9, 6, 10, 8), l3).Equals(new LineSeg(10, 8, 6, 0)));
        }
Example #3
0
 // If a and b are parallel lines that share at least one point, return the merge of these lines.
 // Otherwise return null. If a LineSeg is returned, it will be a new instance.
 public static LineSeg Merge(LineSeg a, LineSeg b)
 {
     if (!IsParallel(a, b))
     {
         return(null);
     }
     if (!a.LiesOn(b.a) && !a.LiesOn(b.b) && !b.LiesOn(a.a) && !b.LiesOn(a.b))
     {
         return(null);
     }
     Point[] p = new Point[] { a.a, a.b, b.a, b.b };
     Array.Sort(p);
     return(new LineSeg(p[0], p[3]));
 }
Example #4
0
        public void TestLineLineSegMix()
        {
            Line    l1  = new Line(2, 3, 8, 9);
            Line    l2  = new Line(8, 9, 2, 3);
            LineSeg ls1 = new LineSeg(2, 3, 8, 9);
            LineSeg ls2 = new LineSeg(8, 9, 2, 3);

            Assert.IsTrue(l1.Equals(l2));
            Assert.IsTrue(l2.Equals(l1));
            Assert.IsTrue(ls1.Equals(ls2));
            Assert.IsTrue(ls2.Equals(ls1));
            Assert.IsFalse(l1.Equals(ls2));
            Assert.IsFalse(l2.Equals(ls1));
            Assert.IsFalse(ls1.Equals(l2));
            Assert.IsFalse(ls2.Equals(l1));

            // TODO: Test Intersect
        }