Does some one know of a fast method of finding point x3,y3 when we know points x1,y1 x2,y2 and distance (heading from x1,y1 towards x2,y2)?
Cheers for you help in advance :)Code:x1_________x3____x2
y1 y3 y2
Printable View
Does some one know of a fast method of finding point x3,y3 when we know points x1,y1 x2,y2 and distance (heading from x1,y1 towards x2,y2)?
Cheers for you help in advance :)Code:x1_________x3____x2
y1 y3 y2
you need to define a vector pointing from (x1,y1) to (x2,y2), normalize it and scale it by the distance:
Code:function getPointAtInterval( p1:Point, p2:Point, t:Number ) : Point
{
var x3:Number = p2.x - p1.x;
var y3:Number = p2.y - p1.y;
var length:Number = Math.sqrt( x3 * x3 + y3 * y3 );
x3 /= length;
y3 /= length;
x3 *= t;
y3 *= t;
return new Point( p1.x + x3, p1.y + y3 );
}
cheers m8.
you missed a line of code there:
Code:function getPointAtDistance( p1:Point, p2:Point, dis:Number ) : Point
{
//get vector
var x3:Number = p2.x - p1.x;
var y3:Number = p2.y - p1.y;
//normalize vector
var length:Number = Math.sqrt( x3 * x3 + y3 * y3 );
x3 /= length;
y3 /= length;
//scale vector
x3 *= dis;
y3 *= dis;
//add vector back onto initial point and return
return new Point( p1.x + x3, p1.y + y3 );
}
oops, thanks, lordofduct! i'll edit the original post.
Cheers to all.