Wednesday, October 12, 2011

Aliased Circles

Inspired by this I wrote a little script to create antialiased circles in python
The Gist:
#! /usr/bin/env python
# -*- coding: latin-1 -*-
import math
characters = [32,0x2591,0x2592,0x2593,0x2588]
breakpoints = [0.1,0.177,0.316,0.563]
#reakpoints = [0.2,0.4,0.6,0.8]
def raster(x,y,R,center,SIGMA=1.5):
r = math.sqrt(math.pow(x-center,2)+math.pow( (y-center)*0.9,2))
return math.exp(-math.pow(r-R,2)/SIGMA)
from bisect import bisect
def genchr(z):
n = bisect(breakpoints,z)
return unichr(characters[n])
def circle(R,sigma=1.5,side=10):
size = 2*R + 2*side
array = []
for z in xrange(size):
array.append([' ']*size)
center = side+R
for i in xrange(size):
for j in xrange(size):
z = raster(i,j,R,center,sigma)
foo = genchr(z)
array[i][j] = foo
#print "{},{}:{}:{}".format(i,j,z,foo)
array[center][center] = 'X'
return array
import sys
def printarr(arr):
for line in arr:
for c in line:
print c,
print
if __name__=="__main__":
if len(sys.argv)>1:
if len(sys.argv)==2:
printarr(circle(int(sys.argv[1])))
elif len(sys.argv)==3:
printarr(circle(int(sys.argv[1]),float(sys.argv[2])))
else:
printarr(circle(int(sys.argv[1]),float(sys.argv[2]),int(sys.argv[3])))
else:
mesg = r"""Usage: circle R [SIGMA] [SIDE] where R is radius, sigma is sigma and side is buffer on edges"""
print mesg
view raw circle.py hosted with ❤ by GitHub

Example output:
░ ▒ ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▒ ░
░ ▓ ▓ █ █ █ █ █ █ █ █ █ █ █ ▓ ▓ ░
░ ▓ █ █ █ █ █ █ █ ▓ ▓ ▓ █ █ █ █ █ █ █ ▓ ░
▒ █ █ █ █ █ ▓ ▒ ░ ░ ▒ ▓ █ █ █ █ █ ▒
░ ▓ █ █ █ ▓ ▒ ░ ░ ▒ ▓ █ █ █ ▓ ░
░ ▓ █ █ █ ▒ ░ ░ ▒ █ █ █ ▓ ░
░ ▓ █ █ ▓ ▒ ▒ ▓ █ █ ▓ ░
▓ █ █ ▓ ░ ░ ▓ █ █ ▓
▒ █ █ █ ▒ ▒ █ █ █ ▒
░ ▓ █ █ ▒ ▒ █ █ ▓ ░
▒ █ █ ▓ ░ ░ ▓ █ █ ▒
▓ █ █ ▒ ▒ █ █ ▓
░ █ █ █ ░ ░ █ █ █ ░
▒ █ █ ▓ ▓ █ █ ▒
▒ █ █ ▒ ▒ █ █ ▒
▓ █ █ ▒ ▒ █ █ ▓
▓ █ █ ▒ X ▒ █ █ ▓
▓ █ █ ▒ ▒ █ █ ▓
▒ █ █ ▒ ▒ █ █ ▒
▒ █ █ ▓ ▓ █ █ ▒
░ █ █ █ ░ ░ █ █ █ ░
▓ █ █ ▒ ▒ █ █ ▓
▒ █ █ ▓ ░ ░ ▓ █ █ ▒
░ ▓ █ █ ▒ ▒ █ █ ▓ ░
▒ █ █ █ ▒ ▒ █ █ █ ▒
▓ █ █ ▓ ░ ░ ▓ █ █ ▓
░ ▓ █ █ ▓ ▒ ▒ ▓ █ █ ▓ ░
░ ▓ █ █ █ ▒ ░ ░ ▒ █ █ █ ▓ ░
░ ▓ █ █ █ ▓ ▒ ░ ░ ▒ ▓ █ █ █ ▓ ░
▒ █ █ █ █ █ ▓ ▒ ░ ░ ▒ ▓ █ █ █ █ █ ▒
░ ▓ █ █ █ █ █ █ █ ▓ ▓ ▓ █ █ █ █ █ █ █ ▓ ░
░ ▓ ▓ █ █ █ █ █ █ █ █ █ █ █ ▓ ▓ ░
░ ▒ ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▒ ░
view raw circle15.txt hosted with ❤ by GitHub

1 comment: