I have two points and I need to create a line that is perpendicular to the line they form. Also the intersection has to be 5 (units) away from the first point. I know how to get a perpendicular line but not how to get the point on the first line that is 5 units away from the first point.
views:
49answers:
2
A:
use the equation of a circle centered on the first point, and solve for x and y.
first point = x_0, y_0
equation of a circle of radius 5 around first point
(x - x_0)^2 + (y - y_0)^2 = 25
use the equation of the line to replace y, and solve for x. Careful that you get 2 points, pick the right one. The use the equation of the line again to solve for y.
Marco
2010-07-31 05:43:02
+2
A:
public static function distanceFromPoint(a:Point, b:Point, dist:Number):Point {
var tmp:Point = b.subtract(a);
tmp.normalize(dist);
return a.add(tmp);
}
How this works:
You subtract a
from b
to get the vector between the two points. You normalize this vector and multiply it by dist
to get a line dist
units long pointing in the direction from a
to b
. Add this vector to point a
and the result will be a new point that is dist
units from a
in the direction of b
.
Gunslinger47
2010-07-31 06:09:04
+1. A minor note. You should use a temporary variable to store the normalized point, since `normalize` returns `void`. Something like this: `public static function distanceFromPoint(a:Point, b:Point, dist:Number):Point { var tmp:Point = b.subtract(a); tmp.normalize(dist); return a.add(tmp);}`
Juan Pablo Califano
2010-07-31 23:41:56
Thanks @Juan. I modded up [one of your underrated answers](http://stackoverflow.com/questions/3217212/key-value-pairs-in-as3/3217584#3217584) in return.
Gunslinger47
2010-08-01 00:21:28