Calculate a vector from the center of a square to edge based on radius

前端 未结 5 1912
Happy的楠姐
Happy的楠姐 2021-01-06 12:58

Given a square (described by x, y, width, height) and an angle (in radians) I need to calculate a vector that originates at the squares centre and terminates at the point th

5条回答
  •  醉话见心
    2021-01-06 13:50

    Generalized to rectangles, if a = the angle of vector from the horizontal increasing counter cloclkwise, then the points coordinates can be calculated by the following:

    let dx = distance from center horizontally, and 
        dy = distance form the center vertically, then 
    
     dx =  if (tan(a) == 0, then width/2, else Min( height / (2 * tan(a)), width/2)
     dy =  if ABS(a) == Pi/2 then height/2 else  Min( (width/2) * tan(a)),  height/2)
    

    Then coordinates of the point are:

       px = (x+width/2) + dx for right quadrants (Pi/2 >= a >= - Pi/2);
          = (x+width/2) - dx for left quadrants  (Pi/2 <= a <= 3Pi/2)
       py = (y+height/2) + dy for lower quadrants (Pi <= a <= 2Pi);
          = (y+height/2) - dy for upper quadrants (0 <= a <= Pi);
    

提交回复
热议问题