【发布时间】:2018-08-24 21:44:46
【问题描述】:
我需要围绕它的中心旋转矩形,矩形是地图套件上的多边形,我有 4 个点和中心。我分别旋转每个点,然后创建新的多边形。
我使用这个代码:
if let rotation = rotation, let center = zoneCenter {
let radians = Double(rotation) * (Double.pi/180.0)
print(radians)
var newPoints: [CLLocationCoordinate2D] = []
for point in squarePoints {
let latitude: CLLocationDegrees = center.latitude + (point.longitude - center.longitude) * sin(radians) + (point.latitude - center.latitude) * cos(radians)
let longitude: CLLocationDegrees = center.longitude + (point.longitude - center.longitude) * cos(radians) - (point.latitude - center.latitude) * sin(radians)
newPoints.append(CLLocationCoordinate2DMake(latitude, longitude))
}
squarePointsWithRotation = newPoints
squareOverlay = MKPolygon(coordinates: &newPoints, count: squarePoints.count)
mapView.add(squareOverlay)
}
}
如您所见,矩形变成了菱形,并且角度不是必须的 90 度。无法弄清楚如何使所有角度保持 90 度。 我使用这个公式进行旋转
/// X = x0 + (x - x0) * cos(a) - (y - y0) * sin(a);
/// Y = y0 + (y - y0) * cos(a) + (x - x0) * sin(a);
/// where x0, y0 - center, a - rotation angle, x, y - point to rotate`
希望得到帮助!
【问题讨论】:
标签: ios geometry mapkit rectangles