0

image

Sometimes A can be below line BC and sometimes above -> so sometimes rotate clockwise and sometimes counterclockwise. Angle ABC = 90 degrees.

vectors:

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

are known

need calculate vector / line

A'{x,y} / BA'

here is bisector by 45degrees but idk what about x and y (or maybe all is bad? source: https://stackoverflow.com/a/6563044/9187461 - but there is angle between vectors not lines 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?
laalto
  • 150,114
  • 66
  • 286
  • 303

1 Answers1

0

So in this case, C is irrelevant, what you want is rotating vector BA around B clockwise by 30 degrees. See here how to do, you don't need atan function, it's bad in terms of numerical precision.

Here is the code, input point a and b, return the rotated point 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

EDIT

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
llllllllll
  • 16,169
  • 4
  • 31
  • 54