コード例 #1
0
        /// <summary>
        /// Test if a point touches a multi-segment line.
        /// </summary>
        /// <param name="testPoint">Point to test.</param>
        /// <param name="width">Test width of line.</param>
        /// <param name="bounds">Bounding rectagle of all points.</param>
        /// <param name="line"></param>
        /// <returns></returns>
        public static bool HitTestLine(RawVector2 testPoint, float width, SharpDX.RectangleF bounds, IList <RawVector2> line)
        {
            // if we are outside the bounds of the rectangle containing the line, false
            // TODO: fix bounds test
            // if (!bounds.Contains(testPoint)) return false;

            // if the line doesn't have any segments, return false
            if (line.Count < 2)
            {
                return(false);
            }

            // test each segment
            for (int i = 0; i < line.Count - 1; i++)
            {
                if (HitTestLineSegment(testPoint, line[i], line[i + 1], width))
                {
                    return(true);
                }
            }

            // no segments within range
            return(false);
        }
コード例 #2
0
        public static void CalculateCoords(LineProxy proxy, MM_Line line, int zoomLevel)
        {
            proxy.Coordinates.Clear();
            //Ignore invalid lines
            if (float.IsNaN(line.CenterLngLat.X))
            {
                return;
            }

            // flip the line coords
            if (line.Coordinates.Count > 0 && (line.Substation2.LngLat == line.Coordinates[0] || line.Substation1.LngLat == line.Coordinates[line.Coordinates.Count - 1]))
            {
                line.Coordinates.Reverse();
            }

            if (line.Coordinates.Count == 0)
            {
                line.Coordinates.Add(line.Substation1.LngLat);
                line.Coordinates.Add(line.Substation2.LngLat);
            }
            //Determine if we need to flip coordinates because the substations have changed
            if (line.Coordinates.Count > 0 && (line.Substation2.LngLat == line.Coordinates[0] || line.Substation1.LngLat == line.Coordinates[line.Coordinates.Count - 1]))
            {
                line.Coordinates.Reverse();
            }

            Vector2 lastPoint = Vector2.Zero;
            Vector2 aggPoint  = Vector2.Zero;

            float minX = float.MaxValue;
            float minY = float.MaxValue;
            float maxY = float.MinValue;
            float maxX = float.MinValue;


            float length = 0;

            if (proxy.Segments == null)
            {
                proxy.Segments = new List <LineSegment>();
            }
            else
            {
                proxy.Segments.Clear();
            }
            if (line.Coordinates.Count > 0)
            {
                for (int i = 0; i < line.Coordinates.Count; i++)
                {
                    PointF  Pt           = line.Coordinates[i];
                    Vector2 currentPoint = MM_Coordinates.LngLatToScreenVector2(Pt, zoomLevel);
                    if (lastPoint.IsZero || lastPoint.X != currentPoint.X || lastPoint.Y != currentPoint.Y)
                    {
                        aggPoint.X += currentPoint.X;
                        aggPoint.Y += currentPoint.Y;
                    }
                    proxy.Coordinates.Add(currentPoint);


                    if (currentPoint.X < minX)
                    {
                        minX = currentPoint.X;
                    }
                    if (currentPoint.X > maxX)
                    {
                        maxX = currentPoint.X;
                    }

                    if (currentPoint.Y < minY)
                    {
                        minY = currentPoint.Y;
                    }
                    if (currentPoint.Y > maxY)
                    {
                        maxY = currentPoint.Y;
                    }

                    if (i > 0)
                    {
                        var segment = new LineSegment(lastPoint, currentPoint);
                        proxy.Segments.Add(segment);
                        length += segment.length;
                    }

                    lastPoint = currentPoint;
                }
            }
            proxy.Length = length;
            var bounds = new SharpDX.RectangleF(minX, minY, maxX - minX, maxY - minY);

            proxy.Bounds = bounds;

            lastPoint    = proxy.Coordinates[proxy.Coordinates.Count - 1];
            proxy.Center = new Vector2(aggPoint.X / (float)proxy.Coordinates.Count, aggPoint.Y / (float)proxy.Coordinates.Count);

            var lineAngle = (float)Math.Atan2(proxy.Coordinates[0].Y - lastPoint.Y, proxy.Coordinates[0].X - lastPoint.X);

            // if (lineAngle < 0)
            // {
            //     lineAngle += (float)(Math.PI * 2);
            // }
            if (lineAngle > (float)Math.PI / 2f)
            {
                lineAngle       = (float)lineAngle - (float)Math.PI;
                proxy.FlipSides = true;
            }
            else if (lineAngle < (float)Math.PI / -2f)
            {
                lineAngle       = (float)lineAngle - (float)Math.PI;
                proxy.FlipSides = true;
            }
            else
            {
                proxy.FlipSides = false;
            }

            //lineAngle *= 180f / (float)Math.PI;

            proxy.Angle = lineAngle;
        }