Exemplo n.º 1
0
        /// <summary>
        /// Get intersection points between a straight line and circle.
        /// </summary>
        /// <param name="line">The straight line used for the intersection.</param>
        /// <param name="circle">The circle used for the intersection.</param>
        /// <param name="intersections"></param>
        /// <returns></returns>
        public bool GetIntersection(StraightLine line, Circle circle, out Vector2[] intersections)
        {
            float   g = line.Gradient;
            float   i = line.Intercept;
            Vector2 c = circle.Transform.PositionGlobal;
            float   r = circle.Radius;

            //Setup polynomial equation
            float A = 1 + g * g;
            float B = -2 * c.X - 2 * g * i + 2 * c.Y * g;
            float C = c.X * c.X + g * g + 2 * c.Y * i + c.Y * c.Y - r * r;

            float[] xSolutions = m_solver.Poly_2(A, B, C);
            intersections = new Vector2[xSolutions.Length];

            if (xSolutions.Length == 0)
            {
                return(false);
            }

            for (int j = 0; j < intersections.Length; ++j)
            {
                intersections[j].X = xSolutions[j];
                intersections[j].Y = g * intersections[j].X + i;
            }

            return(true);
        }
Exemplo n.º 2
0
        public Vector2 GetProjectionPoint(StraightLine screen, Vector2 projector)
        {
            Vector2 pointA = new Vector2(0, screen.GetY(0));
            Vector2 AB     = screen.Direction;
            Vector2 AX     = Vector2.Subtract(projector, pointA);

            return(pointA + AB * GetProjectionRatio(AB, AX));
        }
Exemplo n.º 3
0
        public bool Intersects(Circle circle, StraightLine line)
        {
            //Circle center - Projection vector
            Vector2 XP = GetProjectionPoint(line, circle.Transform.PositionGlobal) - circle.Transform.PositionGlobal;

            if (XP.Length() < circle.Radius)
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 4
0
 public bool BelongsToLine(StraightLine L2)
 {
     if (IsVertical != L2.IsVertical)
     {
         return(false);
     }
     if (IsVertical && L2.IsVertical)
     {
         return(A_Global.X == L2.GetX(0));
     }
     return(Gradient == L2.Gradient &&
            Intercept == L2.Intercept);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Returns true if the lines are equal, false if they are not.
        /// </summary>
        /// <param name="lineA">First line to test.</param>
        /// <param name="lineB">Second Line to test.</param>
        /// <param name="intersection">The point of intersection.</param>
        /// <returns>True if the lines are equal</returns>
        public bool GetIntersection(StraightLine lineA, StraightLine lineB, out Vector2 intersection)
        {
            float x;
            float y;
            bool  aV = lineA.IsVertical;
            bool  bV = lineB.IsVertical;

            intersection = new Vector2();

            //Lines are equal: intersection.
            if (lineA.Equals(lineB))
            {
                return(true);
            }

            //Lines are parallel: no intersection.
            if (lineA.IsParallel(lineB))
            {
                return(false);
            }

            if (aV && !bV)
            {
                x            = -lineA.C / lineA.A;
                y            = (-lineB.A * x - lineB.C) / lineB.B;
                intersection = new Vector2(x, y);
                return(true);
            }
            if (!aV && bV)
            {
                x            = lineB.GetX(0);
                y            = (-lineA.A * x - lineA.C) / lineA.B;
                intersection = new Vector2(x, y);
                return(true);
            }
            if (!aV && !bV)
            {
                x            = (lineB.Intercept - lineA.Intercept) / (lineA.Gradient - lineB.Gradient);
                y            = lineA.Gradient * x + lineA.Intercept;
                intersection = new Vector2(x, y);
                return(true);
            }

            throw new Exception("Well that wasn't supposed to happen...");
        }
Exemplo n.º 6
0
 public bool IsEqual(StraightLine L2)
 {
     return(A == L2.A && B == L2.B && C == L2.C);
 }
Exemplo n.º 7
0
 public bool IsParallel(StraightLine L2)
 {
     return((IsVertical && L2.IsVertical) ||
            Gradient == L2.Gradient);
 }
 public bool Intersects(Circle circle, StraightLine line)
 {
     //Circle center - Projection vector
     Vector2 XP = GetProjectionPoint(line, circle.Transform.PositionGlobal) - circle.Transform.PositionGlobal;
     if (XP.Length() < circle.Radius)
         return true;
     return false;
 }
 public Vector2 GetProjectionPoint(StraightLine screen, Vector2 projector)
 {
     Vector2 pointA = new Vector2(0, screen.GetY(0));
     Vector2 AB = screen.Direction;
     Vector2 AX = Vector2.Subtract(projector, pointA);
     return pointA + AB * GetProjectionRatio(AB, AX);
 }
Exemplo n.º 10
0
        /// <summary>
        /// Returns true if the lines are equal, false if they are not.
        /// </summary>
        /// <param name="lineA">First line to test.</param>
        /// <param name="lineB">Second Line to test.</param>
        /// <param name="intersection">The point of intersection.</param>
        /// <returns>True if the lines are equal</returns>
        public bool GetIntersection(StraightLine lineA, StraightLine lineB, out Vector2 intersection)
        {
            float x;
            float y;
            bool aV = lineA.IsVertical;
            bool bV = lineB.IsVertical;
            intersection = new Vector2();

            //Lines are equal: intersection.
            if (lineA.Equals(lineB))
                return true;

            //Lines are parallel: no intersection.
            if (lineA.IsParallel(lineB))
                return false;

            if (aV && !bV)
            {
                x = -lineA.C / lineA.A;
                y = (-lineB.A * x - lineB.C) / lineB.B;
                intersection = new Vector2(x, y);
                return true;
            }
            if (!aV && bV)
            {
                x = lineB.GetX(0);
                y = (-lineA.A * x - lineA.C) / lineA.B;
                intersection = new Vector2(x, y);
                return true;
            }
            if (!aV && !bV)
            {
                x = (lineB.Intercept - lineA.Intercept) / (lineA.Gradient - lineB.Gradient);
                y = lineA.Gradient * x + lineA.Intercept;
                intersection = new Vector2(x, y);
                return true;
            }

            throw new Exception("Well that wasn't supposed to happen...");
        }
Exemplo n.º 11
0
        /// <summary>
        /// Get intersection points between a straight line and circle.
        /// </summary>
        /// <param name="line">The straight line used for the intersection.</param>
        /// <param name="circle">The circle used for the intersection.</param>
        /// <param name="intersections"></param>
        /// <returns></returns>
        public bool GetIntersection(StraightLine line, Circle circle, out Vector2[] intersections)
        {
            float g = line.Gradient;
            float i = line.Intercept;
            Vector2 c = circle.Transform.PositionGlobal;
            float r = circle.Radius;

            //Setup polynomial equation
            float A = 1 + g * g;
            float B = -2 * c.X - 2 * g * i + 2 * c.Y * g;
            float C = c.X * c.X + g * g + 2 * c.Y * i + c.Y * c.Y - r * r;

            float[] xSolutions = m_solver.Poly_2(A, B, C);
            intersections = new Vector2[xSolutions.Length];

            if (xSolutions.Length == 0)
                return false;

            for(int j = 0; j < intersections.Length; ++j)
            {
                intersections[j].X = xSolutions[j];
                intersections[j].Y = g * intersections[j].X + i;
            }

            return true;
        }
Exemplo n.º 12
0
 public bool IsParallel(StraightLine L2)
 {
     return (IsVertical && L2.IsVertical)
         || Gradient == L2.Gradient;
 }
Exemplo n.º 13
0
 public bool BelongsToLine(StraightLine L2)
 {
     if (IsVertical != L2.IsVertical)
         return false;
     if (IsVertical && L2.IsVertical)
         return A_Global.X == L2.GetX(0);
     return Gradient == L2.Gradient
         && Intercept == L2.Intercept;
 }
Exemplo n.º 14
0
 public bool IsEqual(StraightLine L2)
 {
     return A == L2.A && B == L2.B && C == L2.C;
 }