Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def plot(x,y)
- puts "(#{x},#{y})"
- end
- def les(x1,y1,x2,y2, edge=:left)
- # sort by y
- if y1 > y1
- x1,y1,x2,y2 = x2,y2,x1,y1
- end
- # precalculations
- dx = x2-x1
- dy = y2-y1
- m = dy.to_f/dx
- x = x1
- s = dx / dx.abs
- if edge == :left
- if s > 0
- inc = dy
- o = dy
- else
- inc = 0
- o = dy-1
- end
- else
- if s > 0
- inc = 0
- o = dy-1
- else
- inc = -dy
- o = dy
- end
- end
- y1.upto(y2) do |y|
- plot(x,y)
- inc += dx
- while inc.abs > o
- x += s
- inc -= s*dy
- end
- end
- puts
- end
- les(0,0,3,12)
- les(0,0,-3,12)
- les(0,0,11,3)
- les(0,0,-11,3)
- les(0,0,3,12,:right)
- les(0,0,-3,12,:right)
- les(0,0,11,3,:right)
- les(0,0,-11,3,:right)
Add Comment
Please, Sign In to add comment