mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
MapPolypoint implementation
This commit is contained in:
parent
d2c3382f46
commit
cb01a6a12f
|
|
@ -51,39 +51,31 @@ namespace MapControl
|
||||||
|
|
||||||
protected void UpdateData(IEnumerable<Location> locations, bool closed)
|
protected void UpdateData(IEnumerable<Location> locations, bool closed)
|
||||||
{
|
{
|
||||||
var pathFigures = new PathFigures();
|
var pathFigure = new PathFigure();
|
||||||
|
|
||||||
if (ParentMap != null && locations != null)
|
if (ParentMap != null && locations?.Count() >= 2)
|
||||||
{
|
{
|
||||||
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
|
var longitudeOffset = GetLongitudeOffset(Location ?? locations.First());
|
||||||
|
|
||||||
AddPolylinePoints(pathFigures, locations, longitudeOffset, closed);
|
AddPolylinePoints(pathFigure, locations, longitudeOffset, closed);
|
||||||
}
|
}
|
||||||
|
|
||||||
((PathGeometry)Data).Figures = pathFigures;
|
((PathGeometry)Data).Figures = new PathFigures { pathFigure };
|
||||||
|
|
||||||
InvalidateGeometry();
|
InvalidateGeometry(); // ignores an empty PathGeometry.Figures collection
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddPolylinePoints(PathFigures pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
|
private void AddPolylinePoints(PathFigure pathFigure, IEnumerable<Location> locations, double longitudeOffset, bool closed)
|
||||||
{
|
|
||||||
if (locations.Count() >= 2)
|
|
||||||
{
|
{
|
||||||
var points = locations
|
var points = locations
|
||||||
.Select(location => LocationToView(location, longitudeOffset))
|
.Select(location => LocationToView(location, longitudeOffset))
|
||||||
.Where(point => point.HasValue)
|
.Where(point => point.HasValue)
|
||||||
.Select(point => point.Value);
|
.Select(point => point.Value);
|
||||||
|
|
||||||
var figure = new PathFigure
|
pathFigure.StartPoint = points.First();
|
||||||
{
|
pathFigure.Segments.Add(new PolyLineSegment(points.Skip(1)));
|
||||||
StartPoint = points.First(),
|
pathFigure.IsClosed = closed;
|
||||||
IsClosed = closed,
|
pathFigure.IsFilled = true;
|
||||||
IsFilled = true
|
|
||||||
};
|
|
||||||
|
|
||||||
figure.Segments.Add(new PolyLineSegment(points.Skip(1)));
|
|
||||||
pathFigures.Add(figure);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,103 +0,0 @@
|
||||||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
||||||
// Copyright © 2024 Clemens Fischer
|
|
||||||
// Licensed under the Microsoft Public License (Ms-PL)
|
|
||||||
|
|
||||||
using System;
|
|
||||||
#if WPF
|
|
||||||
using System.Windows;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MapControl
|
|
||||||
{
|
|
||||||
public static class Intersections
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the intersection point of two line segments given by (p1,p2) and (p3,p4),
|
|
||||||
/// or null if no intersection exists. See https://stackoverflow.com/a/1968345.
|
|
||||||
/// </summary>
|
|
||||||
public static Point? GetIntersection(Point p1, Point p2, Point p3, Point p4)
|
|
||||||
{
|
|
||||||
var x12 = p2.X - p1.X;
|
|
||||||
var y12 = p2.Y - p1.Y;
|
|
||||||
var x34 = p4.X - p3.X;
|
|
||||||
var y34 = p4.Y - p3.Y;
|
|
||||||
var x13 = p3.X - p1.X;
|
|
||||||
var y13 = p3.Y - p1.Y;
|
|
||||||
|
|
||||||
var d = x12 * y34 - x34 * y12;
|
|
||||||
var s = (x13 * y12 - y13 * x12) / d;
|
|
||||||
var t = (x13 * y34 - y13 * x34) / d;
|
|
||||||
|
|
||||||
if (s >= 0d && s <= 1d && t >= 0d && t <= 1d)
|
|
||||||
{
|
|
||||||
return new Point(p1.X + t * x12, p1.Y + t * y12);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Calculates the potential intersections of a line segment given by (p1,p2) with a rectangle.
|
|
||||||
/// Updates either p1, p2, or both with any found intersection and returns a value that indicates
|
|
||||||
/// whether the segment intersects or lies inside the rectangle.
|
|
||||||
/// </summary>
|
|
||||||
public static bool GetIntersections(ref Point p1, ref Point p2, Rect rect)
|
|
||||||
{
|
|
||||||
if (rect.Contains(p1) && rect.Contains(p2))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var topLeft = new Point(rect.X, rect.Y);
|
|
||||||
var topRight = new Point(rect.X + rect.Width, rect.Y);
|
|
||||||
var bottomLeft = new Point(rect.X, rect.Y + rect.Height);
|
|
||||||
var bottomRight = new Point(rect.X + rect.Width, rect.Y + rect.Height);
|
|
||||||
var numIntersections = 0;
|
|
||||||
|
|
||||||
if (GetIntersection(ref p1, ref p2, topLeft, bottomLeft, p => p.X <= rect.X)) // left edge
|
|
||||||
{
|
|
||||||
numIntersections++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (GetIntersection(ref p1, ref p2, topLeft, topRight, p => p.Y <= rect.Y)) // top edge
|
|
||||||
{
|
|
||||||
numIntersections++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (numIntersections < 2 &&
|
|
||||||
GetIntersection(ref p1, ref p2, topRight, bottomRight, p => p.X >= rect.X + rect.Width)) // right edge
|
|
||||||
{
|
|
||||||
numIntersections++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (numIntersections < 2 &&
|
|
||||||
GetIntersection(ref p1, ref p2, bottomLeft, bottomRight, p => p.Y >= rect.Y + rect.Height)) // bottom edge
|
|
||||||
{
|
|
||||||
numIntersections++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return numIntersections > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool GetIntersection(ref Point p1, ref Point p2, Point p3, Point p4, Predicate<Point> predicate)
|
|
||||||
{
|
|
||||||
var intersection = GetIntersection(p1, p2, p3, p4);
|
|
||||||
|
|
||||||
if (!intersection.HasValue)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (predicate(p1))
|
|
||||||
{
|
|
||||||
p1 = intersection.Value;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
p2 = intersection.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -89,9 +89,6 @@
|
||||||
<Compile Include="..\Shared\ImageLoader.cs">
|
<Compile Include="..\Shared\ImageLoader.cs">
|
||||||
<Link>ImageLoader.cs</Link>
|
<Link>ImageLoader.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="..\Shared\Intersections.cs">
|
|
||||||
<Link>Intersections.cs</Link>
|
|
||||||
</Compile>
|
|
||||||
<Compile Include="..\Shared\Location.cs">
|
<Compile Include="..\Shared\Location.cs">
|
||||||
<Link>Location.cs</Link>
|
<Link>Location.cs</Link>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|
|
||||||
|
|
@ -60,9 +60,9 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
using (var context = ((StreamGeometry)Data).Open())
|
using (var context = ((StreamGeometry)Data).Open())
|
||||||
{
|
{
|
||||||
if (ParentMap != null && locations != null)
|
if (ParentMap != null && locations?.Count() >= 2)
|
||||||
{
|
{
|
||||||
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
|
var longitudeOffset = GetLongitudeOffset(Location ?? locations.First());
|
||||||
|
|
||||||
AddPolylinePoints(context, locations, longitudeOffset, closed);
|
AddPolylinePoints(context, locations, longitudeOffset, closed);
|
||||||
}
|
}
|
||||||
|
|
@ -77,7 +77,7 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
var longitudeOffset = GetLongitudeOffset(Location);
|
var longitudeOffset = GetLongitudeOffset(Location);
|
||||||
|
|
||||||
foreach (var polygon in polygons)
|
foreach (var polygon in polygons.Where(p => p?.Count() >= 2))
|
||||||
{
|
{
|
||||||
AddPolylinePoints(context, polygon, longitudeOffset, true);
|
AddPolylinePoints(context, polygon, longitudeOffset, true);
|
||||||
}
|
}
|
||||||
|
|
@ -86,8 +86,6 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddPolylinePoints(StreamGeometryContext context, IEnumerable<Location> locations, double longitudeOffset, bool closed)
|
private void AddPolylinePoints(StreamGeometryContext context, IEnumerable<Location> locations, double longitudeOffset, bool closed)
|
||||||
{
|
|
||||||
if (locations.Count() >= 2)
|
|
||||||
{
|
{
|
||||||
var points = locations
|
var points = locations
|
||||||
.Select(location => LocationToView(location, longitudeOffset))
|
.Select(location => LocationToView(location, longitudeOffset))
|
||||||
|
|
@ -99,4 +97,3 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Copyright © 2024 Clemens Fischer
|
// Copyright © 2024 Clemens Fischer
|
||||||
// Licensed under the Microsoft Public License (Ms-PL)
|
// Licensed under the Microsoft Public License (Ms-PL)
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
|
|
@ -61,17 +62,15 @@ namespace MapControl
|
||||||
var pathFigures = ((PathGeometry)Data).Figures;
|
var pathFigures = ((PathGeometry)Data).Figures;
|
||||||
pathFigures.Clear();
|
pathFigures.Clear();
|
||||||
|
|
||||||
if (ParentMap != null && locations != null)
|
if (ParentMap != null && locations?.Count() >= 2)
|
||||||
{
|
{
|
||||||
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
|
var longitudeOffset = GetLongitudeOffset(Location ?? locations.First());
|
||||||
|
|
||||||
AddPolylinePoints(pathFigures, locations, longitudeOffset, closed);
|
AddPolylinePoints(pathFigures, locations, longitudeOffset, closed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
|
private void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
|
||||||
{
|
|
||||||
if (locations.Count() >= 2)
|
|
||||||
{
|
{
|
||||||
var points = locations
|
var points = locations
|
||||||
.Select(location => LocationToView(location, longitudeOffset))
|
.Select(location => LocationToView(location, longitudeOffset))
|
||||||
|
|
@ -83,7 +82,7 @@ namespace MapControl
|
||||||
var figure = new PathFigure
|
var figure = new PathFigure
|
||||||
{
|
{
|
||||||
StartPoint = points.First(),
|
StartPoint = points.First(),
|
||||||
IsClosed = closed,
|
IsClosed = true,
|
||||||
IsFilled = true
|
IsFilled = true
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -99,22 +98,16 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var pointList = points.ToList();
|
|
||||||
|
|
||||||
if (closed)
|
|
||||||
{
|
|
||||||
pointList.Add(pointList[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
var viewport = new Rect(0, 0, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height);
|
|
||||||
PathFigure figure = null;
|
PathFigure figure = null;
|
||||||
PolyLineSegment polyline = null;
|
PolyLineSegment polyline = null;
|
||||||
|
var viewport = new Rect(0, 0, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height);
|
||||||
|
var pointList = points.ToList();
|
||||||
|
|
||||||
for (int i = 1; i < pointList.Count; i++)
|
for (int i = 1; i < pointList.Count; i++)
|
||||||
{
|
{
|
||||||
var p1 = pointList[i - 1];
|
var p1 = pointList[i - 1];
|
||||||
var p2 = pointList[i];
|
var p2 = pointList[i];
|
||||||
var inside = Intersections.GetIntersections(ref p1, ref p2, viewport);
|
var inside = GetIntersections(ref p1, ref p2, viewport);
|
||||||
|
|
||||||
if (inside)
|
if (inside)
|
||||||
{
|
{
|
||||||
|
|
@ -142,6 +135,94 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Calculates the potential intersections of a line segment given by (p1,p2) with a rectangle.
|
||||||
|
/// Updates either p1, p2, or both with any found intersection and returns a value that indicates
|
||||||
|
/// whether the segment intersects or lies inside the rectangle.
|
||||||
|
/// </summary>
|
||||||
|
private static bool GetIntersections(ref Point p1, ref Point p2, Rect rect)
|
||||||
|
{
|
||||||
|
if (rect.Contains(p1) && rect.Contains(p2))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var topLeft = new Point(rect.X, rect.Y);
|
||||||
|
var topRight = new Point(rect.X + rect.Width, rect.Y);
|
||||||
|
var bottomLeft = new Point(rect.X, rect.Y + rect.Height);
|
||||||
|
var bottomRight = new Point(rect.X + rect.Width, rect.Y + rect.Height);
|
||||||
|
var numIntersections = 0;
|
||||||
|
|
||||||
|
if (GetIntersection(ref p1, ref p2, topLeft, bottomLeft, p => p.X <= rect.X)) // left edge
|
||||||
|
{
|
||||||
|
numIntersections++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (GetIntersection(ref p1, ref p2, topLeft, topRight, p => p.Y <= rect.Y)) // top edge
|
||||||
|
{
|
||||||
|
numIntersections++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numIntersections < 2 &&
|
||||||
|
GetIntersection(ref p1, ref p2, topRight, bottomRight, p => p.X >= rect.X + rect.Width)) // right edge
|
||||||
|
{
|
||||||
|
numIntersections++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numIntersections < 2 &&
|
||||||
|
GetIntersection(ref p1, ref p2, bottomLeft, bottomRight, p => p.Y >= rect.Y + rect.Height)) // bottom edge
|
||||||
|
{
|
||||||
|
numIntersections++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return numIntersections > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool GetIntersection(ref Point p1, ref Point p2, Point p3, Point p4, Predicate<Point> predicate)
|
||||||
|
{
|
||||||
|
var intersection = GetIntersection(p1, p2, p3, p4);
|
||||||
|
|
||||||
|
if (!intersection.HasValue)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (predicate(p1))
|
||||||
|
{
|
||||||
|
p1 = intersection.Value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
p2 = intersection.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the intersection point of two line segments given by (p1,p2) and (p3,p4),
|
||||||
|
/// or null if no intersection exists. See https://stackoverflow.com/a/1968345.
|
||||||
|
/// </summary>
|
||||||
|
private static Point? GetIntersection(Point p1, Point p2, Point p3, Point p4)
|
||||||
|
{
|
||||||
|
var x12 = p2.X - p1.X;
|
||||||
|
var y12 = p2.Y - p1.Y;
|
||||||
|
var x34 = p4.X - p3.X;
|
||||||
|
var y34 = p4.Y - p3.Y;
|
||||||
|
var x13 = p3.X - p1.X;
|
||||||
|
var y13 = p3.Y - p1.Y;
|
||||||
|
|
||||||
|
var d = x12 * y34 - x34 * y12;
|
||||||
|
var s = (x13 * y12 - y13 * x12) / d;
|
||||||
|
var t = (x13 * y34 - y13 * x34) / d;
|
||||||
|
|
||||||
|
if (s >= 0d && s <= 1d && t >= 0d && t <= 1d)
|
||||||
|
{
|
||||||
|
return new Point(p1.X + t * x12, p1.Y + t * y12);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue