コード例 #1
0
        private void DetectIntersectSegments(List <Vector2d> footprint, Vector2d start, Vector2d end,
                                             out Vector2d first, out int firstIndex, out Vector2d second, out int secondIndex)
        {
            firstIndex  = -1;
            secondIndex = -1;
            first       = default(Vector2d);
            second      = default(Vector2d);
            for (int i = 0; i < footprint.Count; i++)
            {
                var p1 = footprint[i];
                var p2 = footprint[i == footprint.Count - 1 ? 0 : i + 1];

                double r;
                if (Vector2dUtils.LineIntersects(start, end, p1, p2, out r))
                {
                    var intersectionPoint = Vector2dUtils.GetPointAlongLine(p1, p2, r);
                    if (firstIndex == -1)
                    {
                        firstIndex = i;
                        first      = intersectionPoint;
                    }
                    else
                    {
                        secondIndex = i;
                        second      = intersectionPoint;
                        break;
                    }
                }
            }
        }
コード例 #2
0
        public void CanDetectIntersection()
        {
            // ARRANGE
            var    s1 = new Vector2d(0, 0);
            var    e1 = new Vector2d(10, 10);
            var    s2 = new Vector2d(10, 0);
            var    e2 = new Vector2d(0, 10);
            double r;

            // ACT
            var result             = Vector2dUtils.LineIntersects(s1, e1, s2, e2, out r);
            var currIntersectPoint = Vector2dUtils.GetPointAlongLine(s1, s2, r);

            // ASSERT
            Assert.IsTrue(result);
            Assert.IsTrue(Math.Abs(currIntersectPoint.X - 5) < MathUtils.Epsion);
            Assert.IsTrue(Math.Abs(currIntersectPoint.X - 5) < MathUtils.Epsion);
        }