From The Compiler, 6 Years ago, written in Python.
Embed
  1. #!/usr/bin/python3
  2. # vim: set fileencoding=utf-8 ts=4 sts=4 sw=4 tw=80 expandtab :
  3.  
  4. # Copyright (C) 2013 Florian Bruhin <me@the-compiler.org>
  5.  
  6. # NAME is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # NAME is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with NAME.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. import sys
  20. # import math
  21.  
  22. fb = [ None ] * 64
  23.  
  24. for i in range(64):
  25.     fb[i-1] = [ '.' ] * 128
  26.  
  27. def set(x, y):
  28.     if 0 <= x <= 127 and 0 <= y <= 63:
  29.         fb[y][x] = 'x'
  30.  
  31. def printfb():
  32.     for l in fb:
  33.         print(''.join(l))
  34.  
  35. # def circle(x0, y0, r):
  36. #     frac = 1000
  37. #     step = 2 * math.pi / frac
  38. #     for theta in _arange(0, 2 * math.pi, step):
  39. #         x = x0 + r * math.cos(theta)
  40. #         y = y0 + r * math.sin(theta)
  41. #         set(round(x), round(y))
  42.  
  43. # def _arange(start, stop, step):
  44. #     while start < stop:
  45. #         yield start
  46. #         start += step
  47.  
  48. # http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
  49. def circle(x0, y0, r):
  50.     f = 1 - r
  51.     ddF_x = 1
  52.     ddF_y = -2 * r
  53.     x = 0
  54.     y = r
  55.     setpm(x0, y0, dx=r)
  56.     setpm(x0, y0, dy=r)
  57.     while x < y:
  58.         if (f >= 0):
  59.             y -= 1
  60.             ddF_y += 2
  61.             f += ddF_y
  62.         x += 1
  63.         ddF_x += 2
  64.         f += ddF_x
  65.         setpm(x0, y0, dx=x, dy=y)
  66.         setpm(x0, y0, dx=y, dy=x)
  67.  
  68. def setpm(x0, y0, dx=None, dy=None):
  69.     if (dx is not None) and (dy is not None): # +- x and +- y
  70.         setpm(x0 + dx, y0, dy=dy) # +- y  with x+
  71.         setpm(x0 - dx, y0, dy=dy) # +- y  with x-
  72.     elif (dx is not None): # +- x
  73.         set(x0 + dx, y0)
  74.         set(x0 - dx, y0)
  75.     elif (dy is not None): # +- y
  76.         set(x0, y0 + dy)
  77.         set(x0, y0 - dy)
  78.  
  79. # def line(x1, y1, x2, y2):
  80. #     dx = x1 - x2
  81. #     dy = y1 - y2
  82. #     m = dy / dx
  83. #     q = y1
  84. #     for x in range(0, x2 - x1 + 1):
  85. #         y = m * x + q
  86. #         set(x1 + x, round(y))
  87.  
  88. # http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
  89. def line(x1, y1, x2, y2):
  90.     dx = abs(x2 - x1)
  91.     dy = abs(y2 - y1)
  92.     sx = 1 if x1 < x2 else -1
  93.     sy = 1 if y1 < y2 else -1
  94.     err = dx - dy
  95.     while not (x1 == x2 and y1 == y2):
  96.         set(x1, y1)
  97.         if 2*err > -dy:
  98.             err -= dy
  99.             x1 += sx
  100.         elif 2*err < dx:
  101.             err += dx
  102.             y1 += sy
  103.  
  104. def main():
  105.     set(23,42)
  106.     line(0,0,127,33)
  107.     line(50,50,100,100)
  108.     line(80,30,80,40)
  109.     line(100,8,120,8)
  110.     circle(30, 30, 20)
  111.     printfb()
  112.  
  113. if __name__ == '__main__':
  114.     sys.exit(main())
  115.