def rose(d, n): """ Returns a polyline representing a rose of radius 1 """ n_cycles = 1.0 * d / gcd(n,d) # <- number of cycles to close the rose aa = np.linspace(0, 2 * np.pi * n_cycles, 1000) rr = np.cos( n*aa/d) points = gz.polar2cart(rr, aa) return gz.polyline(points, stroke=[0,0,0], stroke_width=.05)
def make_frame(t): dt = 1.0 * DURATION / 2 / NDISKS_PER_CYCLE # delay between disks N = int(NDISKS_PER_CYCLE / SPEED) # total number of disks t0 = 1.0 / SPEED # indicates at which avancement to start surface = gz.Surface(W, H) for i in range(1, N): a = (np.pi / NDISKS_PER_CYCLE) * (N - i - 1) r = np.maximum(0, .05 * (t + t0 - dt * (N - i - 1))) center = W * (0.5 + gz.polar2cart(r, a)) color = 3 * ((1.0 * i / NDISKS_PER_CYCLE) % 1.0, ) circle = gz.circle(r=0.3 * W, xy=center, fill=color, stroke_width=0.01 * W) circle.draw(surface) contour1 = gz.circle(r=.65 * W, xy=[W / 2, W / 2], stroke_width=.5 * W) contour2 = gz.circle(r=.42 * W, xy=[W / 2, W / 2], stroke_width=.02 * W, stroke=(1, 1, 1)) contour1.draw(surface) contour2.draw(surface) return surface.get_npimage()
def make_frame(t): surface = gz.Surface(W, H, bg_color=(1, 1, 1)) gradient = gz.ColorGradient(type="radial", stops_colors=[(0, (1, 0, 0, alpha)), (1, (0.9, 0, 0, alpha))], xy1=[0.3, -0.3], xy2=[0, 0], xy3=[0, 1.4]) for i in range(numBalls): if (t < duration / 2): newRadius = pytweening.easeInOutBounce(2 * t / duration) * radius newCircleRadius = pytweening.easeInOutBounce( 2 * t / duration) * circleRadius else: newRadius = pytweening.easeInOutBounce(1 - (t / duration)) * radius newCircleRadius = pytweening.easeInOutBounce( 1 - (t / duration)) * circleRadius angle = (2 * np.pi / numBalls) * i #center = (W/2) + gz.polar2cart(newCircleRadius, angle) center = (W / 2) + gz.polar2cart(circleRadius, angle) #ball = gz.circle(r=newRadius, fill=gradient).translate((x, y)) ball = gz.circle(r=newRadius, fill=gradient).translate(center) ball.draw(surface) return surface.get_npimage()
def make_frame(t): dt = 1.0 * DURATION / 2 / NDISKS_PER_CYCLE N = int(NDISKS_PER_CYCLE / SPEED) t0 = 1.0 / SPEED surface = gz.Surface(W, H) for i in range(1, N): a = (np.pi / NDISKS_PER_CYCLE) * (N - i - 1) r = np.maximum(0, 0.05 * (t + t0 - dt * (N - i - 1))) center = W * (0.5 + gz.polar2cart(r, a)) color = 1 * ((1.0 * i / NDISKS_PER_CYCLE) % 1.0, 1, 0) circle = gz.circle(r=0.3 * W, xy=center, fill=color, stroke_width=0.01 * W) circle.draw(surface) contour1 = gz.circle(r=0.65 * W, xy=[W / 2, W / 2], stroke_width=0.5 * W) contour2 = gz.circle(r=0.42 * W, xy=[W / 2, W / 2], stroke_width=0.02 * W, stroke=(1, 1, 1)) contour1.draw(surface) contour2.draw(surface) return surface.get_npimage()
def half(t, side="left"): points = gz.geometry.polar_polygon(NFACES, R, NSQUARES) #ipoint = 0 if side=="left" else NSQUARES/2 #points = (points[ipoint:]+points[:ipoint])[::-1] #for point in points: # print point surface = gz.Surface(W,H) centerPoints = [] for (r, th, d) in points: center = W * (0.5 + gz.polar2cart(r, th)) angle = -(6 * np.pi * d + t * np.pi / DURATION) #color= colorsys.hls_to_rgb((2*d+t/DURATION)%1,.5,.5) centerPoints.append(center) color= (0.9, 0.9, 0.9) square = gz.circle(r=4.0, xy= center, fill=color) #square = gz.square(l=0.17*W, xy= center, angle=angle, # fill=color, stroke_width= 0.005*W, stroke=(1,1,1)) square.draw(surface) line = gz.polyline(points=centerPoints, stroke_width=1, stroke=(1, 0, 0)) line.draw(surface) im = surface.get_npimage() return (im[:,:W/2] if (side=="left") else im[:,W/2:])
def half(t, side="left"): points = gz.geometry.polar_polygon(NFACES, R, NSQUARES) #ipoint = 0 if side=="left" else NSQUARES/2 #points = (points[ipoint:]+points[:ipoint])[::-1] #for point in points: # print point surface = gz.Surface(W, H) centerPoints = [] for (r, th, d) in points: center = W * (0.5 + gz.polar2cart(r, th)) angle = -(6 * np.pi * d + t * np.pi / DURATION) #color= colorsys.hls_to_rgb((2*d+t/DURATION)%1,.5,.5) centerPoints.append(center) color = (0.9, 0.9, 0.9) square = gz.circle(r=4.0, xy=center, fill=color) #square = gz.square(l=0.17*W, xy= center, angle=angle, # fill=color, stroke_width= 0.005*W, stroke=(1,1,1)) square.draw(surface) line = gz.polyline(points=centerPoints, stroke_width=1, stroke=(1, 0, 0)) line.draw(surface) im = surface.get_npimage() return (im[:, :W / 2] if (side == "left") else im[:, W / 2:])
def make_frame(t): surface = gz.Surface(W, H) G = 2**(2 * (t / D)) # zoom coefficient (fractal.translate([R * 2 * (1 - 1.0 / G) / 3, 0]).scale(G) # zoom .translate(W / 2 + gz.polar2cart(W / 12, 2 * np.pi * t / D)) # spiral effect .draw(surface)) return surface.get_npimage()
def make_frame(t): surface = gz.Surface(W, H) G = 2**(2 * (t / D)) (fractal.translate( [R * 2 * (1 - 1.0 / G) / 3, 0]).scale(G).translate(W / 2 + gz.polar2cart(W / 12, 2 * np.pi * t / D)).draw(surface)) return surface.get_npimage()
def make_frame(t): surface = gz.Surface(W,H) for angle in np.linspace(0,2*np.pi,ncircles+1)[:-1]: center = np.array([W/2,H/2]) + gz.polar2cart(.2*W,angle) for i in [0,1]: # two circles belongin to two groups circle = gz.circle(W*.45*(i+t/D),xy=center, fill=(1,1,1,1.0/255)) circle.draw(surface) return 255*((surface.get_npimage()+1) % 2)
def make_frame(t): surface = gz.Surface(W, H) for angle in np.linspace(0, 2 * np.pi, ncircles + 1)[:-1]: center = np.array([W / 2, H / 2]) + gz.polar2cart(.2 * W, angle) for i in [0, 1]: # two circles belongin to two groups circle = gz.circle(W * .45 * (i + t / D), xy=center, fill=(1, 1, 1, 1.0 / 255)) circle.draw(surface) return 255 * ((surface.get_npimage() + 1) % 2)
def polar_ngon_to_cart(nfaces, radius, angle): """ Returns the (x,y) coordinates of n points regularly spaced along a regular polygon of `nfaces` faces and given radius. """ #theta=np.linspace(0,2*np.pi,npoints)[:-1] theta = angle cos, pi, n = np.cos, np.pi, nfaces r = cos( pi / n ) / cos((theta % (2 * pi/n)) - pi/n) #d = np.cumsum(np.sqrt(((r[1:]-r[:-1])**2))) #d = [0] + list( d / d.max()) return gz.polar2cart(radius*r, theta)
def make_frame(t): surface = gizeh.Surface(W,H) for i in range(ncircles): angle = 2*np.pi*(1.0*i/ncircles+t/duration) center = W*(0.5+gizeh.polar2cart(0.1,angle)) circle = gizeh.circle(r=W*(1.0-1.0*i/ncircles), xy=center,fill=(i%2,i%2,i%2)) circle.draw(surface) return surface.get_npimage()
def polar_ngon_to_cart(nfaces, radius, angle): """ Returns the (x,y) coordinates of n points regularly spaced along a regular polygon of `nfaces` faces and given radius. """ #theta=np.linspace(0,2*np.pi,npoints)[:-1] theta = angle cos, pi, n = np.cos, np.pi, nfaces r = cos(pi / n) / cos((theta % (2 * pi / n)) - pi / n) #d = np.cumsum(np.sqrt(((r[1:]-r[:-1])**2))) #d = [0] + list( d / d.max()) return gz.polar2cart(radius * r, theta)
def make_frame(t): surface = gz.Surface(W,H) for r,color, center in zip(radii, colors, centers): angle = 2*np.pi*(t/D*np.sign(color[0]-.5)+color[1]) xy = center+gz.polar2cart(W/5,angle) # center of the ball gradient = gz.ColorGradient(type="radial", stops_colors = [(0,color),(1,color/10)], xy1=[0.3,-0.3], xy2=[0,0], xy3 = [0,1.4]) ball = gz.circle(r=1, fill=gradient).scale(r).translate(xy) ball.draw(surface) return surface.get_npimage()
def make_frame(t): surface = gizeh.Surface(W,H) for i in range(ncircles): angle = 2*np.pi*(1.0*i/ncircles+t/duration) center = W*( 0.5+ gizeh.polar2cart(0.1,angle)) circle = gizeh.circle(r= W*(1.0-1.0*i/ncircles), xy= center, fill= (i%2,i%2,i%2)) circle.draw(surface) return surface.get_npimage()
def draw(self, ctx, anchor_at, width, height, parent_shape): t = self.progress*10 W,H = 256, 256 DURATION = 2.0 NDISKS_PER_CYCLE = 8 SPEED = .05 W,H = int(width), int(height) dt = 1.0*DURATION/2/NDISKS_PER_CYCLE # delay between disks N = int(NDISKS_PER_CYCLE/SPEED) # total number of disks t0 = 1.0/SPEED # indicates at which avancement to start surface = gz.Surface(W,H) for i in range(1,N): a = (math.pi/NDISKS_PER_CYCLE)*(N-i-1) r = max(0, .05*(t+t0-dt*(N-i-1))) center = W*(0.5+ gz.polar2cart(r,a)) color = 3*((1.0*i/NDISKS_PER_CYCLE) % 1.0,) circle = gz.circle(r=0.3*W, xy = center,fill = color, stroke_width=0.01*W) circle.draw(surface) contour1 = gz.circle(r=.65*W,xy=[W/2,W/2], stroke_width=.5*W) contour2 = gz.circle(r=.42*W,xy=[W/2,W/2], stroke_width=.02*W, stroke=(1,1,1)) #contour1.draw(surface) #contour2.draw(surface) cs = surface._cairo_surface ctx.set_source_surface( cairo.ImageSurface.create_for_data( cs.get_data(), cs.get_format(), cs.get_width(), cs.get_height())) ctx.paint() step_angle = math.pi*2./self.circle_count circle_radius = self.circle_radius circle_radius = (.5+self.progress)*circle_radius spread_radius = min(width, height)*.5-circle_radius-self.stroke_size for i in range(self.circle_count): angle = i*step_angle x = spread_radius*math.cos(angle) y = spread_radius*math.sin(angle) ctx.save() ctx.translate(anchor_at.x, anchor_at.y) ctx.new_path() ctx.translate(x, y) ctx.arc(0, 0, circle_radius, 0, 2*math.pi) ctx.restore() draw_stroke(ctx, self.stroke_size, self.stroke_color)
def test_transparent_colors(): L = 200 # <- dimensions of the final picture surface = gz.Surface(L, L, bg_color=(1, 1, 1)) # <- white background radius = 50 centers = [gz.polar2cart(40, angle) for angle in [0, 2 * np.pi / 3, 4 * np.pi / 3]] colors = [(1, 0, 0, .4), # <- Semi-tranparent red (R,G,B, transparency) (0, 1, 0, .4), # <- Semi-tranparent green (0, 0, 1, .4)] # <- Semi-tranparent blue circles = gz.Group([gz.circle(radius, xy=center, fill=color, stroke_width=3, stroke=(0, 0, 0)) for center, color in zip(centers, colors)]) circles.translate([L / 2, L / 2]).draw(surface) assert is_like_sample(surface, 'transparent_colors')
def animation1(t): W,H = 480,480 ncircles = 20 surface = gz.Surface(W,H) for i in range(ncircles): angle = 2*np.pi*(1.0*i/ncircles+t/DURATION) center = W*( 0.5+ gz.polar2cart(0.1,angle)) circle = gz.circle(r= W*(1.0-1.0*i/ncircles), xy= center, fill= (i%2,i%2,i%2)) circle.draw(surface) return surface.get_npimage()
def make_frame(t, speed_list): surface = gizeh.Surface(W, H) max_speed = max(speed_list) c = 2 * np.pi / max_speed # 係数 arc = gizeh.arc(r=R, a1=np.pi, a2=2 * np.pi, xy=(W / 2, H), fill=(1, 1, 1)) arc.draw(surface) for speed in speed_list: line = gizeh.polyline(points=[ (W / 2, H), (W / 2, H) + (gizeh.polar2cart(R, np.pi + c * speed)) ], stroke_width=4, stroke=(1, 0, 0), fill=(0, 1, 0)) line.draw(surface) return surface.get_npimage()
def half(t, side="left"): points = list(gz.geometry.polar_polygon(NFACES, R, NSQUARES)) ipoint = 0 if side == "left" else NSQUARES // 2 points = (points[ipoint:] + points[:ipoint])[::-1] surface = gz.Surface(W, H) for (r, th, d) in points: center = W * (0.5 + gz.polar2cart(r, th)) angle = -(6 * np.pi * d + t * np.pi / DURATION) color = colorsys.hls_to_rgb((2 * d + t / DURATION) % 1, 0.5, 0.5) square = gz.square(l=0.17 * W, xy=center, angle=angle, fill=color, stroke_width=0.005 * W, stroke=(1, 1, 1)) square.draw(surface) im = surface.get_npimage() return (im[:, :W // 2] if side == "left" else im[:, W // 2:])
def animation2(t): W = H = 480 nballs=60 radii = np.random.randint(.1*W,.2*W, nballs) colors = np.random.rand(nballs,3) centers = np.random.randint(0,W, (nballs,2)) surface = gz.Surface(W,H) for r,color, center in zip(radii, colors, centers): angle = 2*np.pi*(t/DURATION*np.sign(color[0]-.5)+color[1]) xy = center+gz.polar2cart(W/5,angle) gradient = gz.ColorGradient(type="radial", stops_colors = [(0,color),(1,color/10)], xy1=[0.3,-0.3], xy2=[0,0], xy3 = [0,1.4]) ball = gz.circle(r=1, fill=gradient).scale(r).translate(xy) ball.draw(surface) return surface.get_npimage()
def make_frame(t): dt = 1.0*DURATION/2/NDISKS_PER_CYCLE # delay between disks N = int(NDISKS_PER_CYCLE/SPEED) # total number of disks t0 = 1.0/SPEED # indicates at which avancement to start surface = gz.Surface(W,H) for i in range(1,N): a = (np.pi/NDISKS_PER_CYCLE)*(N-i-1) r = np.maximum(0, .05*(t+t0-dt*(N-i-1))) center = W*(0.5+ gz.polar2cart(r,a)) color = 3*((1.0*i/NDISKS_PER_CYCLE) % 1.0,) circle = gz.circle(r=0.3*W, xy = center,fill = color, stroke_width=0.01*W) circle.draw(surface) contour1 = gz.circle(r=.65*W,xy=[W/2,W/2], stroke_width=.5*W) contour2 = gz.circle(r=.42*W,xy=[W/2,W/2], stroke_width=.02*W, stroke=(1,1,1)) contour1.draw(surface) contour2.draw(surface) return surface.get_npimage()
def make_frame(t): surface = gz.Surface(R, R) background = gz.square(l=R, xy= [R/2,R/2], fill = (1,1,1,1)) background.draw(surface) r = R/4 ith = -np.pi/2 C = pytweening.easeInOutSine(t/DURATION) # C = t/DURATION for i in range(NCIRCLES): th = ith + (i+1)*(2*np.pi*NROTATIONS) * C center = gz.polar2cart(R/4,th) color = (1.0,1.0,1.0,1.0/255) if i % 3 == 0: color = (1.0,0,0,1.0/255) elif i % 3 == 1: color = (0,1.0,0,1.0/255) else: color = (0,0,1.0,1.0/255) cir = gz.circle(R/4 - (i/4), fill=color, xy=center) cir.translate([R/2,R/2]).draw(surface) im = 255*((surface.get_npimage()) % 2) return im
def make_frame(t): surface = gz.Surface(W,H, bg_color=(1,1,1)) gradient = gz.ColorGradient(type="radial", stops_colors = [(0,(1,0,0, alpha)),(1,(0.9,0,0,alpha))], xy1=[0.3,-0.3], xy2=[0,0], xy3 = [0,1.4]) for i in range(numBalls): if (t < duration/2): newRadius = pytweening.easeInOutBounce(2 * t / duration) * radius newCircleRadius = pytweening.easeInOutBounce(2 * t / duration) * circleRadius else: newRadius = pytweening.easeInOutBounce(1 - (t / duration)) * radius newCircleRadius = pytweening.easeInOutBounce(1 - (t / duration)) * circleRadius angle = (2 * np.pi / numBalls) * i #center = (W/2) + gz.polar2cart(newCircleRadius, angle) center = (W/2) + gz.polar2cart(circleRadius, angle) #ball = gz.circle(r=newRadius, fill=gradient).translate((x, y)) ball = gz.circle(r=newRadius, fill=gradient).translate(center) ball.draw(surface) return surface.get_npimage()
def half(t, side="left"): points = list( gizeh.geometry.polar_polygon( NFACES, amplitude * np.sin(2 * np.pi * frequency * t + phase_shift) + vertical_shift, NSQUARES)) ipoint = 0 if side == "left" else int(NSQUARES / 2) points = (points[ipoint:] + points[:ipoint])[::-1] surface = gizeh.Surface(W, H) for (r, th, d) in points: center = W * (0.5 + gizeh.polar2cart(r, th)) angle = -(6 * np.pi * d + t * np.pi / DURATION) color = hsl_to_rgb((2 * d + t / DURATION) % 1, .5, .5) square = gizeh.square(l=0.17 * W, xy=center, angle=angle, fill=color, stroke_width=0.005 * W, stroke=(1, 1, 1)) square.draw(surface) im = surface.get_npimage() return (im[:, :int(W / 2)] if (side == "left") else im[:, int(W / 2):])
def test_transparent_colors(): L = 200 # <- dimensions of the final picture surface = gz.Surface(L, L, bg_color=(1, 1, 1)) # <- white background radius = 50 centers = [ gz.polar2cart(40, angle) for angle in [0, 2 * np.pi / 3, 4 * np.pi / 3] ] colors = [ (1, 0, 0, .4), # <- Semi-tranparent red (R,G,B, transparency) (0, 1, 0, .4), # <- Semi-tranparent green (0, 0, 1, .4) ] # <- Semi-tranparent blue circles = gz.Group([ gz.circle(radius, xy=center, fill=color, stroke_width=3, stroke=(0, 0, 0)) for center, color in zip(centers, colors) ]) circles.translate([L / 2, L / 2]).draw(surface) assert is_like_sample(surface, 'transparent_colors')
""" This generates a picture of 3 semi-transparent circles of different colors which overlap to some extent. """ import gizeh as gz from numpy import pi # <- 3.14... :) L = 200 # <- dimensions of the final picture surface = gz.Surface(L,L, bg_color=(1,1,1)) # <- white background radius = 50 centers = [ gz.polar2cart(40, angle) for angle in [0, 2*pi/3, 4*pi/3]] colors = [ (1,0,0,.4), # <- Semi-tranparent red (R,G,B, transparency) (0,1,0,.4), # <- Semi-tranparent green (0,0,1,.4)] # <- Semi-tranparent blue circles = gz.Group( [ gz.circle(radius, xy=center, fill=color, stroke_width=3, stroke=(0,0,0)) # black stroke for center, color in zip(centers, colors)] ) circles.translate([L/2,L/2]).draw(surface) surface.write_to_png("transparent_colors.png")
surface = gz.Surface(L,L, bg_color=(1,1,1)) # DRAW THE TEXT txt = gz.text("Gizeh", fontfamily="Dancing Script", fontsize=120, fill=(0,0,0),xy=(L/2,L/9)) txt.draw(surface) # MAKE THE FIRST TRIANGLE gradient=gz.ColorGradient('radial', [(0,(.3,.2,.8)),(1,(.4,.6,.8))], xy1=(0,0), xy2=(0,r/3), xy3=(0,r)) triangle = gz.regular_polygon(r, n=3,fill=gradient, stroke=(0,0,0), stroke_width=3, xy = (r,0)) # BUILD THE FRACTAL RECURSIVELY fractal = gz.Group([triangle.rotate(a) for a in angles]) for i in range(6): fractal = gz.Group([fractal.scale(.5).rotate(-np.pi) .translate(gz.polar2cart(3*r/2,np.pi+a)) for a in angles]+[fractal]) # PLACE AND DRAW THE FRACTAL (this will take time) fractal.rotate(-np.pi/2).translate((L/2,1.1*L/2)).draw(surface) # SAVE surface.write_to_png("logo.png")
# MAKE THE FIRST TRIANGLE gradient = gz.ColorGradient('radial', [(0, (.3, .2, .8)), (1, (.4, .6, .8))], xy1=(0, 0), xy2=(0, r / 3), xy3=(0, r)) triangle = gz.regular_polygon(r, n=3, fill=gradient, stroke=(0, 0, 0), stroke_width=3, xy=(r, 0)) # BUILD THE FRACTAL RECURSIVELY fractal = gz.Group([triangle.rotate(a) for a in angles]) for i in range(6): fractal = gz.Group([ fractal.scale(.5).rotate(-np.pi).translate( gz.polar2cart(3 * r / 2, np.pi + a)) for a in angles ] + [fractal]) # PLACE AND DRAW THE FRACTAL (this will take time) fractal.rotate(-np.pi / 2).translate((L / 2, 1.1 * L / 2)).draw(surface) # SAVE surface.write_to_png("logo.png")
def ray(R, theta, center, **kw): x,y = center dx,dy = gizeh.polar2cart(R, theta) return gizeh.polyline(points=[(x,y), (x+dx,y+dy)], **kw)