示例#1
0
        /// <summary>
        /// True if this crosses the line and returns the intersectin points.
        /// </summary>
        /// <param name="Line">The line.</param>
        /// <param name="IntersectionPts">The point set to recieve the result.</param>
        public bool Crosses(C2DLine Line, List <C2DPoint> IntersectionPts)
        {
            double x1 = Line.point.x;
            double x2 = Line.point.x + Line.vector.i;
            double x3 = _Centre.x;

            double y1 = Line.point.y;
            double y2 = Line.point.y + Line.vector.j;
            double y3 = _Centre.y;

            double r = Radius;

            double a = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);

            double b = 2 * ((x2 - x1) * (x1 - x3) + (y2 - y1) * (y1 - y3));

            double c = x3 * x3 + y3 * y3 + x1 * x1 + y1 * y1 - 2 * (x3 * x1 + y3 * y1) - r * r;

            double u = -b / (2 * a);

            C2DPoint ptClosestToCen = new C2DPoint();

            if (u < 0)
            {
                ptClosestToCen.Set(Line.point);
            }
            else if (u > 1)
            {
                ptClosestToCen.Set(Line.GetPointTo());
            }
            else
            {
                C2DVector V1 = new C2DVector(Line.vector);
                V1.Multiply(u);
                ptClosestToCen = Line.point.GetPointTo(V1);
            }

            double dDist = ptClosestToCen.Distance(_Centre);

            if (dDist > Radius)
            {
                return(false);
            }
            else
            {
                // Calculate the points.
                double d1 = b * b - 4 * a * c;
                Debug.Assert(d1 >= 0);

                if (d1 < 0)
                {
                    return(false);
                }
                else if (d1 == 0)
                {
                    double p1 = -b / (2 * a);
                    IntersectionPts.Add(Line.GetPointOn(p1));
                    return(true);
                }
                else
                {
                    d1 = Math.Sqrt(d1);
                    double p1 = (-b + d1) / (2 * a);
                    double p2 = (-b - d1) / (2 * a);

                    bool bResult = false;
                    if (p2 >= 0 && p2 <= 1)
                    {
                        bResult = true;
                        IntersectionPts.Add(Line.GetPointOn(p2));
                    }

                    if (p1 >= 0 && p1 <= 1)
                    {
                        bResult = true;
                        IntersectionPts.Add(Line.GetPointOn(p1));
                    }

                    return(bResult);
                }
            }
        }
        /// <summary>
        /// True if this crosses the line and returns the intersectin points.
        /// </summary>
        /// <param name="Line">The line.</param> 
        /// <param name="IntersectionPts">The point set to recieve the result.</param> 
        public bool Crosses(C2DLine Line,  List<C2DPoint> IntersectionPts)
        {
            double x1 = Line.point.x;
            double x2 = Line.point.x + Line.vector.i;
            double x3 = _Centre.x;

            double y1 = Line.point.y;
            double y2 = Line.point.y + Line.vector.j;
            double y3 = _Centre.y;

            double r = Radius;

            double a = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);

            double b = 2 * ((x2 - x1) * (x1 - x3) + (y2 - y1) * (y1 - y3));

            double c = x3 * x3 + y3 * y3 + x1 * x1 + y1 * y1 - 2 * (x3 * x1 + y3 * y1) - r * r;

            double u = -b / (2 * a);

            C2DPoint ptClosestToCen = new C2DPoint();

            if (u < 0)
            {
                ptClosestToCen.Set( Line.point );
            }
            else if (u > 1)
            {
                ptClosestToCen.Set( Line.GetPointTo());
            }
            else
            {
                C2DVector V1 = new C2DVector(Line.vector);
                V1.Multiply(u);
                ptClosestToCen = Line.point.GetPointTo(V1);
            }

            double dDist = ptClosestToCen.Distance(_Centre);

            if (dDist > Radius)
            {
                return false;
            }
            else
            {
                // Calculate the points.
                double d1 = b * b - 4 * a * c;
                Debug.Assert(d1 >= 0);

                if (d1 < 0)
                    return false;
                else if (d1 == 0)
                {
                    double p1 = -b / (2 * a);
                    IntersectionPts.Add(Line.GetPointOn(p1));
                    return true;
                }
                else
                {
                    d1 = Math.Sqrt(d1);
                    double p1 = (-b + d1) / (2 * a);
                    double p2 = (-b - d1) / (2 * a);

                    bool bResult = false;
                    if (p2 >= 0 && p2 <= 1)
                    {
                        bResult = true;
                        IntersectionPts.Add(Line.GetPointOn(p2));
                    }

                    if (p1 >= 0 && p1 <= 1)
                    {
                        bResult = true;
                        IntersectionPts.Add(Line.GetPointOn(p1));
                    }

                    return bResult;
                }
            }
        }