# How to extract the angle of rotation of a 2x2 matrix?

This topic is 5019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello guys! I have two unsolved questions. A) How can I test if an axis aligned recteangle and a circle intersect? My approch returns false if the circle is inside the recteangle. C#-Code.( Generic, so I have to use functions( calculator ) for numeric operations.
        #region Intersects( Circle<Type, Calc> circle, Vector2<Type, Calc> recPosition )

/// <summary> Tests if the given circle intersects the rec. </summary>
/// <param name="circle"></param>
/// <param name="recPosition"> Position of the rec. </param>
/// <returns> True if they intersect, else false. </returns>
public bool Intersects( Circle<Type, Calc> circle, Vector2<Type, Calc> recPosition )
{
Type x0 = c.Sub( recPosition.x, circle.Position.x );
x0 = c.Mul( x0, x0 );

Type x1 = c.Add( recPosition.x, c.Sub( Width, circle.Position.x ) );
x1 = c.Mul( x1, x1 );

Type y0 = c.Sub( recPosition.y, circle.Position.y );
y0 = c.Mul( y0, y0 );

Type y1 = c.Add( recPosition.y, c.Sub( Height, circle.Position.y ) );
y1 = c.Mul( y1, y1 );

return true;

return true;

return true;

return true;

return false;
}

#endregion Intersects( Circle<Type, Calc> circle, Vector2<Type, Calc> recPosition )


B) How can I calculate the angle to rotate an object from the identity matrix to the object's orientation? ( 2x2 matrix ) Is this correct:
        #region Angle

/// <summary> Returns the angle to the Idendity-Matrix. </summary>
/// <returns></returns>
public Type Angle
{
get
{
return calc.Acos( calc.Div( m00, calc.Sqrt(
calc.Add( calc.Mul( m00, m00 ), calc.Mul( m01, m01 ) ))));
}
}

#endregion Angle


Thank you very much! :) [Edited by - Riddle on January 19, 2005 1:00:40 PM]

##### Share on other sites
Question A:

This has been discussed recently, along with code and an explanation of the algorithm. But with search down I'm not sure how you'd find it. So:

bool IntersectCircleAABB(const Vector2& C, float r, const Vector2& min, const Vector2& max){    float dist = 0.0f;        for (int i = 0; i < 2; i++)    {        if (C < min)            dist += (min - C) * (min - C);        else if (C > max)            dist += (C - max) * (C - max);    }        return dist <= r * r;}

That's off the top of my head, so it's not guaranteed to be error-free.

Question B:

This question probably has an easy answer, but looking at your code I'm not sure what your terms mean. Do you mean that given a 2x2 rotation matrix in 2D, you want to extract the angle of rotation?

##### Share on other sites
A:
Thank you very much! Works wonderfully! :)

B:
Yes, the amount I have to rotate an object ( Eg. with glRotate() ) from the original Data( Identity-Matrix ) to the rotated Matrix of the Object. ( 2x2 Matricen )

Thank you very much!

##### Share on other sites
Rotation matrix for conterclockwise rotation(y to up, x to right) is
a b
c d
where
a=cos(angle),b=-sin(angle)
c=sin(angle),d=cos(angle)

So you can get angle using just
atan2(c,a)

You might need to flip sign if you want clockwise rotation.

##### Share on other sites
Thank you. :)

My version and your version return exactly the same but different signs. Your version seems to be faster! :)

Everyting works fine now. I have solved all other problems regarding matricen.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 12
• 18
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632929
• Total Posts
3009283
• ### Who's Online (See full list)

There are no registered users currently online

×