Advertisement
nux95

Python line-drawin algo

Jan 23rd, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1.     def drawLine(self, x1, y1, x2, y2, aamode = 0):
  2.         ''' Draws a line between the points *(x1, y1)* and *(x2, y2)* in the
  3.            current pen-color. *aamode* defines the use of anti-aliasing.
  4.  
  5.            .. note:: Anti-aliasing is not implemented yet. '''
  6.         if x2 < x1:
  7.             x1, x2 = x2, x1
  8.        
  9.         # 1. we need to calculate the slope of the line, remember the good old
  10.         #    linear-function formula from maths: f(x) = mx + t
  11.         slope = (y2 - y1) / (x2 - x1)
  12.  
  13.         # 2. we will now iterate over the full length of the line in x-direction
  14.         #    and calculate the corresponding y-value
  15.         for x in xrange(x2 - x1):
  16.             y = slope * x + y1
  17.             x = x + x1
  18.             self.setPixel(x, y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement