jgtrosh

drawline.py

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