jgtrosh

drawsine.py

Sep 7th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. l = [[" "]]
  4. l_len_y = len(l)
  5. l_len_x = len(l[0])
  6.  
  7. class point:
  8.     def __init__(self, x, y):
  9.         self.x = x
  10.         self.y = y
  11.  
  12. def out():
  13.     for i in l:
  14.         print("".join(i))
  15.  
  16. def plot(p, c):
  17.     x = round(p.x); y = round(p.y)
  18.     global l_len_y
  19.     if y > l_len_y:
  20.         for i in range(l_len_y, y):
  21.             l.append([" " for n in range(l_len_x)])
  22.         l_len_y = y
  23.     global l_len_x
  24.     if x > l_len_x:
  25.         for i in range(l_len_y):
  26.             l[i].extend([" " for n in range(x-l_len_x)])
  27.         l_len_x = x
  28.     l[y-1][x-1] = c
  29.  
  30. def draw_line(p0, p1, c):
  31.     x0 = p0.x; y0 = p0.y
  32.     x1 = p1.x; y1 = p1.y
  33.     del p0, p1
  34.     if x0 < 0 or x1 < 0 or y0 < 0 or y1 < 0:
  35.         print("negative coordinate")
  36.         return
  37.     if x1 == x0:
  38.         for y in range(round(y0), round(y1)):
  39.             plot(point(x0, y), c)
  40.         return
  41.     s = (y1-y0)/(x1-x0)
  42.     if s < -1 or s > 1:
  43.         if y0 > y1:
  44.             x0, x1 = x1, x0
  45.             y0, y1 = y1, y0
  46.         s = 1/s
  47.         for y in range(round(y0), round(y1)+1):
  48.             x = round(x0+s*(y-y0))
  49.             plot(point(x, y), c)
  50.     else:
  51.         if x0 > x1:
  52.             x0, x1 = x1, x0
  53.             y0, y1 = y1, y0
  54.         for x in range(round(x0), round(x1+1)):
  55.             y = round(y0+s*(x-x0))
  56.             plot(point(x, y), c)
  57.  
  58. import math
  59. from time import sleep
  60. i = 0
  61. while True:
  62.     draw_line(point(5, 6+5*math.sin(i)), point(40, 6+5*math.sin(i+math.pi/2)), " ")
  63.     i += .3
  64.     draw_line(point(5, 6+5*math.sin(i)), point(40, 6+5*math.sin(i+math.pi/2)), ".")
  65.     out()
  66.     sleep(0.1)
  67.  
  68. import os
  69. if 'win' in os.sys.platform:
  70.     os.system("PAUSE")
Advertisement
Add Comment
Please, Sign In to add comment