Guest User

Untitled

a guest
Jul 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. import pygame
  2. import math
  3. from time import sleep
  4.  
  5. width = 640
  6. height = 480
  7.  
  8.  
  9. screen = pygame.display.set_mode((width,height))
  10.  
  11. def draw_pixel(x,y,r=255,g=255,b=255):
  12.         screen.set_at((int(x),int(y)), (r,g,b))
  13.  
  14.  
  15.  
  16. def draw_line(x1,y1,x2,y2):
  17.     if x1 == x2:
  18.         if y2 < y1:
  19.             for y in range(y1,y2-1, -1):
  20.                 draw_pixel(x1,y)
  21.             return
  22.         else:
  23.             for y in range(y1,y2+1):
  24.                 draw_pixel(x1,y)
  25.             return
  26.     if y1 == y2:
  27.         if x2 < x1:
  28.             for x in range(x1,x2-1,-1):
  29.                 draw_pixel(x,y1)
  30.             return
  31.         else:
  32.             for x in range(x1,x2+1):
  33.                 draw_pixel(x,y1)
  34.             return
  35.                        
  36.     rise = float(y2 - y1)
  37.     run = float(x2 - x1)
  38.  
  39.  
  40.     riser = rise/run
  41.     runr = 1
  42.     slope = riser/runr
  43.     if rise < run:
  44.         if x1 < x2:
  45.             for x in range(x1, x2-1,-1):
  46.                 y = round(slope*x+y1)
  47.                 draw_pixel(x,y)
  48.         else:
  49.             for x in range(x1, x2+1):
  50.                 y  = round(slope*x+y1)
  51.                 draw_pixel(x,y)
  52.     else:
  53.         if y2 < y1:
  54.             for y in range(y1, y2-1, -1):
  55.                 x = round((y-y1)/slope+x1)
  56.                 draw_pixel(x,y)
  57.         else:
  58.             for y in range(y1, y2+1):
  59.                 x = round((y-y1)/slope+x1)
  60.                 draw_pixel(x,y)
  61.        
  62.  
  63.  
  64. def draw_circle(radius, x=0, y=0):
  65.         assert radius >= 1, 'first argument radius must be 1 or greater'
  66.         points_list = []
  67.         mirrored_list = []
  68.         for xc in range(1,radius+1):
  69.                 high = radius
  70.                 low = 0
  71.                 guess = (high + low) / 2.0
  72.                 ctr = 1
  73.                 while abs(radius - math.sqrt((xc**2 + guess**2))) > 0.000000001 and ctr <100:
  74.                         if (xc**2 + guess**2) < radius**2:
  75.                                 low = guess
  76.                         else:
  77.                                 high = guess
  78.                         ctr +=1
  79.                         guess = (high + low) / 2.0
  80.                 assert ctr <= 100, 'iteration count exceeded in draw_point'
  81.                 yc = round(guess)
  82.                 points_list.append((xc,yc))
  83.                 points_list.append((yc,xc))
  84.  
  85.         points_list = list(set(points_list))        
  86.         for coord in points_list:
  87.                 draw_pixel(coord[0]+x,coord[1]+y)
  88.                 mirrored_list.append(((coord[0]*-1),coord[1]))
  89.                 mirrored_list.append(((coord[0]*-1),(coord[1])*-1))
  90.                 mirrored_list.append((coord[0],(coord[1]*-1)))
  91.         for coord in mirrored_list:
  92.                 draw_pixel(coord[0]+x,coord[1]+y)
  93.  
  94. draw_circle(100, 200, 200)
  95. pygame.display.flip()
  96. sleep(1)
  97. draw_line(30,30, 40, 250)
  98. pygame.display.flip()
  99. sleep(1)
  100. draw_line(30,30, 30,200)
  101. pygame.display.flip()
  102. sleep(1)
  103. draw_line(30,30, 200,30)
  104. pygame.display.flip()
  105. sleep(1)
  106. draw_line(100, 100, 50, 300)
  107. pygame.display.flip()
  108. sleep(1)
  109. draw_line(100, 100, 100, 1)
  110. pygame.display.flip()
  111. sleep(1)
  112. draw_line(100, 100, 50, 100)
  113. pygame.display.flip()
  114. sleep(1)
  115. #this one below this comment doesn't work
  116. draw_line(100, 100, 50, 10)
  117. pygame.display.flip()
  118. sleep(1)
  119. draw_line(100, 100, 10, 50)
  120. pygame.display.flip()
  121. sleep(1)
  122. sleep(10)
Add Comment
Please, Sign In to add comment