线旋转30度

时间:2018-01-27 19:40:30

标签: vector lua rotation line

image

有时A可以低于BC线,有时高于-gt;所以有时顺时针旋转,有时逆时针旋转。角度ABC = 90度。

载体:

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

已知

需要计算矢量/行

A'{x,y} / BA'

这里是45度的平分线但是idk x和y怎么样(或者可能一切都很糟糕?来源:https://stackoverflow.com/a/6563044/9187461 - 但是矢量之间的角度不是线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?

1 个答案:

答案 0 :(得分:0)

所以在这种情况下,C无关紧要,你想要的是将BA顺时针旋转B 30度。请参阅here如何操作,您不需要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