Skip to content

Instantly share code, notes, and snippets.

@evilstreak
Created April 20, 2010 10:40
Show Gist options
  • Save evilstreak/372293 to your computer and use it in GitHub Desktop.
Save evilstreak/372293 to your computer and use it in GitHub Desktop.
function points_on_a_line( from, to, gap ) {
// work out the difference in X and Y, and the hypotenuse
var dx = to.x - from.x,
dy = to.y - from.y,
h = Math.sqrt( dx*dx + dy*dy )
points = [],
// work out what proportion of the hypotenuse each step will be
step = gap / h,
d = 0,
i = 0;
// for each step, add that proportion of dx and dy to the start position
while ( ( d += ( ++i * step ) ) < 1 ) {
points.push( {
x : from.x + d * dx,
y : from.y + d * dy
} );
}
return points;
}
@robholland
Copy link

    def each_point(start, finish, max_step)
      dx = finish[0] - start[0]
      dy = finish[1] - start[1]
      hypotenuse = Math.sqrt(dx*dx + dy*dy)
      step = max_step / hypotenuse
      steps = hypotenuse / max_step

      1.upto(steps) do |i|
        yield start[0] + i * step * dx, start[1] + i * step * dy
      end
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment