Exemple #1
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;
        }
Exemple #2
0
 public void CalculateCoords(MM_Line line, int zoomLevel)
 {
     LineProxy.CalculateCoords(this, line, zoomLevel);
 }