/// <summary>
        /// Calculates the interception point of two Lines.
        /// </summary>
        /// <param name="other"></param>
        /// <param name="tolerance"></param>
        /// <returns>Returns a Point if func succeeds. If there is no interception, empty point is returned.</returns>
        public Vector2? IntersectLine(LineSegment2 other, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {

            double interPntX = 0;
            double interPntY = 0;

            if (other == null || this.IsParallelTo(other, tolerance))
            {
                return null;            // Lines are parralell
            }

            //intercept of two endless lines
            if (!this.IsVertical && !other.IsVertical) {    // both NOT vertical
                interPntX = ((-1 * (this.YMovement - other.YMovement)) / (this.Slope - other.Slope));
                interPntY = (this.Slope * interPntX + this.YMovement);
            } else if (this.IsVertical) {                  // this vertical (so it must lie on this.X)
                interPntX = this.Start.X;
                interPntY = (other.Slope * interPntX + other.YMovement);
            } else if (other.IsVertical) {                // Line2 vertical (so it must lie on Line2.X)
                interPntX = other.Start.X;
                interPntY = (this.Slope * interPntX + this.YMovement);
            }

            var interPnt = new Vector2(interPntX, interPntY);

            //check if computed intercept lies on our line.
            if (this.Contains(interPnt, tolerance) && other.Contains(interPnt, tolerance))
            {
                return interPnt;
            }else
                return null;
        }
        /// <summary>
        /// Calculates the interception point of two Lines.
        /// </summary>
        /// <param name="other"></param>
        /// <param name="tolerance"></param>
        /// <returns>Returns a Point if func succeeds. If there is no interception, empty point is returned.</returns>
        public Vector2?IntersectLine(LineSegment2 other, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            double interPntX = 0;
            double interPntY = 0;

            if (other == null || this.IsParallelTo(other, tolerance))
            {
                return(null);            // Lines are parralell
            }

            //intercept of two endless lines
            if (!this.IsVertical && !other.IsVertical)      // both NOT vertical
            {
                interPntX = ((-1 * (this.YMovement - other.YMovement)) / (this.Slope - other.Slope));
                interPntY = (this.Slope * interPntX + this.YMovement);
            }
            else if (this.IsVertical)                      // this vertical (so it must lie on this.X)
            {
                interPntX = this.Start.X;
                interPntY = (other.Slope * interPntX + other.YMovement);
            }
            else if (other.IsVertical)                    // Line2 vertical (so it must lie on Line2.X)
            {
                interPntX = other.Start.X;
                interPntY = (this.Slope * interPntX + this.YMovement);
            }

            var interPnt = new Vector2(interPntX, interPntY);

            //check if computed intercept lies on our line.
            if (this.Contains(interPnt, tolerance) && other.Contains(interPnt, tolerance))
            {
                return(interPnt);
            }
            else
            {
                return(null);
            }
        }
        /// <summary> 
        /// Circle-Line Interception 
        /// </summary>
        /// <param name="uLine"></param>
        /// <returns>Returns all intersection points</returns>
        private List<Vector2> InterceptLine(LineSegment2 uLine, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var intersections = new List<Vector2>();
            Vector2 p1, p2;
            var location = this.Location;

            // we assume that the circle Middlepoint is NULL/NULL
            // So we move the Line with the delta to NULL
            var helperLine = new LineSegment2(uLine.Start - location, uLine.End - location);

            // line
            var q = helperLine.YMovement;
            var m = helperLine.Slope;

            if (!helperLine.IsVertical)
            {
                // The slope is defined as the Line isn't vertical

                var discriminant = (Math.Pow(m, 2) + 1) * Math.Pow(this.Radius, 2) - Math.Pow(q, 2);
                if (discriminant > 0)
                {
                    // only positive discriminants for f() -> sqrt(discriminant) results are defined in |R



                    var p1X = (Math.Sqrt(discriminant) - m * (q)) / (Math.Pow(m, 2) + 1);
                    var p1Y = m * p1X + q;
                    var p2X = (-1 * (Math.Sqrt(discriminant) + m * q)) / (Math.Pow(m, 2) + 1);
                    var p2Y = m * p2X + q;

                    p1 = new Vector2(p1X, p1Y);
                    p2 = new Vector2(p2X, p2Y);

                    if (helperLine.Contains(p1, tolerance))
                    {
                        intersections.Add(p1 + location);
                    }
                    if ((p1.X != p2.X) || (p1.Y != p2.Y))
                    {
                        if (helperLine.Contains(p2, tolerance))
                        {
                            intersections.Add(p2 + location);
                        }
                    }
                }
            }
            else
            {
                // undefined slope, so we have to deal with it directly

                var p1X = this.Location.X + helperLine.Start.X;
                var p1Y = Math.Sqrt(Math.Pow(this.Radius, 2) - Math.Pow(p1X, 2));
                p1 = new Vector2(p1X, p1Y);
                p2 = new Vector2(p1.X, -p1.Y);

                if (helperLine.Contains(p1, tolerance))
                {
                    intersections.Add(p1 + location);
                }
                if (helperLine.Contains(p2, tolerance))
                {
                    intersections.Add(p2 + location);
                }
            }

            return intersections;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Circle-Line Interception
        /// </summary>
        /// <param name="uLine"></param>
        /// <returns>Returns all intersection points</returns>
        private List <Vector2> InterceptLine(LineSegment2 uLine, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var     intersections = new List <Vector2>();
            Vector2 p1, p2;
            var     location = this.Location;

            // we assume that the circle Middlepoint is NULL/NULL
            // So we move the Line with the delta to NULL
            var helperLine = new LineSegment2(uLine.Start - location, uLine.End - location);

            // line
            var q = helperLine.YMovement;
            var m = helperLine.Slope;

            if (!helperLine.IsVertical)
            {
                // The slope is defined as the Line isn't vertical

                var discriminant = (Math.Pow(m, 2) + 1) * Math.Pow(this.Radius, 2) - Math.Pow(q, 2);
                if (discriminant > 0)
                {
                    // only positive discriminants for f() -> sqrt(discriminant) results are defined in |R



                    var p1X = (Math.Sqrt(discriminant) - m * (q)) / (Math.Pow(m, 2) + 1);
                    var p1Y = m * p1X + q;
                    var p2X = (-1 * (Math.Sqrt(discriminant) + m * q)) / (Math.Pow(m, 2) + 1);
                    var p2Y = m * p2X + q;

                    p1 = new Vector2(p1X, p1Y);
                    p2 = new Vector2(p2X, p2Y);

                    if (helperLine.Contains(p1, tolerance))
                    {
                        intersections.Add(p1 + location);
                    }
                    if ((p1.X != p2.X) || (p1.Y != p2.Y))
                    {
                        if (helperLine.Contains(p2, tolerance))
                        {
                            intersections.Add(p2 + location);
                        }
                    }
                }
            }
            else
            {
                // undefined slope, so we have to deal with it directly

                var p1X = this.Location.X + helperLine.Start.X;
                var p1Y = Math.Sqrt(Math.Pow(this.Radius, 2) - Math.Pow(p1X, 2));
                p1 = new Vector2(p1X, p1Y);
                p2 = new Vector2(p1.X, -p1.Y);

                if (helperLine.Contains(p1, tolerance))
                {
                    intersections.Add(p1 + location);
                }
                if (helperLine.Contains(p2, tolerance))
                {
                    intersections.Add(p2 + location);
                }
            }

            return(intersections);
        }