Skip to content

Instantly share code, notes, and snippets.

@bayleedev
Last active August 29, 2015 14:05
Show Gist options
  • Save bayleedev/19a157a35490013430b9 to your computer and use it in GitHub Desktop.
Save bayleedev/19a157a35490013430b9 to your computer and use it in GitHub Desktop.
class Line
constructor: (one, two) ->
@one = one
@two = two
m: ->
(@two.y - @one.y) / (@two.x - @one.x)
b: ->
@one.y - (@m() * @one.x)
y: (x) ->
(@m() * x) + @b()
x: (y) ->
(y - @b()) / @m()
points: (callback) ->
points = []
callback = ((v) -> v) unless callback
if Math.abs(@one.x - @two.x) > Math.abs(@one.y - @two.y)
for x in [@one.x..@two.x]
points.push callback([x, @y(x)])
else
for y in [@one.y..@two.y]
points.push callback([@x(y), y])
points
line = new Line({x: 3, y: 2}, {x: 7, y: 4})
line.y(3) # 2
line.y(7) # 4
line.x(2) # 3
line.x(4) # 7
line.points() # [[3,2], [4,2.5], [5,3], [6,3.5], [7,4]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment