Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 586809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:08:44+00:00 2026-05-13T15:08:44+00:00

What is the logic that goes behind rotating an image via mouse movement. I

  • 0

What is the logic that goes behind rotating an image via mouse movement. I know how to rotate using graphics2d.rotate…but having difficulty doing it with the mouse as the source for rotation. Here is basic steps:

  1. get mouse x(dx) and mouse y(dy) distances from anchoring point( in this case that would be
    the center of the image we want to rotate).
  2. use this point in Math.arcTan2(dy,dx) to obtain the angle or rotation.
  3. use the value from step to for Graphics2D.rotate method.

With that strategy, everytime i rotate the image, the image starts rotating from -pi and after 90 degrees of rotation it go goes back to -pi. I don’t understand what I’m doing wrong here, it should be pretty basic.

Here is a part of the code :

// mouse dragged events get sent here.
public void mouseDragged( MouseEvent e ) {
    int mx = e.getX( ), my = e.getY( );
    // just checking if it falls within bounds of the image we 
    // want to rotate.
    if( mx > speedKX || mx < speedKX + speedK.getWidth( ) || my > speedKY || my < speedKY + speedK.getHeight( )/2 )
    {
        theta += getTheta( e.getX( ), e.getY( ) ); 
    }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:08:44+00:00Added an answer on May 13, 2026 at 3:08 pm

    As I understand you should look for the initial angle (when you clicked, the line between the anchor and your click) and the current angle (when you are dragging, same line). That angle (independent from the current distance to anchor point) will give you the rotation.

    So you have to:

    rotate(anglenow - angle0)
    

    How to find it:

    In both cases (initial click and mouse move event) you have to find angle between anchor and mouse point thinking the anchor as the origin.

    I would use a method (getAngle(x1,y1,x2,y2). That method (except for race conditions like same x or same y, easily detectable) should calculate arctan(dy/dx).

    Sign

    But when you are dividing dy/dx it can be:

    + / + -> +
    + / - -> -
    - / + -> -
    - / - -> +
    

    It is, four posibilities give you two kind of results. So, you have to look some condition to detect them.

    I should review arctan doc or source to see what values it gives (between 0 and pi, or -pi/2 and +pi/2) and check then sign of the dx or the dy (depending on what range arctan returns) and use it to add/decrement pi to then resulting angle.

    Then you’ll get a getAngle method that return correctly the 360º space.

    Edit

    Javadoc says:

    Math.atan retuns the arc tangent of an angle, in the range of -pi/2 through pi/2.

    So, assuming your angle of value 0 is for the X axis as I assumed, the range it returns is the right hemisphere. So you have to distiguish the right hemisphere from the left one.

    If you calculate dx = xtarget – xorigin (as you did for the division) it will be positive if the correct hemisphere is the right one, and negative if it’s not.

    So if dy < 0 then you have to add pi to your resulting angle. It will be between -pi/2 and 3pi/2. You also can correct the result by passing all to (-pi,pi) range or (0,2pi) range.

    Edit: pseudocode, please double check!

    onmousedown {
        startpoint = (x,y);
        startangle = getAngle(origin, startpoint);
    }
    
    onmousemove {
        currentpoint = (x,y);
        currentangle = getAngle(origin, currentpoint);
        originalimage.rotate(currentangle - startangle);
    }
    
    getAngle(origin, other) {
        dy = other.y - origin.y;
        dx = other.x - origin.x;
        if (dx == 0) // special case
            angle = dy >= 0? PI/2: -PI/2;
        else
        {
            angle = Math.atan(dy/dx);
            if (dx < 0) // hemisphere correction
                angle += PI;
        }
        // all between 0 and 2PI
        if (angle < 0) // between -PI/2 and 0
            angle += 2*PI;
        return angle;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using Linq To SQL with our own data context logic that executes
I am using PHPDoc and JSDoc for my source code. I know that there
I want to display an animated arrow that goes back and forth (using flex
It is possible that my question title is misleading, but here goes -- I
My model will have logic that uses Date.current like this: if(date_three && date_three !=
I have authentication logic that uses cookies. The model attributes are initialized from cookies,
I have some custom logic that needs to be executed every single time a
I have some logic that depends upon two properties being set, as it executes
Assuming some logic that deals with measure units and the user is asked to
I'm trying to build a control logic that depends on the current gsp page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.