Bubbles3/Controls/BblImageSurface.cs
/// <summary>
/// Rotate the image around the client center with the mouse cursor.
/// </summary>
/// <param name="x">mouse x</param>
/// <param name="y">mouse y</param>
/// <param name="init">set to true to initialize a new rotation</param>
Rotate the image around the client center with the mouse cursor.
Rotate the image around the client center with the mouse cursor .
public void FreeRotation(float x, float y, bool init = false)
        {
            Vector2 mouse = new Vector2(x, y) - ClientCenter;
            Vector3 rotcursor = new Vector3(mouse.X, mouse.Y, 0);
            rotcursor.Normalize();
            if (init)
            {
                _freeRotInitClick = rotcursor;
                _freeRotInitAngle = (float)-Math.Atan2((double)_rotation.M21, (double)_rotation.M11);
                Matrix3x2 t = _rotation * _scale * _translation;
                t.Invert();
                _freeRotCenter = Matrix3x2.TransformPoint(t, ClientCenter);
                Vector2 sc = Matrix3x2.TransformPoint(_scale, _freeRotCenter);
                _translation = Matrix3x2.Translation(ClientCenter - sc);
            }
            float dot = Vector3.Dot(rotcursor, _freeRotInitClick);
            float angle = (float)(Math.Acos(dot));
            if (Single.IsNaN(angle)) angle = 0;
            Vector3 cross = Vector3.Cross(rotcursor, _freeRotInitClick);
            if (cross.Z > 0) angle = (float)(2 * Math.PI) - angle;
            angle += _freeRotInitAngle;
            _rotation = Matrix3x2.Rotation(angle, _freeRotCenter);
            _freerot = true;
            Invalidate();
        }
public void FreeRotation ( float x , float y , bool init = false ) { Vector2 mouse = new Vector2 ( x , y ) - ClientCenter ; Vector3 rotcursor = new Vector3 ( mouse . X , mouse . Y , 0 ) ; rotcursor . Normalize ( ) ; if ( init ) { _freeRotInitClick = rotcursor ; _freeRotInitAngle = ( float ) - Math . Atan2 ( ( double ) _rotation . M21 , ( double ) _rotation . M11 ) ; Matrix3x2 t = _rotation * _scale * _translation ; t . Invert ( ) ; _freeRotCenter = Matrix3x2 . TransformPoint ( t , ClientCenter ) ; Vector2 sc = Matrix3x2 . TransformPoint ( _scale , _freeRotCenter ) ; _translation = Matrix3x2 . Translation ( ClientCenter - sc ) ; } float dot = Vector3 . Dot ( rotcursor , _freeRotInitClick ) ; float angle = ( float ) ( Math . Acos ( dot ) ) ; if ( Single . IsNaN ( angle ) ) angle = 0 ; Vector3 cross = Vector3 . Cross ( rotcursor , _freeRotInitClick ) ; if ( cross . Z > 0 ) angle = ( float ) ( 2 * Math . PI ) - angle ; angle += _freeRotInitAngle ; _rotation = Matrix3x2 . Rotation ( angle , _freeRotCenter ) ; _freerot = true ; Invalidate ( ) ; }
Rotate the image around the client center with the mouse cursor.
Rotate the image around the client center with the mouse cursor .
//get click vector and initial rotation //get center of rotation //adapt translation so that the center of rotation coincides with the client center //get angle between current mouse pos and reference vector //add initial rotation to it //apply rotation
x
float
None
y
float
None
init
bool
set to true to initialize a new rotation
set to true to initialize a new rotation