示例#1
0
        public void Contains_Point()
        {
            var sut = new LineSegment(new Point(0, 0), new Point(2, 2));

            Assert.IsTrue(sut.Contains(new Point(0, 0)));
            Assert.IsTrue(sut.Contains(new Point(1, 1)));
            Assert.IsTrue(sut.Contains(new Point(2, 2)));
        }
示例#2
0
        public void Does_Not_Contain_Point()
        {
            var sut = new LineSegment(new Point(0, 0), new Point(2, 2));

            Assert.IsFalse(sut.Contains(new Point(-1, -1)));
            Assert.IsFalse(sut.Contains(new Point(3, 3)));
            Assert.IsFalse(sut.Contains(new Point(3, 0)));
            Assert.IsFalse(sut.Contains(new Point(1, 2)));
        }
示例#3
0
        public Point Intersect(LineSegment segment)
        {
            if (Start.Equals(segment.End))
                return Start;
            if (End.Equals(segment.Start))
                return End;
            if ( Gradient == segment.Gradient)
                return null;
            
            if (Gradient == null)
                return intersectWithVertical(Start.X, segment);
            if (segment.Gradient == null)
                return intersectWithVertical(segment.Start.X, this);
            if (Gradient == 0)
                return intersectWithHorizontal(Start.Y, segment);
            if (segment.Gradient == 0)
                return intersectWithHorizontal(segment.Start.Y, this);

            var b1 = Start.Y - Start.X * Gradient.Value;
            var b2 = segment.Start.Y - segment.Start.X * segment.Gradient.Value;

            var x = (b1 - b2)/(segment.Gradient.Value - Gradient.Value);
            var y = Gradient.Value*x + b1;

            var point = new Point(x, y);

            if (Contains(point) && segment.Contains(point))
                return point;

            return null;
        }