Calculate rotated rectangle size from known bounding box coordinates

前端 未结 2 933
北海茫月
北海茫月 2020-11-30 23:28

I read the Calculate Bounding box coordinates from a rotated rectangle to know how to calculate bounding box coordinates from a rotated rectangle. But in a special case as f

相关标签:
2条回答
  • 2020-11-30 23:34

    enter image description here

    Solution

    Given bounding box dimensions bx by by and t being the anticlockwise rotation of rectangle sized x by y:

    x = (1/(cos(t)^2-sin(t)^2)) * (  bx * cos(t) - by * sin(t))
    y = (1/(cos(t)^2-sin(t)^2)) * (- bx * sin(t) + by * cos(t))
    

    Derivation

    Why is this?

    First, consider that the length bx is cut in two pieces, a and b, by the corner of the rectangle. Use trigonometry to express bx in terms of x, y, and theta:

    bx = b          + a
    bx = x * cos(t) + y * sin(t)            [1]
    

    and similarly for by:

    by = c          + d
    by = x * sin(t) + y * cos(t)            [2]
    

    1 and 2 can be expressed in matrix form as:

    [ bx ] = [ cos(t)  sin(t) ] * [ x ]     [3]
    [ by ]   [ sin(t)  cos(t) ]   [ y ]
    

    Note that the matrix is nearly a rotation matrix (but not quite - it's off by a minus sign.)

    Left-divide the matrix on both sides, giving:

    [ x ] = inverse ( [ cos(t)  sin(t) ]    * [ bx ]                        [4]
    [ y ]             [ sin(t)  cos(t) ] )    [ by ]
    

    The matrix inverse is easy to evaluate for a 2x2 matrix and expands to:

    [ x ] = (1/(cos(t)^2-sin(t)^2)) * [ cos(t) -sin(t) ] * [ bx ]           [5]
    [ y ]                             [-sin(t)  cos(t) ]   [ by ]
    

    [5] gives the two formulas:

    x = (1/(cos(t)^2-sin(t)^2)) * (  bx * cos(t) - by * sin(t))             [6]
    y = (1/(cos(t)^2-sin(t)^2)) * (- bx * sin(t) + by * cos(t))
    

    Easy as pie!

    0 讨论(0)
  • 2020-11-30 23:41

    You'll probably need something like affine transformation to discover point coordinates. And then using standard geometry formulas calculate the size.

    0 讨论(0)
提交回复
热议问题