代码之家  ›  专栏  ›  技术社区  ›  user9187461

管线旋转30度

  •  0
  • user9187461  · 技术社区  · 7 年前

    image

    有时A可以低于BC线,有时高于->所以有时顺时针旋转,有时逆时针旋转。角度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 回复  |  直到 6 年前
        1
  •  0
  •   llllllllll    7 年前

    所以在这种情况下, C 不相关,你想要的是旋转向量 BA 围绕 B 顺时针旋转30度。看见 here 怎么做,你不需要 atan 函数,它的数值精度很差。

    这是代码,输入点 a b ,返回旋转的点 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