Skip to content

Instantly share code, notes, and snippets.

@ChangJoo-Park
Created April 26, 2013 04:52
Show Gist options
  • Save ChangJoo-Park/5465099 to your computer and use it in GitHub Desktop.
Save ChangJoo-Park/5465099 to your computer and use it in GitHub Desktop.
Bresenham Algorithm with Ruby
def Bresenham(x1,y1,x2,y2)
if(x1 > x2)
Bresenham(x2,y2,x1,y1)
return
end
dx = x2 - x1
dy = y2 - y1
if( dy< 0 )
slope = -1
dy = -dy
else
slope = 1
end
incE = 2 * dy
incNE = (2 * dy) - (2 * dx)
d = (2 * dy) - dx
y = y1
for x in x1..x2
puts "x : #{x} , y : #{y}"
if(d<=0)
d += incE
puts "increase East"
else
d += incNE;
y += slope
puts "increase NorthEast"
end
end
end
Bresenham(0,0,11,3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment