Example #1
0
        public override bool Intersects(GeoLine line, out GeoPoint intersection1, out GeoPoint intersection2, out int intersections)
        {
            intersection1 = new GeoPoint(float.NaN, float.NaN);
            intersection2 = new GeoPoint(float.NaN, float.NaN);

            const Double increment = .1;

            bool     intersects = false;
            GeoPoint p          = line.P1.Clone() as GeoPoint;

            do
            {
                if (this.Contains(p))
                {
                    intersects = true;
                    break;
                }

                p = EarthGeo.GetPoint(p, line.Bearing, increment);
            } while(EarthGeo.GetDistance(line.P1 as GeoPoint, p) < line.Length);

            intersections = 0;
            if (intersects)
            {
                Double degy   = EarthGeo.DegreesPerMeterAtLatitude(_center.Latitude);
                Double degx   = EarthGeo.DegreesPerMeterAtLongitude(_center.Longitude);
                Double radius = Radius * Math.Max(degx, degy);

                Double dx, dy, A, B, C, det, t;

                dx = line.P2.X - line.P1.X;
                dy = line.P2.Y - line.P1.Y;

                A = dx * dx + dy * dy;
                B = 2 * (dx * (line.P1.X - Center.X) + dy * (line.P1.Y - Center.Y));
                C = (line.P1.X - Center.X) * (line.P1.X - Center.X) + (line.P1.Y - Center.Y) * (line.P1.Y - Center.Y) - radius * radius;

                det = B * B - 4 * A * C;

                if ((A <= 0.00000001) || (det < 0))
                {
                    // No real solutions.
                    intersection1 = new GeoPoint(float.NaN, float.NaN);
                    intersection2 = new GeoPoint(float.NaN, float.NaN);
                }
                else if (det == 0)
                {
                    // One solution.
                    t             = -B / (2 * A);
                    intersection1 = new GeoPoint(line.P1.X + t * dx, line.P1.Y + t * dy);
                    intersection2 = new GeoPoint(float.NaN, float.NaN);
                    intersections = 1;
                }
                else
                {
                    // Two solutions.
                    t             = (float)((-B + Math.Sqrt(det)) / (2 * A));
                    intersection1 = new GeoPoint(line.P1.X + t * dx, line.P1.Y + t * dy);
                    t             = (float)((-B - Math.Sqrt(det)) / (2 * A));
                    intersection2 = new GeoPoint(line.P1.X + t * dx, line.P1.Y + t * dy);
                    intersections = 2;
                }
            }
            return(intersects);
        }