直线旋转 30 度

rotation of the line by 30 degrees

有时A可以在BC线下方,有时在BC线上方 -> 所以有时顺时针旋转,有时逆时针旋转。角 ABC = 90 度。

向量:

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

已知

需要计算向量/线

A'{x,y} / BA'

这里是 45 度的平分线,但我知道 x 和 y 怎么样(或者也许一切都不好?来源: - 但向量之间有角度而不是线 idk):

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 --bisector
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?

所以在这种情况下,C 是无关紧要的,您想要的是将向量 BAB 顺时针旋转 30 度。请参阅 here 如何操作,您不需要 atan 函数,它在数值精度方面很糟糕。

这里是代码,输入点ab,return旋转后的点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