示例#1
0
        /// <summary>
        /// Calculates the intersection points of a line against this ellipse.
        /// </summary>
        /// <param name="lineP1">Origin point of the line.</param>
        /// <param name="lineP2">End point of the line.</param>
        /// <param name="inSegment">Choose only those intersection points in the segment P1-P2.</param>
        /// <returns></returns>
        public List <Vector2> Intersect(Vector3 lineP1, Vector3 lineP2, bool inSegment)
        {
            // Source:
            //http://csharphelper.com/blog/2017/08/calculate-where-a-line-segment-and-an-ellipse-intersect-in-c/

            float xVector2        = Mathf.Pow((lineP2.x - lineP1.x), 2);
            float yVector2        = Mathf.Pow((lineP2.y - lineP1.y), 2);
            float horizontalValue = (xVector2 / HorizontalRadius) / HorizontalRadius;
            float verticalValue   = (yVector2 / VerticalRadius) / VerticalRadius;

            float A = horizontalValue + verticalValue;
            float B = 2 * lineP1.x * horizontalValue + 2 * lineP1.y * verticalValue;
            float C = lineP1.x * horizontalValue + lineP1.y * verticalValue - 1;

            float[] t = Hedra.QuadraticFormula(A, B, C);

            List <Vector2> intersectionPoints = new List <Vector2>();

            for (int i = 0; i < t.Length; i++)
            {
                if (!inSegment || ((t[i] >= 0f) && (t[i] <= 1f)))
                {
                    Vector2 point = new Vector2();
                    point.x = lineP1.x + (lineP2.x - lineP1.x) * t[i] + Center.x;
                    point.y = lineP1.y + (lineP2.y - lineP1.y) * t[i] + Center.y;

                    intersectionPoints.Add(point);
                }
            }

            return(intersectionPoints);
        }