1

I have several shapes that I want to rotate and keep the same distance between them. What's the best way to do that ? I was thinking to calculate the center of the rectangle that wraps the selected shapes and rotate the shaped compared to that point

This is my code

    while(iter.hasNext()  ){
        shape = (Shape)iter.next(); 
        anchor = getCenter();
        AffineTransform t =  shape.getAffineTransform();    
        t.rotate(Math.toRadians(thetaDegrees), anchor.x, anchor.y);         
        shape.setAffineTransform(t);

    }

Thanks

Momo
  • 2,471
  • 5
  • 31
  • 52

4 Answers4

1

In principle, rotation always is a distance-preserving operation (a isometry, in mathematical terms, like a translation, and unlike scaling and shearing operations). So, if you rotate all your shapes around any point (by the same angle), it always gives you the desired property.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
1

I coincide with the two previous answers, rotation should not change anything in a shape.

The caveat would be that you need to rotate ONE SHAPE, and you rotate it from the origin. So, each vertex in the shapes is measured against a common reference frame.

For example, if you have two squares A and B, both of 10 units of length. One mistake would be to create two Shapes with points (0, 0) (0, 10) (10, 10) (10, 0). If, for example, square B is over A, then you have Shape A as before, but Shape B is (0, 10) (10, 10) (20, 10), (10, 10)). Now you can rotate both A and B and they will keep their relative position when rotated (if I recall correctly, rotation is usually taking as reference (0,0))

SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

As long as you rotate every object around the same point (and by the same angle, of course), you can pick any point you want as the center.

Common choices are the center of the union of the bounding boxes, the average of the individual object centers, the area-weighted average of the centers, and so on. You can even rotate around an arbitrary point outside the group of objects. It depends more on what you want to achieve than anything else.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
0

You would need to keep an initial state of the objects (i.e. position), apply that saved position for rotation then check if the objects are within bounds. If they are outside the bounds then you need to translate them toward the center of the bounding box with the appropriate distance.

Unless you have circles that rotate around their center the rotation will always modify the bounding box (growing or shrinking it). That's why you would need to keep the original reference at all times and transform from that.

brainwash
  • 690
  • 5
  • 19