Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python Line drawer using PyGame Library.
- # Code by Umut Bilgic.
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement