Angle -> draw Line
Hi,
I can calculate the angle between 2 lines with the following formula:
private String calcAngle(Point2D p, Point2D lp){
radians = (Math.atan2(lp.getY() - p.getY(), lp.getX() - p.getX()) * 180) / Math.PI;
return formatter.format(radians);
}
Now I have to go the way vice versa: I have a line, I get a certain angle and have to draw a 2nd line using this angle; how can I do this?
Thanks a lot!
[563 byte] By [
SFLa] at [2007-11-27 4:58:09]

# 1
You can use [url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/AffineTransform.html]AffineTransform[/url] to rotate the line.
Here's an example how to rotate a line by 120 degrees:
Line2D line = new Line2D.Double(10, 10, 100, 100);
AffineTransform at = AffineTransform.getRotateInstance(Math.PI/3.0*2.0, line.getP2().getX(), line.getP2().getY());
Shape line2 = at.createTransformedShape(line);
Then just use Graphics2D.draw(Shape) function to draw the rotated line.
# 3
I already gave you an example.
You can use Math.toRadians instead of Math.PI/3.0*2.0:
Line2D line = new Line2D.Double(10, 10, 100, 100);
// x = degree between 0 and 360
AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(x), line.getP2().getX(), line.getP2().getY());
Shape line2 = at.createTransformedShape(line);
# 4
Assuming you know the position of one end of your line, and the length it should have, you could use the function movePoint() below to get the second end based on a given angle:
/**
* Computes a new point based on a starting position, distance and direction.
*
* @param startPoint
* @param distance
* @param angle -
* Angle 0 is three o'clock, angle 90 is 12 o'clock, angle 180 is 9
* o'clock and angle 270 is 6 o'clock.
*
* @return Point2D object holding the location one arrives at if moving from
* startPoint by the given distance, in the given angle.
*/
public static Point2D.Double movePoint(Point2D startPoint, double distance,
double angle) {
double newX = startPoint.getX() + circulDistX(distance, angle);
double newY = startPoint.getY() + circulDistY(distance, angle);
return new Point2D.Double(newX, newY);
}
/**
* Calculates the horizontal distance of a "vector". Used by movePoint.
*
* @param distance
* is the distance we want to move.
* @param angle -
* the angle we want to move in.
* @return how much the X coordinate needs to be updated in order to reach the
* new position.
*/
private static double circulDistX(double distance, double angle) {
return distance * Math.cos(angle * Math.PI / 180);
}
/**
* Calculates the vertical distance of a "vector" Used by movePoint.
*
* @param distance
* is the distance we want to move.
* @param angle -
* the angle we want to move in.
* @return how much the Y coordinate needs to be updated in order to reach the
* new position.
*/
private static double circulDistY(double distance, double angle) {
return -distance * Math.sin(angle * Math.PI / 180);
// the minus sign is because the coordinate system in java starts
// with 0,0 in the top left corner of the canvas rather than in
// the bottom right corner, which is usual on pen and paper drawings.
}