我正在使用 Dundas 地图,需要用描绘一些数据的气泡覆盖地图。我想向地图添加形状以实现此目的。我可以像这样添加一个三角形(或任何直线多边形):
public static void AddShape(this MapControl map, List<MapPoint> points, Color color, string name)
{
if (points[0].X != points[points.Count - 1].X && points[0].Y != points[points.Count - 1].Y)
points.Add(points[0]);
var shape = new Shape
{
Name = name,
BorderColor = color,
BorderStyle = MapDashStyle.Solid,
BorderWidth = 1,
Color = Color.FromArgb((int)(255 * (0.3)), color)
};
var segments = new[] {new ShapeSegment {Type = SegmentType.Polygon, Length = points.Count}};
shape.AddSegments(points.ToArray(), segments);
map.Shapes.Add(shape);
}
public static void AddBermudaTriangle(this MapControl map)
{
var points = new List<MapPoint>
{
new MapPoint(-80.15, 26.0667),
new MapPoint(-64.75, 32.333),
new MapPoint(-66.07, 18.41)
};
map.AddShape(points, Color.Red, "Bermuda Triangle");
}
您可以看到百慕大三角以红色覆盖了地图。现在我想计算一组点以传递给我的 AddShape 方法,该方法将绘制一个椭圆或圆。我只需要一个简单的算法来计算给定点数的 x 和 y 坐标。也许从代表圆心的给定点开始。例如:
public static void AddCircle(this MapControl map, Point centre, double radius, string name)
{
var points = new List<MapPoint>();
const int n = 360;
for(var i = 0; i < n; i++)
{
//calculate x & y using n, radius and centre
double x = 0;
double y = 0;
points.Add(new MapPoint(x, y));
}
map.AddShape(points, Color.Red, name);
}
我知道 x,y 计算是简单的三角函数,但我正在遭受大脑冻结。帮助!
编辑(使用 tur!ng 的代码解决):
public static void AddCircle(this MapControl map, Color color, MapPoint centre, double radius, string name)
{
var points = new List<MapPoint>();
const int n = 360;
for(var i = 0; i < n; i++)
{
var x = (radius * Math.Cos(i * Math.PI / 180)) + centre.X;
var y = (radius * Math.Sin(i * Math.PI / 180)) + centre.Y;
points.Add(new MapPoint(x, y));
}
map.AddShape(points, color, name);
}
由于地图投影在 Robinson 网格上,蓝色圆圈(格林威治上空)变形。