#============================================================================== # * Class Bitmap #============================================================================== class Bitmap def draw_line(x1,y1,x2,y2,c = Color.new(0,0,0,255)) # Vertical line if x1 == x2 for y in y1...y2 self.set_pixel(x1,y,c) end # Horizontal line elsif y1 == y2 for x in x1...x2 self.set_pixel(x,y1,c) end # point elsif x1 == x2 and y1 == y2 self.set_pixel(x1,y1,c) # Neither horizontal or vertical else # Function for line (f(x) = mx + n) m = (y2-y1)/(x2-x1).to_f # gradient # Prevent zerodivision if x1 != 0 n = y1/(m*x1) # base elsif x2 != 0 n = y2/(m*x2) # base end # set_pixel(x,f(x)) for every x between x1 and x2 for x in x1...x2 self.set_pixel(x,m*x+n,c) end end end end