将直线旋转30度

image

有时点A可能在线段BC之下,有时可能在线段BC之上 -> 所以有时需要顺时针旋转,有时需要逆时针旋转。 角ABC = 90度。

向量:

A{x,y}
B{x,y}
C{x,y}

已知

需要计算向量/直线

A'{x,y} / BA'

这里是45度的角平分线,但我不知道x和y(或者说全部都不对?出处:https://stackoverflow.com/a/6563044/9187461 - 但是这里是向量之间的角度而不是直线,我不知道):

local ux = A.x - B.x
local uy = A.y - B.y
local vx = C.x - B.x
local vy = C.y - B.y
local theta_u = math.atan2(ux, uy)
local theta_v = math.atan2(vx, vy)
local theta = (theta_u+theta_v)/2 --角平分线
theta = theta * math.pi / 180
local x = math.cos(theta) * (x2?-x1?) - math.sin(theta) * (y2?-y1?) + x1?
local y = math.sin(theta) * (x2?-x1?) + math.cos(theta) * (y2?-y1?) + y1?
点赞
用户8605791
用户8605791

在这种情况下,C是无关紧要的,你要做的是将向量BA以顺时针30度围绕B旋转。在这里看到如何操作,你不需要atan函数,因为它在数值精度方面不太好。

这是代码,输入点ab,返回旋转后的点a'

function rotate(a, b)
   local ba_x = a.x - b.x
   local ba_y = a.y - b.y
   local x = (math.sqrt(3) * ba_x + ba_y)/2
   local y = (-ba_x + math.sqrt(3) * ba_y)/2
   local ap = {}
   ap.x = b.x + x
   ap.y = b.y + y
   return ap
end

编辑

function cross(v1, v2)
    return v1.x*v2.y - v2.x*v1.y
end

function make_vec(a, b)
    local r = {}
    r.x = b.x - a.x
    r.y = b.y - a.y
    return r
end

function rotate(a, b, c)
   local ba_x = a.x - b.x
   local ba_y = a.y - b.y
   local x, y
   if cross(make_vec(b, c), make_vec(b, a)) > 0 then
       x = (math.sqrt(3) * ba_x + ba_y)/2
       y = (-ba_x + math.sqrt(3) * ba_y)/2
   else
       x = (math.sqrt(3) * ba_x - ba_y)/2
       y = (ba_x + math.sqrt(3) * ba_y)/2
   end

   local ap = {}
   ap.x = b.x + x
   ap.y = b.y + y
   return ap
end
2018-01-27 22:24:38