Created
February 5, 2013 14:36
-
-
Save 0xKD/4714810 to your computer and use it in GitHub Desktop.
Bresenham line drawing in Python using Pygame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys,pygame | |
from pygame import gfxdraw | |
pygame.init() | |
screen = pygame.display.set_mode((400,400)) | |
screen.fill((0,0,0)) | |
pygame.display.flip() | |
white = (255,255,255) | |
def bresenham(x1,y1,x2,y2): | |
dx = x2-x1 | |
dy = y2-y1 | |
D = 2*dy - dx | |
gfxdraw.pixel(screen,x1,y1,white) | |
y = y1 | |
for x in range(x1+1,x2+1): | |
if D > 0: | |
y += 1 | |
gfxdraw.pixel(screen,x,y,white) | |
D += (2*dy-2*dx) | |
else: | |
gfxdraw.pixel(screen,x,y,white) | |
D += 2*dy | |
pygame.display.flip() | |
bresenham(10,10,50,50) | |
while 1: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
c++