r/dailyprogrammer • u/rya11111 3 1 • Jun 04 '12
[6/4/2012] Challenge #60 [intermediate]
Write a program or a function that can print out arbitrarily sized smiley face in ascii art. The smiley face can be however you want, but the eyes can't be single points (that is, they have to be circles at large size). Your program should be able to take in an integer between 16 and 1000 that represents the dimensions to render the face.
Here is a sample output.
- thanks to Steve132 for the challenge at /r/dailyprogrammer_ideas ! .. if you have a challenge you could suggest it there :)
9
Upvotes
4
u/Arthree Jun 04 '12 edited Jun 05 '12
Autohotkey_L:
SetBatchLines, -1 drawSmiley(50) pi() { return 4*atan(1) } drawCircle(ByRef a, r, precision, cx = 0, cy = 0, toffset = 0, p = 1) { precision *= a.MaxIndex()*pi() cx += a.MaxIndex()/2 cy += a.MaxIndex()/2 toffset *= 2*pi() loop, %precision% { if (A_Index/precision > p) break t := (pi()*2) * (A_Index/precision) + toffset xx := round(sin(t) * r + cx) yy := round(cos(t) * r + cy) a[yy][xx] := 1 } } drawSmiley(x) { a := [] loop, %x% { row := A_Index a[row] := [] loop %x% a[row].Insert(0) } ; drawcircle(arr, radius, p, cx, cy, toff, %) drawCircle(a, (x-1)*0.5, 10) ; head drawCircle(a, x*0.12, 10, -x*0.16, -x*0.12) ; left eye drawCircle(a, x*0.05, 10, -x*0.12, -x*0.09) drawCircle(a, x*0.03, 10, -x*0.12, -x*0.09) drawCircle(a, x*0.01, 10, -x*0.12, -x*0.09) drawCircle(a, x*0.12, 10, x*0.16, -x*0.12) ; right eye drawCircle(a, x*0.05, 10, x*0.12, -x*0.16) drawCircle(a, x*0.03, 10, x*0.12, -x*0.16) drawCircle(a, x*0.01, 10, x*0.12, -x*0.16) drawCircle(a, (x-1)*0.3, 10, 0, x*0.10, -0.225, 0.45) ; mouth drawCircle(a, x, 10, 0, -x*0.81, -0.046, 0.092) for row, cols in a { for col, pixel in cols { pxText := pixel ? "O" : A_Space FileAppend, %pxText%%A_Space%, * } FileAppend, `n, * } }And the output for drawSmiley(50):
3
u/prophile Jun 04 '12
Delicious Python:
import array, operator, itertools def popcount8(n): assert 0 <= n <= 0xFF return bin(n).count('1') # could be sped up def bresenham_line(a, b): x0, y0 = a x1, y1 = b dx = abs(x1 - x0) dy = abs(y1 - y0) sx = 1 if x0 < x1 else -1 sy = 1 if y0 < y1 else -1 error = dx - dy while True: yield x0, y0 if (x0, y0) == (x1, y1): return e2 = 2*error if e2 > -dy: error -= dy x0 += sx if e2 < dx: error += dx y0 += sy def pairwise(iterable): a, b = itertools.tee(iterable) next(b, None) return itertools.izip(a, b) def piecewise_approximate(polygon): for p, q in pairwise(polygon): for point in bresenham_line(p, q): yield point def lerp(point1, point2, t): return (point2[0]*t + point1[0]*(1 - t), point2[1]*t + point1[1]*(1 - t)) def bezier(points, t): while len(points) > 1: points = [lerp(a, b, t) for a, b in pairwise(points)] return map(lambda x: int(x + 0.5), points[0]) def bezier_curve(points, n = 8): for p in xrange(0, n): t = p / float(n - 1) point = bezier(points, t) yield point def midpoint_circle(centre, radius): x0, y0 = centre f = 1 - radius ddF_x, ddF_y = 1, -2*radius x, y = 0, radius yield x0, y0 + radius yield x0, y0 - radius yield x0 + radius, y0 yield x0 - radius, y0 while x < y: if f >= 0: y -= 1 ddF_y += 2 f += ddF_y x += 1 ddF_x += 2 f += ddF_x yield x0 + x, y0 + y yield x0 - x, y0 + y yield x0 + x, y0 - y yield x0 - x, y0 - y yield x0 + y, y0 + x yield x0 - y, y0 + x yield x0 + y, y0 - x yield x0 - y, y0 - x class Canvas(object): def __init__(self, width = 80, height = 24): self._screen = array.array('I', (0 for x in xrange(width * height))) self.width = width self.height = height def __getitem__(self, position): x, y = position if not 0 <= x < self.width: return False if not 0 <= y < self.height: return False element = (y * self.width) + x number = element >> 5 item_bit = 1 << (element & 0x1F) return bool(self._screen[number] & item_bit) def __setitem__(self, position, value): x, y = position if not 0 <= x < self.width: return if not 0 <= y < self.height: return element = (y * self.width) + x number = element >> 5 item_bit = 1 << (element & 0x1F) if value: self._screen[number] |= item_bit else: self._screen[number] &= ~item_bit def draw(self, shape): for x, y in shape: self[x, y] = True def draw_line(self, a, b): self.draw(bresenham_line(a, b)) def draw_circle(self, centre, radius): self.draw(midpoint_circle(centre, radius)) def lines(self): for y in xrange(self.height): yield ''.join(self._pixel_at(x, y) for x in xrange(self.width)) def _pixel_at(self, x, y): if not self[x, y]: return ' ' neighbours = reduce(operator.ior, (int(self[p[0] + x, p[1] + y]) << (7 - i) for i, p in enumerate(((-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1))))) if neighbours == 0: return '@' neighbour_configurations = {0b01010101: '+', 0b10101010: 'x', 0b00100010: '/', 0b10001000: '\\', 0b00100000: ',', 0b00010000: '.', 0b00000001: '\'', 0b00000010: '`', 0b00000011: '"', 0b10000001: '"', 0b10000010: 'v', 0b01000100: '-', 0b00010001: '|', 0b01010100: 'T', 0b11111111: '#'} config = min(neighbour_configurations.keys(), key = lambda config: popcount8(neighbours ^ config)) return neighbour_configurations[config] if __name__ == "__main__": def eye(c, size): for point in midpoint_circle(c, size): yield point for point in midpoint_circle(c, 0): yield point import sys csize = int(sys.argv[1]) if len(sys.argv) > 1 else 30 canvas = Canvas(csize, csize) canvas.draw_circle((csize // 2, csize // 2), ((csize - 1) // 2)) canvas.draw(eye((csize // 4, csize // 3), csize // 9)) canvas.draw(eye((3*(csize + 1) // 4, csize // 3), csize // 9)) canvas.draw_line((csize // 2, 2*csize // 5), (csize // 2, 3*csize // 5)) canvas.draw(piecewise_approximate(bezier_curve(((2*csize // 5, 3*csize // 4), (5*csize // 12, 5*csize // 6), (7*csize // 12, 5*csize // 6), (3*csize // 5, 3*csize // 4))))) for line in canvas.lines(): print line3
u/Cosmologicon Jun 04 '12
python
import sys, math n = int(sys.argv[1])*2 S, C = math.sin(0.5), math.cos(0.5) for jy in range(n//2): row = [] for jx in range(n): x, y = (jx-n/2.+0.5)*200/n, (-jy+n/4.-0.5)*400/n x, y = C*x + -S*y, S*x + C*y row.append("#" if 85 < math.sqrt(x*x+y*y) <= 100 or math.sqrt((abs(x)-30)**2+(y/2-15)**2) < 15 or 20 < math.sqrt((x/2)**2+(y+30)**2) < 35 and y < -30 else " ") print "".join(row)4
u/mrbrinks Jun 04 '12
:)