jgtrosh

draw-windscreenswiper.py

Sep 7th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 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 os
  59. if "linux" in os.sys.platform:
  60.     outchar = "\033[41m \033[0m"
  61. else:
  62.     outchar = "."
  63. #p0 = point(3, 1);  p1 = point(22, 10); p2 = point(41, 1)
  64. #p3 = point(1, 15); p4 = point(43, 15); p5 = point(22, 15)
  65. #draw_line(p0, p1, a)
  66. #draw_line(p1, p2, a)
  67. #draw_line(p0, p3, a)
  68. #draw_line(p2, p4, a)
  69. #draw_line(p3, p4, a)
  70. #draw_line(p1, p5, a)
  71. #plot(p0, p)
  72. #plot(p1, p)
  73. #plot(p2, p)
  74. #plot(p3, p)
  75. #plot(p4, p)
  76. #plot(p5, p)
  77. import math
  78. from time import sleep
  79. i = 0
  80. a = point(10, 2)
  81. b = point(10+math.sin(i)*5, math.sin(math.sin(i))**2*5+math.cos(math.sin(i))**2*5)
  82. while True:
  83.     draw_line(a, b, " ")
  84.     i += .5
  85.     b = point(10+math.sin(i)*5, math.sin(math.sin(i))**2*5+math.cos(math.sin(i))**2*5)
  86.     draw_line(a, b, outchar)
  87.     out()
  88.     sleep(0.1)
  89.  
  90. if 'win' in os.sys.platform:
  91.     os.system("PAUSE")
Advertisement
Add Comment
Please, Sign In to add comment