Guest User

Untitled

a guest
Feb 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. def plot(x,y)
  2. puts "(#{x},#{y})"
  3. end
  4.  
  5. def les(x1,y1,x2,y2, edge=:left)
  6. # sort by y
  7. if y1 > y1
  8. x1,y1,x2,y2 = x2,y2,x1,y1
  9. end
  10.  
  11. # precalculations
  12. dx = x2-x1
  13. dy = y2-y1
  14. m = dy.to_f/dx
  15.  
  16. x = x1
  17. s = dx / dx.abs
  18. if edge == :left
  19. if s > 0
  20. inc = dy
  21. o = dy
  22. else
  23. inc = 0
  24. o = dy-1
  25. end
  26. else
  27. if s > 0
  28. inc = 0
  29. o = dy-1
  30. else
  31. inc = -dy
  32. o = dy
  33. end
  34. end
  35.  
  36. y1.upto(y2) do |y|
  37. plot(x,y)
  38. inc += dx
  39. while inc.abs > o
  40. x += s
  41. inc -= s*dy
  42. end
  43. end
  44. puts
  45. end
  46.  
  47. les(0,0,3,12)
  48. les(0,0,-3,12)
  49. les(0,0,11,3)
  50. les(0,0,-11,3)
  51.  
  52. les(0,0,3,12,:right)
  53. les(0,0,-3,12,:right)
  54. les(0,0,11,3,:right)
  55. les(0,0,-11,3,:right)
Add Comment
Please, Sign In to add comment