TheoAllen

Bitmap Drawline

May 21st, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.83 KB | None | 0 0
  1. class Bitmap
  2.  
  3.   def draw_line(x1,y1,x2,y2,color = Color.new(255,255,255))
  4.     jarak_x = (x2-x1).abs
  5.     jarak_y = (y2-y1).abs
  6.     if jarak_y == 0 || jarak_x == 0
  7.       draw_horz(x1,y1,jarak_x,color) if jarak_y == 0
  8.       draw_vert(x1,y1,jarak_y,color) if jarak_x == 0
  9.       return
  10.     end
  11.     maximum = [jarak_x,jarak_y].max
  12.     rasio_x = jarak_x / maximum.to_f
  13.     rasio_y = jarak_y / maximum.to_f
  14.     real_x = x1.to_f
  15.     real_y = y1.to_f
  16.     for i in 0..maximum
  17.       fill_rect(x1,y1,1,1,color)
  18.       real_x += rasio_x
  19.       real_y += rasio_y
  20.       x1 = real_x.round
  21.       y1 = real_y.round
  22.     end
  23.   end
  24.  
  25.   def draw_horz(x,y,width,color = Color.new(255,255,255))
  26.     fill_rect(x,y,width,1,color)
  27.   end
  28.  
  29.   def draw_vert(x,y,height,color = Color.new(255,255,255))
  30.     fill_rect(x,y,1,height,color)
  31.   end
  32.  
  33. end
Advertisement
Add Comment
Please, Sign In to add comment