Skip to main content
More
Source Link
Blindman67
  • 460
  • 2
  • 9

To space points on a line can be done several ways.

Where the line is defined with

x1 = ?  // line start
y1 = ? 
x2 = ?  // line end
y2 = ?

To get a point on the line via a fraction of the line length

pointAt = ? // 0 to 1 are on the line 
            // less than 0 is before the line greater than 1 is after the line

px = (x2-x1) * pointAt + x1;
py = (y2-y1) * pointAt + y1;

To get a point a set distance along the line get the normalized vector of the line

distOnLine = ?
nx = x2-x1
ny = y2-y1
lengthOfLine = sqrt(nx * nx + ny * ny)
if(lengthOfLine > 0){ // make sure the line has length
   nx /= lengthOfLine // normalise the vector
   ny /= lengthOfLine
   px = x1 + nx * distOnLine
   py = y1 + ny * distOnLine
}

To get points on the line a specific x coordinates find the slope of the line in terms of x

findPointForX = ?
if(x1 !== x2){ // make sure the line is not vertical
    slope = (y2-y1)/(x2-x1)
    px = findPointForX
    py = (findPointForX - x1) * slope + y1
}

For y just swap x for y in the above

Think that covers most of the situations. If you want to divide a line into equal segments use the first method and set the fraction as multiples of 1/steps.

If you want a point on a line at an angle

distOnLine = ? // where on the line in pixels
x1 = ?  // the line start
y1 = ?
angle = ?  // the angle in radians

px = cos(angle) * distOnLine + x1
py = sin(angle) * distOnLine + y1

To convert to radians from deg

rad = deg / (180 / PI) // where PI is 3.1415....

To space points on a line can be done several ways.

Where the line is defined with

x1 = ?  // line start
y1 = ? 
x2 = ?  // line end
y2 = ?

To get a point on the line via a fraction of the line length

pointAt = ? // 0 to 1 are on the line 
            // less than 0 is before the line greater than 1 is after the line

px = (x2-x1) * pointAt + x1;
py = (y2-y1) * pointAt + y1;

To get a point a set distance along the line get the normalized vector of the line

distOnLine = ?
nx = x2-x1
ny = y2-y1
lengthOfLine = sqrt(nx * nx + ny * ny)
if(lengthOfLine > 0){ // make sure the line has length
   nx /= lengthOfLine // normalise the vector
   ny /= lengthOfLine
   px = x1 + nx * distOnLine
   py = y1 + ny * distOnLine
}

To get points on the line a specific x coordinates find the slope of the line in terms of x

findPointForX = ?
if(x1 !== x2){ // make sure the line is not vertical
    slope = (y2-y1)/(x2-x1)
    px = findPointForX
    py = (findPointForX - x1) * slope + y1
}

For y just swap x for y in the above

Think that covers most of the situations. If you want to divide a line into equal segments use the first method and set the fraction as multiples of 1/steps.

To space points on a line can be done several ways.

Where the line is defined with

x1 = ?  // line start
y1 = ? 
x2 = ?  // line end
y2 = ?

To get a point on the line via a fraction of the line length

pointAt = ? // 0 to 1 are on the line 
            // less than 0 is before the line greater than 1 is after the line

px = (x2-x1) * pointAt + x1;
py = (y2-y1) * pointAt + y1;

To get a point a set distance along the line get the normalized vector of the line

distOnLine = ?
nx = x2-x1
ny = y2-y1
lengthOfLine = sqrt(nx * nx + ny * ny)
if(lengthOfLine > 0){ // make sure the line has length
   nx /= lengthOfLine // normalise the vector
   ny /= lengthOfLine
   px = x1 + nx * distOnLine
   py = y1 + ny * distOnLine
}

To get points on the line a specific x coordinates find the slope of the line in terms of x

findPointForX = ?
if(x1 !== x2){ // make sure the line is not vertical
    slope = (y2-y1)/(x2-x1)
    px = findPointForX
    py = (findPointForX - x1) * slope + y1
}

For y just swap x for y in the above

Think that covers most of the situations. If you want to divide a line into equal segments use the first method and set the fraction as multiples of 1/steps.

If you want a point on a line at an angle

distOnLine = ? // where on the line in pixels
x1 = ?  // the line start
y1 = ?
angle = ?  // the angle in radians

px = cos(angle) * distOnLine + x1
py = sin(angle) * distOnLine + y1

To convert to radians from deg

rad = deg / (180 / PI) // where PI is 3.1415....
More to add
Source Link
Blindman67
  • 460
  • 2
  • 9

To space points on a line can be done several ways.

Where the line is defined with

x1 = ?  // line start
y1 = ? 
x2 = ?  // line end
y2 = ?

To get a point on the line via a fraction of the line length

pointAt = ? // 0 to 1 are on the line 
            // less than 0 is before the line greater than 1 is after the line

px = (x2-x1) * pointAt + x1;
py = (y2-y1) * pointAt + y1;

To get a point a set distance along the line get the normalized vector of the line

distOnLine = ?
nx = x2-x1
ny = y2-y1
lengthOfLine = sqrt(nx * nx + ny * ny)
if(lengthOfLine > 0){ // make sure the line has length
   nx /= lengthOfLine // normalise the vector
   ny /= lengthOfLine
   px = x1 + nx * distOnLine
   py = y1 + ny * distOnLine
}

To get points on the line a specific x coordinates find the slope of the line in terms of x

findPointForX = ?
if(x1 !== x2){ // make sure the line is not vertical
    slope = (y2-y1)/(x2-x1)
    px = findPointForX
    py = (findPointForX - x1) * slope + y1
}

For y just swap x for y in the above

Think that covers most of the situations. If you want to divide a line into equal segments use the first method and set the fraction as multiples of 1/steps.

To space points on a line can be done several ways.

Where the line is defined with

x1 = ?  // line start
y1 = ? 
x2 = ?  // line end
y2 = ?

To get a point on the line via a fraction of the line length

pointAt = ? // 0 to 1 are on the line 
            // less than 0 is before the line greater than 1 is after the line

px = (x2-x1) * pointAt + x1;
py = (y2-y1) * pointAt + y1;

To space points on a line can be done several ways.

Where the line is defined with

x1 = ?  // line start
y1 = ? 
x2 = ?  // line end
y2 = ?

To get a point on the line via a fraction of the line length

pointAt = ? // 0 to 1 are on the line 
            // less than 0 is before the line greater than 1 is after the line

px = (x2-x1) * pointAt + x1;
py = (y2-y1) * pointAt + y1;

To get a point a set distance along the line get the normalized vector of the line

distOnLine = ?
nx = x2-x1
ny = y2-y1
lengthOfLine = sqrt(nx * nx + ny * ny)
if(lengthOfLine > 0){ // make sure the line has length
   nx /= lengthOfLine // normalise the vector
   ny /= lengthOfLine
   px = x1 + nx * distOnLine
   py = y1 + ny * distOnLine
}

To get points on the line a specific x coordinates find the slope of the line in terms of x

findPointForX = ?
if(x1 !== x2){ // make sure the line is not vertical
    slope = (y2-y1)/(x2-x1)
    px = findPointForX
    py = (findPointForX - x1) * slope + y1
}

For y just swap x for y in the above

Think that covers most of the situations. If you want to divide a line into equal segments use the first method and set the fraction as multiples of 1/steps.

Source Link
Blindman67
  • 460
  • 2
  • 9

To space points on a line can be done several ways.

Where the line is defined with

x1 = ?  // line start
y1 = ? 
x2 = ?  // line end
y2 = ?

To get a point on the line via a fraction of the line length

pointAt = ? // 0 to 1 are on the line 
            // less than 0 is before the line greater than 1 is after the line

px = (x2-x1) * pointAt + x1;
py = (y2-y1) * pointAt + y1;