?
Solved

Calculating corner points for rotated rectangle

Posted on 2003-03-11
5
Medium Priority
?
573 Views
Last Modified: 2008-03-06
I have a Rectangle object and I am trying to calculate where its four corner points would be if I were to rotate it counter-clockwise 45 degrees around its center.  Any ideas on an easy way to do this?
0
Comment
Question by:moshecristel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Accepted Solution

by:
Rezantis earned 500 total points
ID: 8117669
Try the AffineTransform class, like so:

Polygon newShape = AffineTransform.getRotateInstance(angle, xCentre, yCentre).createTransformedShape(yourRectangle);

angle is the angle that you want to rotate the shape through, xCentre and yCentre are the x and y coordinates of the centre of your rectangle.

This will return you a new shape that will be your rectangle rotated through whatever angle you input.  For counterclockwise 45 degrees, you want (-Math.PI/4); at least I think so.

For more info, check the Javadocs:

http://java.sun.com/j2se/1.4.1/docs/api/java/awt/geom/AffineTransform.html
0
 

Author Comment

by:moshecristel
ID: 8119969
This seems like a very good idea but I can't seem to quite get it to work.  AffineTransform's createTransformedShape method doesn't return a Polygon but a GeneralPath and I haven't yet figured out how to get the corner points out of that.  Any ideas?
0
 

Author Comment

by:moshecristel
ID: 8120090
This seems like a very good idea but I can't seem to quite get it to work.  AffineTransform's createTransformedShape method doesn't return a Polygon but a GeneralPath and I haven't yet figured out how to get the corner points out of that.  Any ideas?
0
 

Author Comment

by:moshecristel
ID: 8129338
I was finally able to use the PathIterator to get the corner points.  Thanks for the help.
0
 

Expert Comment

by:Rezantis
ID: 8130625
Glad to help.  Sorry I didn't get back to you on the PathIterator bit; I was going to when I got home, then fell asleep when I got back from work.  ^^;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question