-
Notifications
You must be signed in to change notification settings - Fork 2
/
spirotri
executable file
·93 lines (87 loc) · 2.68 KB
/
spirotri
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python3
#+
# My attempt to create patterns like those published in
# BYTE Magazine of September 1978, pages 40-46
# <https://archive.org/details/byte-magazine-1978-09>.
# Another potentially interesting one is in the issue
# of November 1982, page 119
# <https://archive.org/details/byte-magazine-1982-11>.
#
# Copyright 2016, 2018 by Lawrence D'Oliveiro <ldo@geek-central.gen.nz>. This
# script is licensed CC0
# <https://creativecommons.org/publicdomain/zero/1.0/>; do with it
# what you will.
#-
import sys
import os
import math
import qahirah as qah
from qahirah import \
CAIRO, \
Colour, \
Matrix, \
Vector
triangle_corners = list(Vector(1, 0).rotate(a / 3 * qah.circle) for a in range(3))
triangle_step = Vector(1.5, math.sqrt(3) / 2)
flip_adjust = Vector(0.5, 0)
pix_size = 700
figure_size = 150
pix = qah.ImageSurface.create \
(
format = CAIRO.FORMAT_ARGB32,
dimensions = round(Vector(1, 1) * pix_size)
)
ctx = \
(qah.Context.create(pix)
.translate(Vector(0.5, 0.5) * pix_size)
.set_source_colour(Colour.grey(1))
.paint()
.set_source_colour(Colour.grey(0))
.set_line_width(2)
)
def draw_cell(ctx, angle, colours) :
with ctx.save_state() :
ctx.transform(Matrix.rotate(angle) * Matrix.scale(figure_size))
scale_step = 0.87
ctx.line_width /= figure_size # compensate for scaling of entire coordinate system
for step in range(15) :
for i in range(3) :
(ctx
.new_path()
.move_to(triangle_corners[i - 1])
.line_to(triangle_corners[i])
.set_source_colour(colours[i])
.stroke()
)
#end for
ctx.transform \
(
Matrix.rotate(5 * qah.deg) * Matrix.scale(scale_step)
)
ctx.line_width /= scale_step
#end for
#end with
#end draw_cell
for row in range(-3, 4) :
for col in range(-2, 2) :
flip = (row ^ col) & 1
with ctx.save_state() :
ctx.translate \
(
(Vector(col, row) * triangle_step + flip_adjust * flip) * figure_size
)
draw_cell \
(
ctx = ctx,
angle = flip * 180 * qah.deg,
colours =
[
Colour.from_hsva((0.55, 0.5, 0.4)),
Colour.from_hsva((0.05, 0.5, 0.4)),
Colour.from_hsva((0.8, 0.5, 0.4)),
]
)
#end with
#end for
#end for
pix.flush().write_to_png("%s.png" % os.path.basename(sys.argv[0]))