I’m trying to create an Illustrator style selection box for geometric objects in java.
When the object is selected a border is drawn and it’s possible to drag the little rectangles to re-size the object. I’d also like to be able to rotate the box by dragging.
So far I can scale the box and I can rotate the box but I can’t do the two together. Imagine the box is at an angle of 45 degrees. When you drag the corner to enlarge the box in the x direction this will increase both the width and height of the box because of the angle.
I can get it to work by using:
dx = dx*cos(theta) - dy*sin(theta);
dy = dy*cos(theta) + dx*sin(theta);
But this only works when the pivot point is in the top left corner. I want to be able to move the pivot around and then scale and rotate. This problem must have been solved lots of times before. Is there a way I can use an affine transform to convert my mouse draw to the coordinate space of the rotated object? I’d prefer not to have to dig through the trigonometry! Thanks in advance.
I pretty much worked the answer out myself although it’s still not possible to move the pivot point around. In case it’s helpful here’s the full code for a working example using JavaFX 2.2. You can scale and rotate the box by dragging the corners around:
}