コード例 #1
0
        public LinesIntersection2?Intersects(Ray2 ray, out Parallelism parallelism)
        {
            //http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect

            var p = Position;
            var r = Direction;

            var q = ray.Position;
            var s = ray.Direction;

            // It isn't maths if the variable names make any sense
            // ReSharper disable InconsistentNaming
            var RxS = r.Cross(s);
            var QmP = q - p;

            // ReSharper restore InconsistentNaming

            if (Math.Abs(RxS - 0) < 0.001)
            {
                // ReSharper disable InconsistentNaming
                var QmPxR = QmP.Cross(r);
                // ReSharper restore InconsistentNaming
                parallelism = Math.Abs(QmPxR - 0) < 0.001 ? Geometry.Parallelism.Collinear : Geometry.Parallelism.Parallel;
                return(null);
            }

            var t = QmP.Cross(s) / RxS;
            var u = QmP.Cross(r) / RxS;

            var point = p + (t * r);

            parallelism = Geometry.Parallelism.None;
            return(new LinesIntersection2(point, t, u));
        }
コード例 #2
0
        public Parallelism Parallelism(Ray2 ray)
        {
            //http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect

            var p = Position;
            var r = Direction;

            var q = ray.Position;
            var s = ray.Direction;

            // It isn't maths if the variable names make any sense
            // ReSharper disable InconsistentNaming
            var RxS = r.Cross(s);
            var QmP = q - p;

            // ReSharper restore InconsistentNaming

            if (Math.Abs(RxS - 0) < 0.001)
            {
                // ReSharper disable InconsistentNaming
                var QmPxR = QmP.Cross(r);
                // ReSharper restore InconsistentNaming
                return(Math.Abs(QmPxR - 0) < 0.001 ? Geometry.Parallelism.Collinear : Geometry.Parallelism.Parallel);
            }

            return(Geometry.Parallelism.None);
        }
コード例 #3
0
        public void AssertThat_RayRayIntersection_FindsParallelLines()
        {
            Parallelism para;
            var i = new Ray2(new Vector2(0, 10), new Vector2(0, 1)).Intersects(new Ray2(new Vector2(20, 5), new Vector2(0, 1)), out para);

            Assert.AreEqual(Parallelism.Parallel, para);
            Assert.IsFalse(i.HasValue);
        }
コード例 #4
0
        public void AssertThat_RayRayIntersection_FindsCorrectIntersection()
        {
            var i = new Ray2(new Vector2(0, 10), new Vector2(0, 1)).Intersects(new Ray2(new Vector2(20, 5), new Vector2(1, 0)));

            Assert.IsTrue(i.HasValue);
            Assert.AreEqual(-5, i.Value.DistanceAlongA);
            Assert.AreEqual(-20, i.Value.DistanceAlongB);
            Assert.AreEqual(new Vector2(0, 5), i.Value.Position);
        }
コード例 #5
0
        public LinesIntersection2?Intersects(Ray2 ray, out Parallelism parallelism)
        {
            var intersection = LongLine.Intersects(ray, out parallelism);

            if (!intersection.HasValue)
            {
                return(null);
            }

            if (intersection.Value.DistanceAlongA <= 0 || intersection.Value.DistanceAlongA >= 1)
            {
                return(null);
            }

            return(intersection);
        }
コード例 #6
0
        public void AssertThat_DistanceToPoint_AlwaysEqualsDistanceToClosestPoint()
        {
            Random r = new Random(1);
            for (int i = 0; i < 1000; i++)
            {
                var start = new Vector2((float)r.NextDouble(), (float)r.NextDouble());
                var end = new Vector2((float)r.NextDouble(), (float)r.NextDouble());

                var point = new Vector2((float)r.NextDouble(), (float)r.NextDouble());

                var ry = new Ray2(start, end - start);

                //Check that naieve distance to closest point (literally Distance(CalculateClosestPoint() - Point)
                //is the same as the more refined calculation
                Assert.AreEqual((ry.ClosestPoint(point) - point).Length(), Math.Abs(ry.DistanceToPoint(point)), 0.001f);
            }
        }
コード例 #7
0
        public void AssertThat_DistanceToPoint_IsSignedDistance_Positive()
        {
            Ray2 r = new Ray2(new Vector2(1, 1), new Vector2(10, 0));

            Assert.AreEqual(9, r.DistanceToPoint(new Vector2(0, 10)));
        }
コード例 #8
0
        public void AssertThat_DistanceToPoint_IsSignedDistance_Negative()
        {
            var r = new Ray2(new Vector2(1, 1), new Vector2(20, 0));

            Assert.AreEqual(-9, r.DistanceToPoint(new Vector2(0, -8)));
        }
コード例 #9
0
 /// <summary>
 /// Determines whether the specified Ray is equal to the current Ray.
 /// </summary>
 /// <param name="other">The Ray to compare with the current Ray.</param>
 public bool Equals(Ray2 other)
 {
     return(Position.Equals(other.Position) && Direction.Equals(other.Direction));
 }
コード例 #10
0
 public LinesIntersection2?Intersects(Ray2 ray)
 {
     return(Intersects(ray, out var _));
 }
コード例 #11
0
 public LinesIntersection2? Intersects(Ray2 ray)
 {
     Parallelism _;
     return Intersects(ray, out _);
 }
コード例 #12
0
        public LinesIntersection2? Intersects(Ray2 ray, out Parallelism parallelism)
        {
            var intersection = LongLine.Intersects(ray, out parallelism);

            if (!intersection.HasValue)
                return null;

            if (intersection.Value.DistanceAlongA <= 0 || intersection.Value.DistanceAlongA >= 1)
                return null;

            return intersection;
        }
コード例 #13
0
ファイル: Ray2.cs プロジェクト: martindevans/SwizzleMyVectors
        public Parallelism Parallelism(Ray2 ray)
        {
            //http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect

            var p = Position;
            var r = Direction;

            var q = ray.Position;
            var s = ray.Direction;

            // It isn't maths if the variable names make any sense
            // ReSharper disable InconsistentNaming
            var RxS = r.Cross(s);
            var QmP = q - p;
            // ReSharper restore InconsistentNaming

            if (Math.Abs(RxS - 0) < 0.001)
            {
                // ReSharper disable InconsistentNaming
                var QmPxR = QmP.Cross(r);
                // ReSharper restore InconsistentNaming
                return Math.Abs(QmPxR - 0) < 0.001 ? Geometry.Parallelism.Collinear : Geometry.Parallelism.Parallel;
            }

            return Geometry.Parallelism.None;
        }
コード例 #14
0
ファイル: Ray2.cs プロジェクト: martindevans/SwizzleMyVectors
        public LinesIntersection2? Intersects(Ray2 ray, out Parallelism parallelism)
        {
            //http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect

            var p = Position;
            var r = Direction;

            var q = ray.Position;
            var s = ray.Direction;

            // It isn't maths if the variable names make any sense
            // ReSharper disable InconsistentNaming
            var RxS = r.Cross(s);
            var QmP = q - p;
            // ReSharper restore InconsistentNaming

            if (Math.Abs(RxS - 0) < 0.001)
            {
                // ReSharper disable InconsistentNaming
                var QmPxR = QmP.Cross(r);
                // ReSharper restore InconsistentNaming
                parallelism = Math.Abs(QmPxR - 0) < 0.001 ? Geometry.Parallelism.Collinear : Geometry.Parallelism.Parallel;
                return null;
            }

            var t = QmP.Cross(s) / RxS;
            var u = QmP.Cross(r) / RxS;

            var point = p + (t * r);

            parallelism = Geometry.Parallelism.None;
            return new LinesIntersection2(point, t, u);
        }
コード例 #15
0
ファイル: Ray2.cs プロジェクト: martindevans/SwizzleMyVectors
 /// <summary>
 /// Determines whether the specified Ray is equal to the current Ray.
 /// </summary>
 /// <param name="other">The Ray to compare with the current Ray.</param>
 public bool Equals(Ray2 other)
 {
     return Position.Equals(other.Position) && Direction.Equals(other.Direction);
 }