def xtest_bezier2(self): b = bezier_quad(pts=([0], [4], [4])) print b print b.straight_enough(0.1) b0 = bezier_quad(split_parent=b, first_split=True) print b0 print b0.straight_enough(0.1) b1 = bezier_quad(split_parent=b, first_split=False) print b1 print b1.straight_enough(0.1) print b.break_into_segments(0.1) b = bezier_quad(pts=([0, 0], [0, 100], [50, 0])) print b print b.straight_enough(0.1) b0 = bezier_quad(split_parent=b, first_split=True) print b0 print b0.straight_enough(0.1) b1 = bezier_quad(split_parent=b, first_split=False) print b1 print b1.straight_enough(0.1) print b.break_into_segments(1) d = drawing.draw_buffer(mode="1", size=(50, 50)) lines = [] for bez in b.break_into_segments(1): bez.draw_in_lines(lambda x, y: lines.append((x, y))) pass for l in lines: ln = drawing.line(l[0][0], l[0][1], l[1][0], l[1][1]) d.draw_line(ln, value=255) pass b.draw_in_dots(lambda pt: d.putpixel(pt[0], pt[1] + 4)) print d pass
def xtest_bezier2(self): b = bezier_quad(pts=([0],[4],[4])) print b print b.straight_enough(0.1) b0 = bezier_quad(split_parent=b,first_split=True) print b0 print b0.straight_enough(0.1) b1 = bezier_quad(split_parent=b,first_split=False) print b1 print b1.straight_enough(0.1) print b.break_into_segments(0.1) b = bezier_quad(pts=([0,0],[0,100],[50,0])) print b print b.straight_enough(0.1) b0 = bezier_quad(split_parent=b,first_split=True) print b0 print b0.straight_enough(0.1) b1 = bezier_quad(split_parent=b,first_split=False) print b1 print b1.straight_enough(0.1) print b.break_into_segments(1) d = drawing.draw_buffer(mode="1",size=(50,50)) lines = [] for bez in b.break_into_segments(1): bez.draw_in_lines(lambda x,y:lines.append((x,y))) pass for l in lines: ln = drawing.line(l[0][0],l[0][1],l[1][0],l[1][1]) d.draw_line(ln,value=255) pass b.draw_in_dots(lambda pt:d.putpixel(pt[0],pt[1]+4)) print d pass
def xtest_tiny_diag_fill_rings(self): d = drawing.draw_buffer(mode="1",size=(8,8)) p = [] for i in [1,2,3,4]: p.append( ( (i-0.1, i-0.1), (i-0.1, 8.1-i), (8.1-i, 8.1-i), (8.1-i, i-0.1)) ) pass d.fill_paths( p, value=64 ) return self.check_buffer(d,[' ', '....... ', '. . ', '. ... . ', '. . . . ', '. ... . ', '. . ', '....... ', ''])
def main(): size = 200 size = size/2 scale = 1.0/size scale *= 3.0 color_by_direction = False #color_by_direction = True squash_by_modulus = False #squash_by_modulus = True differentiate = False #differentiate = True suffix = "" if differentiate: suffix="_d" d = drawing.draw_buffer(mode="RGB",size=(2*size+1,2*size+1)) imaginary = complex.complex(imaginary=1) m_min, m_max, p = [0,2.2,polynomial.polynomial([1,0,2,1])] m_min, m_max, p = [0,2.2,polynomial.polynomial([1,0,imaginary*2,1])] m_min, m_max, p = [0,1.9,polynomial.polynomial([0,-0.1,imaginary*2,-imaginary,0,0.25,0.1,imaginary*0.02+0.01,imaginary*0.4])] if differentiate: p = p.differentiate() m_min, m_max = [0,3.5] m_min, m_max = [0,5.3] m_range = m_max-m_min new_min=1E9 new_max=-1E9 max_power = len(p.coeffs()) for r in range(-size,size+1): x = complex.complex(real=r)*scale for i in range(-size,size+1): z = x+imaginary*i*scale fz = p.evaluate(z) (m,th) = fz.polar() if squash_by_modulus: m /= 1+pow(z.modulus(),max_power) new_min = min(m,new_min) new_max = max(m,new_max) m = (m-m_min)/m_range if color_by_direction: m = 0.3+m*0.7 th = 0.5+th/math.pi th = 0.5*th d.putpixel(r+size,i+size,drawing.hsv(th,m,m)) pass else: if (m<0):m=0 if (m>1):m=1 d.putpixel(r+size,i+size,drawing.hsv(m,1,1)) pass pass pass print new_min, new_max if color_by_direction: d.save("polynomial%s_direction.png"%suffix) pass else: d.save("polynomial%s_modulus.png"%suffix) pass pass
def test_tiny_diag_dl(self): d = drawing.draw_buffer(mode="1",size=(4,4)) for (l,v) in [((4,0,0,4),64), ((3,0,0,3), 255 ), ((2,0,0,2), 128 ), ((1,0,0,1), 192 ), ]: d.draw_line( drawing.line(l[0],l[1],l[2],l[3]), value=v ) pass return self.check_buffer(d,[' *.*', '*.*.', '.*. ', '*. ', ''])
def test_tiny_horiz(self): d = drawing.draw_buffer(mode="1",size=(4,4)) for (l,v) in [((0,0,4,0),64), ((0,1,4,1), 255 ), ((0,2,4,2), 128 ), ((0,3,4,3), 192 ), ]: d.draw_line( drawing.line(l[0],l[1],l[2],l[3]), value=v ) pass return self.check_buffer(d,['....', '****', '....', '****', ''])
def test_tiny_diag_dl(self): d = drawing.draw_buffer(mode="1", size=(4, 4)) for (l, v) in [ ((4, 0, 0, 4), 64), ((3, 0, 0, 3), 255), ((2, 0, 0, 2), 128), ((1, 0, 0, 1), 192), ]: d.draw_line(drawing.line(l[0], l[1], l[2], l[3]), value=v) pass return self.check_buffer(d, [' *.*', '*.*.', '.*. ', '*. ', ''])
def test_tiny_horiz(self): d = drawing.draw_buffer(mode="1", size=(4, 4)) for (l, v) in [ ((0, 0, 4, 0), 64), ((0, 1, 4, 1), 255), ((0, 2, 4, 2), 128), ((0, 3, 4, 3), 192), ]: d.draw_line(drawing.line(l[0], l[1], l[2], l[3]), value=v) pass return self.check_buffer(d, ['....', '****', '....', '****', ''])
def xtest_tiny_diag_fill_rings(self): d = drawing.draw_buffer(mode="1", size=(8, 8)) p = [] for i in [1, 2, 3, 4]: p.append(((i - 0.1, i - 0.1), (i - 0.1, 8.1 - i), (8.1 - i, 8.1 - i), (8.1 - i, i - 0.1))) pass d.fill_paths(p, value=64) return self.check_buffer(d, [ ' ', '....... ', '. . ', '. ... . ', '. . . . ', '. ... . ', '. . ', '....... ', '' ])
def xtest_tiny_diag_fill_center(self): d = drawing.draw_buffer(mode="1", size=(4, 4)) d.fill_paths([ ( (0.9, 0.9), (0.9, 2.1), (2.1, 2.1), (2.1, 0.9), ), ], value=255) return self.check_buffer(d, [' ', '** ', '** ', ' ', ''])
def xtest_tiny_diag_fill_center(self): d = drawing.draw_buffer(mode="1",size=(4,4)) d.fill_paths( [((0.9,0.9), (0.9,2.1), (2.1,2.1), (2.1,0.9),), ],value=255 ) return self.check_buffer(d,[' ', '** ', '** ', ' ', ''])
def test_tiny_dot(self): d = drawing.draw_buffer(mode="1",size=(4,4)) d.putpixel(1,1) return self.check_buffer(d,[' ', ' * ', ' ', ' ', ''])
def test_from_image(self): import PIL.Image a = PIL.Image.new(mode="RGB",size=(20,20)) d = drawing.draw_buffer(image=a) self.assertEqual(a,d.get_image(),'Images do not match') pass
def test_tiny_dot(self): d = drawing.draw_buffer(mode="1", size=(4, 4)) d.putpixel(1, 1) return self.check_buffer(d, [' ', ' * ', ' ', ' ', ''])
def test_from_image(self): import PIL.Image a = PIL.Image.new(mode="RGB", size=(20, 20)) d = drawing.draw_buffer(image=a) self.assertEqual(a, d.get_image(), 'Images do not match') pass
def main(): size = 200 size = size / 2 scale = 1.0 / size scale *= 3.0 color_by_direction = False #color_by_direction = True squash_by_modulus = False #squash_by_modulus = True differentiate = False #differentiate = True suffix = "" if differentiate: suffix = "_d" d = drawing.draw_buffer(mode="RGB", size=(2 * size + 1, 2 * size + 1)) imaginary = complex.complex(imaginary=1) m_min, m_max, p = [0, 2.2, polynomial.polynomial([1, 0, 2, 1])] m_min, m_max, p = [0, 2.2, polynomial.polynomial([1, 0, imaginary * 2, 1])] m_min, m_max, p = [ 0, 1.9, polynomial.polynomial([ 0, -0.1, imaginary * 2, -imaginary, 0, 0.25, 0.1, imaginary * 0.02 + 0.01, imaginary * 0.4 ]) ] if differentiate: p = p.differentiate() m_min, m_max = [0, 3.5] m_min, m_max = [0, 5.3] m_range = m_max - m_min new_min = 1E9 new_max = -1E9 max_power = len(p.coeffs()) for r in range(-size, size + 1): x = complex.complex(real=r) * scale for i in range(-size, size + 1): z = x + imaginary * i * scale fz = p.evaluate(z) (m, th) = fz.polar() if squash_by_modulus: m /= 1 + pow(z.modulus(), max_power) new_min = min(m, new_min) new_max = max(m, new_max) m = (m - m_min) / m_range if color_by_direction: m = 0.3 + m * 0.7 th = 0.5 + th / math.pi th = 0.5 * th d.putpixel(r + size, i + size, drawing.hsv(th, m, m)) pass else: if (m < 0): m = 0 if (m > 1): m = 1 d.putpixel(r + size, i + size, drawing.hsv(m, 1, 1)) pass pass pass print new_min, new_max if color_by_direction: d.save("polynomial%s_direction.png" % suffix) pass else: d.save("polynomial%s_modulus.png" % suffix) pass pass