假设我们有一个100x100的坐标系统,如下图所示。0,0是其左上角,50,50是其中心点,100,100是其右下角,以此类推。
现在我们需要从中心向外画一条线。我们知道线的角度,但需要计算其终点的坐标。你认为最好的方法是什么?
例如,如果线的角度为45度,则其终点坐标大约为75,15。
(下方为图片,请见谅)
现在我们需要从中心向外画一条线。我们知道线的角度,但需要计算其终点的坐标。你认为最好的方法是什么?
例如,如果线的角度为45度,则其终点坐标大约为75,15。
(下方为图片,请见谅)
你需要使用三角函数 sin
和 cos
。
类似这样:
theta = 45
// theta = pi * theta / 180 // convert to radians.
radius = 50
centerX = 50
centerY = 50
p.x = centerX + radius * cos(theta)
p.y = centerY - radius * sin(theta)
请记住,大多数实现都假定您使用弧度并且正y朝上。
centerY + radius * sin(theta)
而不是 centerY - radius * sin(theta)
。 - thinklinuxX = 50 + (cos(45) * 50) ~ 85,36
Y = 50 - (sin(45) * 50) ~ 14,65
pi * x / 180
或x * (pi/180)
。 - Wouter Simonscos(45) = x ~= .707
sin(45) = y ~= .707
.707*50 = 35.35
35.35+50 = 85.35
abs(35.35-50) = 14.65