Skip to main content
saratogacoach
Inspiring
June 25, 2013
Answered

Getting Line Length

  • June 25, 2013
  • 1 reply
  • 1100 views

Hi,

I need to be able to draw lines dynamically between several sets of 2 dragged/dropped points on stage.

If I draw a line dynamically between a set of 2 points (LineA), using graphics class, moveTo(point1.x, point1.y), lineTo (point2.x, Point2.y), and I want to then get LineA's length (length of the newly drawn line), plus LineA.x, LineA.y, would I simply use LineA.width, LineA.x, LineA.y?

I'm not sure how else to do this?

Any suggestions appreciated.

saratogacoach

This topic has been closed for replies.
Correct answer kglad

One clarification:

The dots being dragged/dropped are movieclips that I am creating with a center registration point. Should I instead create these with top left registration. My goal is to get an accurate measure of the distance between the centers of the 2 dots (line length drawn from center to center). So, I figured that center registration would be useful. But, if your above formula depends on a different registration point then I would need to change this.


if you want the distance between the centers, use the function in my last message and leave the dots' reg points in their centers.

1 reply

kglad
Community Expert
Community Expert
June 25, 2013

use distanceF():

function distanceF(pt1:Point,pt2:Point):Number{

return Math.sqrt((pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y));

}

saratogacoach
Inspiring
June 25, 2013

I think I didn't explain fully. While I used the term "points" what I meant was "dots." The user drags and drops two 6X6 dots on the stage, then dynamically draws a line between these 2 dots.

So a set of 2 dots (the beginning and end for the dynamically drawn line between them). Then need to get the length of the drawn line, maybe also its x, y.

Would I still use distanceF() ? Or some other way?