Skip to content

Instantly share code, notes, and snippets.

@vegard
Last active December 1, 2016 22:39
Show Gist options
  • Select an option

  • Save vegard/929a74bbfe862c7a1e0f61e4d2bbceb0 to your computer and use it in GitHub Desktop.

Select an option

Save vegard/929a74bbfe862c7a1e0f61e4d2bbceb0 to your computer and use it in GitHub Desktop.

Revisions

  1. vegard revised this gist Dec 1, 2016. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions art.py
    Original file line number Diff line number Diff line change
    @@ -53,8 +53,7 @@ def draw_triangle(c, a):
    for i in range(nr_triangles):
    t = 1. * i / nr_triangles
    c1 = circle((.5, .5), .3, 360. * t)
    c2 = circle((0, 0), .1, 360. * t * 4)
    draw_triangle(add(c1, c2), i * 4 * 3 + frame * (120. / nr_frames))
    draw_triangle(c1, 15 * math.sin(3 * math.pi * i / nr_frames) + frame * (120. / nr_frames))

    #ctx.set_source_rgba(0, 0, 0, math.sin(1. * i * math.pi / nr_triangles))
    ctx.set_source_rgb(0, 0, 0)
  2. vegard revised this gist Dec 1, 2016. 1 changed file with 31 additions and 17 deletions.
    48 changes: 31 additions & 17 deletions art.py
    Original file line number Diff line number Diff line change
    @@ -18,37 +18,51 @@
    nr_frames = 30
    nr_triangles = 90

    p0 = (.5 + .4 * math.cos(0 * 120 * math.pi / 180.), .5 + .4 * math.sin(0 * 120 * math.pi / 180.))
    p1 = (.5 + .4 * math.cos(1 * 120 * math.pi / 180.), .5 + .4 * math.sin(1 * 120 * math.pi / 180.))
    p2 = (.5 + .4 * math.cos(2 * 120 * math.pi / 180.), .5 + .4 * math.sin(2 * 120 * math.pi / 180.))

    def circle(c, r, a):
    x = c[0] + r * math.cos(a * math.pi / 180.)
    y = c[1] + r * math.sin(a * math.pi / 180.)
    return (x, y)

    def linear(a, b, t):
    ti = 1 - t
    x = a[0] * t + b[0] * ti
    y = a[1] * t + b[1] * ti
    return (x, y)

    def add(a, b):
    x = a[0] + b[0]
    y = a[1] + b[1]
    return (x, y)

    for frame in range(nr_frames):
    ctx.set_source_rgb(1, 1, 1)
    ctx.rectangle(0, 0, 1, 1)
    ctx.fill()

    def draw_triangle(cx, cy, a):
    def draw_triangle(c, a):
    r = .1

    a0 = a + 0 * 120
    a1 = a + 1 * 120
    a2 = a + 2 * 120

    ctx.move_to(cx + r * math.cos(a0 * math.pi / 180.), cy + r * math.sin(a0 * math.pi / 180.))
    ctx.line_to(cx + r * math.cos(a1 * math.pi / 180.), cy + r * math.sin(a1 * math.pi / 180.))
    ctx.line_to(cx + r * math.cos(a2 * math.pi / 180.), cy + r * math.sin(a2 * math.pi / 180.))
    ctx.move_to(*circle(c, r, a + 0 * 120))
    ctx.line_to(*circle(c, r, a + 1 * 120))
    ctx.line_to(*circle(c, r, a + 2 * 120))
    ctx.close_path()

    for i in range(nr_triangles):
    #cx = .5 + .4 * math.cos(3 * 4 * (i / 6.) * math.pi / 180.)
    #cy = .5 + .4 * math.sin(2 * 4 * (i / 6.) * math.pi / 180.)
    cx = .1 + .8 * i / nr_triangles
    cy = .5 + .25 * math.sin(4 * i * math.pi / 180.)

    draw_triangle(cx, cy, i * 4 * 3 + frame * (120. / nr_frames))
    t = 1. * i / nr_triangles
    c1 = circle((.5, .5), .3, 360. * t)
    c2 = circle((0, 0), .1, 360. * t * 4)
    draw_triangle(add(c1, c2), i * 4 * 3 + frame * (120. / nr_frames))

    ctx.set_source_rgba(0, 0, 0, math.sin(1. * i * math.pi / nr_triangles))
    #ctx.set_source_rgba(0, 0, 0, math.sin(1. * i * math.pi / nr_triangles))
    ctx.set_source_rgb(0, 0, 0)
    ctx.set_line_width(0.003)
    ctx.stroke()

    data = surface.get_data()[:]
    im = PIL.Image.frombuffer('RGBA', (WIDTH, HEIGHT), data, 'raw', 'RGBA', 0, 1)
    ims.append(im)

    ims[0].save('example.gif', save_all=True, append_images=list(ims[1:]), loop=0, duration=30)
    ims[0].save('example.gif', save_all=True, append_images=list(ims[1:]), loop=0, duration=30)
  3. vegard created this gist Dec 1, 2016.
    54 changes: 54 additions & 0 deletions art.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    import math
    import os
    import sys

    import PIL
    import PIL.Image
    import cairo

    WIDTH, HEIGHT = 512, 512

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
    ctx = cairo.Context(surface)

    ctx.scale(WIDTH, HEIGHT)

    ims = []

    nr_frames = 30
    nr_triangles = 90

    for frame in range(nr_frames):
    ctx.set_source_rgb(1, 1, 1)
    ctx.rectangle(0, 0, 1, 1)
    ctx.fill()

    def draw_triangle(cx, cy, a):
    r = .1

    a0 = a + 0 * 120
    a1 = a + 1 * 120
    a2 = a + 2 * 120

    ctx.move_to(cx + r * math.cos(a0 * math.pi / 180.), cy + r * math.sin(a0 * math.pi / 180.))
    ctx.line_to(cx + r * math.cos(a1 * math.pi / 180.), cy + r * math.sin(a1 * math.pi / 180.))
    ctx.line_to(cx + r * math.cos(a2 * math.pi / 180.), cy + r * math.sin(a2 * math.pi / 180.))
    ctx.close_path()

    for i in range(nr_triangles):
    #cx = .5 + .4 * math.cos(3 * 4 * (i / 6.) * math.pi / 180.)
    #cy = .5 + .4 * math.sin(2 * 4 * (i / 6.) * math.pi / 180.)
    cx = .1 + .8 * i / nr_triangles
    cy = .5 + .25 * math.sin(4 * i * math.pi / 180.)

    draw_triangle(cx, cy, i * 4 * 3 + frame * (120. / nr_frames))

    ctx.set_source_rgba(0, 0, 0, math.sin(1. * i * math.pi / nr_triangles))
    ctx.set_line_width(0.003)
    ctx.stroke()

    data = surface.get_data()[:]
    im = PIL.Image.frombuffer('RGBA', (WIDTH, HEIGHT), data, 'raw', 'RGBA', 0, 1)
    ims.append(im)

    ims[0].save('example.gif', save_all=True, append_images=list(ims[1:]), loop=0, duration=30)