asciimoo/drawille — pull request #2 by livibetter (2014-04-24 08:18:57+00:00)
Title: add more images

Make `README` more attractive.

Just for the record, here are the modified example codes for grabbing frames:

```python
from drawille import Canvas, line
import math
import os
from time import sleep

class Point3D:
def __init__(self, x = 0, y = 0, z = 0):
self.x, self.y, self.z = float(x), float(y), float(z)

def rotateX(self, angle):
""" Rotates the point around the X axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
y = self.y * cosa - self.z * sina
z = self.y * sina + self.z * cosa
return Point3D(self.x, y, z)

def rotateY(self, angle):
""" Rotates the point around the Y axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
z = self.z * cosa - self.x * sina
x = self.z * sina + self.x * cosa
return Point3D(x, self.y, z)

def rotateZ(self, angle):
""" Rotates the point around the Z axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
x = self.x * cosa - self.y * sina
y = self.x * sina + self.y * cosa
return Point3D(x, y, self.z)

def project(self, win_width, win_height, fov, viewer_distance):
""" Transforms this 3D point to 2D using a perspective projection. """
factor = fov / (viewer_distance + self.z)
x = self.x * factor + win_width / 2
y = -self.y * factor + win_height / 2
return Point3D(x, y, 1)

vertices = [
Point3D(-20,20,-20),
Point3D(20,20,-20),
Point3D(20,-20,-20),
Point3D(-20,-20,-20),
Point3D(-20,20,20),
Point3D(20,20,20),
Point3D(20,-20,20),
Point3D(-20,-20,20)
]

# Define the vertices that compose each of the 6 faces. These numbers are
# indices to the vertices list defined above.
faces = [(0,1,2,3),(1,5,6,2),(5,4,7,6),(4,0,3,7),(0,4,5,1),(3,2,6,7)]

angleX, angleY, angleZ = 0, 0, 0
c = Canvas()
for i in range(30):
# Will hold transformed vertices.
t = []

for v in vertices:
# Rotate the point around X axis, then around Y axis, and finally around Z axis.
p = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ)
# Transform the point from 3D to 2D
#p = p.project(50, 50, 50, 50)
# Put the point in the list of transformed vertices
t.append(p)

for f in faces:
for x,y in line(t[f[0]].x, t[f[0]].y, t[f[1]].x, t[f[1]].y):
c.set(x,y)
for x,y in line(t[f[1]].x, t[f[1]].y, t[f[2]].x, t[f[2]].y):
c.set(x,y)
for x,y in line(t[f[2]].x, t[f[2]].y, t[f[3]].x, t[f[3]].y):
c.set(x,y)
for x,y in line(t[f[3]].x, t[f[3]].y, t[f[0]].x, t[f[0]].y):
c.set(x,y)

c.set(-80,-80)
c.set(80,80)
print "\x1b[2J\x1b[H" + c.frame()

angleX += 2 * 3
angleY += 3 * 3
angleZ += 5 * 3
sleep(1.0/20)
os.system('xsnap -region 240x240+124+163 -file /tmp/rotating_cube.%04d.png' % i)
# $ mogrify -format gif *.png
# $ gifsicle --delay 10 --loopcount -O3 /tmp/rotating_cube.????.gif > rotating_cube.gif
c.clear()
```

```python
from drawille import Canvas, line
import math
import os
from time import sleep
c = Canvas()
deg = 15
height = 40
for i in range(360//deg):
for x,y in line(0, height, 180, int(math.sin(math.radians(i*deg))*height + height)):
c.set(x,y)
for x in range(0, 360, 2):
coords = (x/2, height + int(round(math.sin(math.radians(x+i*deg)) * height)))
c.set(*coords)
print "\x1b[2J\x1b[H" + c.frame()
sleep(1.0/24)
os.system('xsnap -region 543x270+2+20 -file /tmp/sine_tracking.%04d.png' % i)
# $ mogrify -format gif *.png
# $ gifsicle --delay 10 --loopcount -O3 /tmp/sine_tracking.????.gif > sine_tracking.gif
c.clear()

```