So I was coding something on a little side project of mine and realised I had a need to "extend a line". Usually I would email my old colleague and friend David Waters, but decided to try and work this one out for myself. I had a flick through my math book I mentioned above and didn't find a direct solution to my problem. However, I did have a good read through the section on Vectors.
Given I had two points and a distance I needed to extend one of those points by, I realised that I needed the following components:
- a vector (subtract point 1 from point 2)
- the magnitude of the vector (i.e. the distance between two points, either my starting points or the origin and the vector)
The code looks like this:
It appears to work in my test cases, so I'm happy with it, but of course I'd love to hear from the math wizards if it could be better or simpler!
/**
*
* @param p1
* the start of the line
* @param p2
* the end of the line to extend
* @param distance
* how far to extend the line by
* @return the line object
*/
private Line2D createAndExtendLine(Point2D p1, Point2D p2, int distance) {
double length = p1.distance(p2);
Point2D vec = new Point2D.Double(p2.getX() - p1.getX(), p2.getY()
- p1.getY());
double multiplier = (length + distance) / length;
Point2D p3 = new Point2D.Double((vec.getX() * multiplier) + p1.getX(),
(vec.getY() * multiplier) + p1.getY());
return new Line2D.Double(p1, p3);
}
0 comments:
Post a Comment