-
getting point on line at distance
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)?
Code:
x1_________x3____x2
y1 y3 y2
Cheers for you help in advance
Last edited by pepsicoder; 02-08-2009 at 04:51 PM.
-
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 );
}
Last edited by newblack; 02-09-2009 at 09:52 AM.
-
your the daddy
-
Viral tick
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 );
}
I do not vow to know everything, and I can not guarantee what I say is correct. We are all learning, and I actually thank you the one with the questions for asking and stimulating my mind.
www.lordofduct.com - check out my blog, or contact me through my website. I'm always available for freelance work in Flex3/Actionscript 3.
how many times must I say it... the livedocs are your friend!
http://livedocs.adobe.com/flash/9.0/...l-classes.html
-
oops, thanks, lordofduct! i'll edit the original post.
-
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|