bresenham python
时间: 2023-08-16 11:14:42 浏览: 136
Bresenham's line algorithm is a popular algorithm used for drawing lines on a raster grid. It is efficient and widely used in computer graphics. Here's an example of how to implement Bresenham's line algorithm in Python:
```python
def draw_line(x0, y0, x1, y1):
dx = abs(x1 - x0)
dy = abs(y1 - y0)
sx = -1 if x0 > x1 else 1
sy = -1 if y0 > y1 else 1
err = dx - dy
while x0 != x1 or y0 != y1:
print(x0, y0) # Replace this with your desired action (e.g., drawing a pixel)
e2 = 2 * err
if e2 > -dy:
err -= dy
x0 += sx
if e2 < dx:
err += dx
y0 += sy
# Example usage
draw_line(1, 1, 8, 5)
```
This implementation will print out the coordinates of the pixels along the line. You can replace the `print(x0, y0)` line with your own code to draw pixels or perform any desired action.
阅读全文