03-01-2014, 11:20 AM 
(This post was last modified: 03-01-2014, 11:20 AM by WrytXander.)
	
	
	
		Hello folks! I have been trying to wrap my head around the infamous "Bresenham's Line Drawing Algorithm" in order to build a simple application that, well, draws lines. I managed to make one using in python, also using the PyGame Library for visuals.
I will post a link and paste the code here. (If you dont like reading in on the forums and like the sytnax highligting of pastebin, follow this link.)
Here goes le code:
And yeah, that is probably the worst implementation of this algorithm in Python, ever. That is why I need your help. Please post your thoughs and tell me what can be improved.
Thanks :3
	
	
	
I will post a link and paste the code here. (If you dont like reading in on the forums and like the sytnax highligting of pastebin, follow this link.)
Here goes le code:
Code:
import pygame
from pygame.locals import *
import sys
def app_quit():
    pygame.quit()
    sys.exit("app_quit()")
def get_line(x1, y1, x2, y2):
    points = []
    issteep = abs(y2-y1) > abs(x2-x1)
    if issteep:
        x1, y1 = y1, x1
        x2, y2 = y2, x2
    rev = False
    if x1 > x2:
        x1, x2 = x2, x1
        y1, y2 = y2, y1
        rev = True
    deltax = x2 - x1
    deltay = abs(y2-y1)
    error = int(deltax / 2)
    y = y1
    ystep = None
    if y1 < y2:
        ystep = 1
    else:
        ystep = -1
    for x in range(x1, x2 + 1):
        if issteep:
            points.append((y, x))
        else:
            points.append((x, y))
        error -= deltay
        if error < 0:
            y += ystep
            error += deltax
    if rev:
        points.reverse()
    return points
    
# Colors #
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
# Screen Size #
width,height = 400,400
screen_size = (width,height)
# Inıtialization #
pygame.init()
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Pygame")
# Main loop #
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            app_quit()
    mouse_state = pygame.mouse.get_pressed()
    draw_pos = pygame.mouse.get_pos()
    
    print ("")
    user_pos_1 = input("Enter position 1: ")
    user_pos_2 = input("Enter position 2: ")
    pos_1_list = user_pos_1.split(",")
    pos_2_list = user_pos_2.split(",")
    X1,Y1 = int(pos_1_list[0]),int(pos_1_list[1])
    X2,Y2 = int(pos_2_list[0]),int(pos_2_list[1])
    final_list = get_line(X1,Y1,X2,Y2)
    for i in range(0,len(final_list)):
        final_pos = final_list[i]
        screen.set_at(final_pos, white)
 
    pygame.display.update()And yeah, that is probably the worst implementation of this algorithm in Python, ever. That is why I need your help. Please post your thoughs and tell me what can be improved.
Thanks :3