A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: getting point on line at distance

  1. #1
    Junior Member
    Join Date
    Sep 2007
    Location
    kent, uk
    Posts
    20

    Lightbulb 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.

  2. #2
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    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.

  3. #3
    Junior Member
    Join Date
    Sep 2007
    Location
    kent, uk
    Posts
    20

    resolved your the daddy

    cheers m8.

  4. #4
    Viral tick lordofduct's Avatar
    Join Date
    May 2008
    Location
    South Florida
    Posts
    159
    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

  5. #5
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    oops, thanks, lordofduct! i'll edit the original post.

  6. #6
    Junior Member
    Join Date
    Sep 2007
    Location
    kent, uk
    Posts
    20

    resolved Thank you

    Cheers to all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center