Bresenham line algorithm
(x,y,x2,y2)
| 221 | self.drawRect( x, y, wid, wid, fill ) |
| 222 | |
| 223 | def bresLine(x,y,x2,y2): |
| 224 | """Bresenham line algorithm""" |
| 225 | steep = 0 |
| 226 | coords = [] |
| 227 | dx = int(abs(x2 - x)+0.5) |
| 228 | if (x2 - x) > 0: |
| 229 | sx = 1 |
| 230 | else: |
| 231 | sx = -1 |
| 232 | dy = int(abs(y2 - y)+0.5) |
| 233 | if (y2 - y) > 0: |
| 234 | sy = 1 |
| 235 | else: |
| 236 | sy = -1 |
| 237 | if dy > dx: |
| 238 | steep = 1 |
| 239 | x,y = y,x |
| 240 | dx,dy = dy,dx |
| 241 | sx,sy = sy,sx |
| 242 | dx2 = dx*2 |
| 243 | dy2 = dy*2 |
| 244 | d = dy2 - dx |
| 245 | for i in range(0,dx): |
| 246 | coords.append( (x,y) ) |
| 247 | while d >= 0: |
| 248 | y += sy |
| 249 | d -= dx2 |
| 250 | x += sx |
| 251 | d += dy2 |
| 252 | |
| 253 | if steep: #transpose x's and y's |
| 254 | coords = [ (c[1],c[0]) for c in coords ] |
| 255 | |
| 256 | coords.append( (x2,y2) ) |
| 257 | |
| 258 | return coords |
| 259 | bresLine = staticmethod( bresLine ) |
| 260 | |
| 261 | def _drawLine( self, x1, y1, x2, y2 ): |