• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

custom shape rotation issue

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to rotate a custom shape around its center, but can not get the result as expected.

what i want is

*shape should be rotated around its center without moving itself.*

what my solution is currently doing is rotating
a whole shape around its center , by every rotation its changing its position.

I have multiple shapes so i have created a class to encapsulate a shape with its transform in following class




I am calculating angle of rotation using following method

public static double getAngle(Point2D origin, Point2D other) {

double dy = other.getY() - origin.getY();
double dx = other.getX() - origin.getX();
double angle;

if (dx == 0) {// special case
angle = dy >= 0 ? Math.PI / 2 : -Math.PI / 2;
} else {
angle = Math.atan(dy / dx);
if (dx < 0) // hemisphere correction
angle += Math.PI;
}
// all between 0 and 2PI
if (angle < 0) // between -PI/2 and 0
angle += 2 * Math.PI;
return angle;
}

in mouse press event of the canvas mouse listener

selectedShape.onMousePress(me, canvasBoundary, shoeViewer
.getShapeOperation());


i am just calling selected shape's onMousePress method

and in my mouse drag method of the canvas mouse listener , i am just calling the selected shape's onMouseDrag method which updates the rotation angle as you can see from the very first class

selectedShape.onMouseDrag(me, canvasBoundary, shoeViewer
.getShapeOperation());


and you can see the draw method of the individual shape , to draw the shape according to current transform , i am calling from paintComponent like

Iterator<Shoe> shoeIter = shoeShapeMap.values().iterator();

while (shoeIter.hasNext()) {

Shoe shoe = shoeIter.next();
shoe.draw(g2, firstTime);

}


where shoeShapeMap contains all of the custom shapes currently on the canvas.

is i am doing mistake in calculating angle or determining anchor point ? my current solution rotates shape 360 degree by checking all the conditions[90 degree etc.] as you can see in the above mentioned method.

i want the shape should be rotated around its center without resizing its positions ? in the word it is difficult to explain , so please suggest me any better way to show here what i want to accomplish ?

i think i have mentioned all the things related to this issue. if you have any doubts please feel free to ask me.

Thank You
Mihir Parekh
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic