我试图用一个点的中心来计算范围
我尝试使用边界范围方法,但它不起作用。计算的范围是单个点,而不是真正的范围。
我该怎么做?
谢谢:)
如果您想要一个以点[x, y]
为中心的范围,但也要包含所有其他要素的范围[x1, y1, x2, y2]
,我认为您需要计算它
var halfWidth = Math.max(Math.abs(x - x1), Math.abs(x - x2));
var halfHeight = Math.max(Math.abs(y - y1), Math.abs(y - y2));
var extent = [x - halfWidth, y - halfHeight, x + halfWidth, y + halfHeight];
我想你会从我用 C# 实现的源代码中得到一个想法。
public static List<Coordinate> GetExtentFromCenterCoordinate(Coordinate center, double distance_km)
{
double hypotenuse_each_quardilator = Math.Sqrt(Math.Pow((distance_km / 2), 2) + Math.Pow((distance_km / 2), 2)); //Hy = Sqrt(L2 + h2)
double rad_angle = (Math.PI / 180) * 45;
double distance = hypotenuse_each_quardilator / 100;
//left-top
double x1 = center.X - distance * Math.Cos(rad_angle);
double y1 = center.Y + distance * Math.Sin(rad_angle);
//right-top
double x2 = center.X + distance * Math.Cos(rad_angle);
double y2 = center.Y + distance * Math.Sin(rad_angle);
//right-bottom
double x3 = center.X + distance * Math.Cos(rad_angle);
double y3 = center.Y - distance * Math.Sin(rad_angle);
//left-bottom
double x4 = center.X - distance * Math.Cos(rad_angle);
double y4 = center.Y - distance * Math.Sin(rad_angle);
List<Coordinate> extent_area = new List<Coordinate>();
extent_area.Add(new Coordinate(x1, y1));//left-top
extent_area.Add(new Coordinate(x2, y2));//right-top
extent_area.Add(new Coordinate(x3, y3));//right-bottom
extent_area.Add(new Coordinate(x4, y4));//left-bottom
return extent_area;
}