0
votes

demo.jpg_explanation demo.jpgI have a random serial points array like:

var arr:Array=[[115,195],[142,161],[177,132],[217,105],[258,122],[304,128],[361,119],[395,98],[427,65],[467,67],[501,93],[536,136],...]

Just click mouse on stage and push the mouseX,mouseY into the arr to got it.

I get the length of them by:

var _length:Number=0;
for(var i:int=1;i<arr.length;i++){
            var p1:Point =new Point(arr[i-1].x,arr[i-1].y);
            var p2:Point =new Point(arr[i].x,arr[i].y);         
            _length+=Point.distance(p1, p2);

}

Now I want to subdivie the line segment made by those points to equal segment by a random int: 10,or 11,20,...and the equal point must be on the line still .

I am not good at the Math,so sorry about that.I can't find the right function after some days trying to find it myself.

So,would you like to help me?

Best thanks and best wish to you!

Sorry for my English*^_^*

Ps:

I can get the point on the line by:
var p1:Point=new Point(Xn,Yn)
var p2:Point=new Point(Xn+1,Yn+1)
var p3:Point=Point.interpolate(p1,p2,m)//m= 0~1
2
Just find the slope of the line and calculate it based off that. Given point (0,0) and (10,5) you would have a slope of 5/10 or 1/2. The slope of a line is (Y2-Y1) / (X2-X1). Knowing that you can then assume that at point (5,Y) you would have a Y of 2.5. Using this kind of math could figure out any point along that line. - Andrew Sellenrick
Yes,thank you Andrew Sellenrick,you are right,but please see the demo.jpg,I am silly to find out the point one the next line or more next lines ^_^ - Mr.liao
Not sure I understand but I took a shot at answering below. - Andrew Sellenrick
Thank you Andrew Sellenrick:).Please look at demo.jpg_explanation.I try your function I think it's work for divide N segment between two points. With as3: var divides:int=10 var pts:Array=[]; for(var i:int=0;i<divides;i++){ var p1:Point =new Point(arr[0][0],arr[0][1]); var p2:Point =new Point(arr[1][0],arr[1][1]); var p3:Point =Point.interpolate(p1,p2,i/divides) pts.push(p3) } this can get the points along the line between the two points - Mr.liao

2 Answers

0
votes

You will have to loop over your array of points and with two at a time call this function and it will return to you an array of points along that line based on the number a divisions you want the line broken in to.

You need to call divideLine(arr[0],arr[1],#random) divideLine(arr[1],arr[2],#random) divideLine(arr[3],arr[4],#random) ect.

function divideLine(point1,point2,divisions)
{
  var rise = point2[1] - point1[1];
  var run = point2[0] - point1[0];
  var slope = rise/run;
  var seg = run/divisions
  var pointsAlongLine = [];
  for(var i = 1 ; i < divisions ; i++){
    var pointOnLine = [seg*i,seg*i*slope];
    pointsAlongLine.push(pointOnLine);
  }
  return pointsAlongLine;
}

working JS version http://codepen.io/asellenrick/pen/bpqGwE?editors=0011

0
votes

Thank you,Andrew Sellenrick.I get the right function from your way:)

function divideLine(point1,point2,divisions)
{
  var rise = point2[1] - point1[1];
  var run = point2[0] - point1[0];
  var slope = rise/run;
  var step = divisions * 200;
  var seg = run/step;
  var pointsAlongLine = [];
  for(var i = 1 ; i < step ; i++){
    var pointOnLine = [point1[0]+seg*i,point1[1]+seg*i*slope];
    pointsAlongLine.push(pointOnLine);
  }
  return pointsAlongLine;
}