-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
152 lines (127 loc) · 4.9 KB
/
main.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def save_as_ppm(width, height, pixels: hex, file_name):
buffer = bytearray()
for y in range(height):
for x in range(width):
pixel = pixels[y * width + x]
buffer += bytes(
[
pixel >> 8 * 2 & 0xFF,
pixel >> 8 * 1 & 0xFF,
pixel >> 8 * 0 & 0xFF,
]
)
with open(f"{file_name}.ppm", "wb") as file:
file.write(bytes(f"P6\n{width} {height} 255\n", "ascii"))
file.write(buffer)
print(f"Saved {file_name}.ppm")
def hollow_circle(width, height, pixels, foreground):
r = width // 4
cx = width // 2
cy = height // 2
x = 0
y = r
while x <= y:
px = x + cx
py = y + cy
for b in range(height):
for a in range(width):
if a == px and b == py:
dx = px
dy = py
pixels[dy * width + dx] = foreground
pixels[dx * width + dy] = foreground
pixels[(height - dy) * width + dx] = foreground
pixels[dx * width + (height - dy)] = foreground
pixels[dy * width + (width - dx)] = foreground
pixels[(width - dx) * width + dy] = foreground
pixels[(height - dy) * width + (width - dx)] = foreground
pixels[(width - dx) * width + (height - dy)] = foreground
x += 1
if x * x + y * y > r * r:
y -= 1
save_as_ppm(width, height, pixels, "hollow")
def circle(width, height, pixels, foreground, background):
cx = width // 2
cy = height // 2
r = cx // 2
for y in range(height):
for x in range(width):
if (
(x + 0.5 - cx) * (x + 0.5 - cx)
+ (y + 0.5 - cy) * (y + 0.5 - cy)
) <= r * r:
pixels[y * width + x] = foreground
else:
pixels[y * width + x] = background
save_as_ppm(width, height, pixels, "circle")
def four_dimensional(width, height, pixels):
for y in range(height):
for x in range(width):
pixels[y * width + x] = (x * y) * 0x010101
save_as_ppm(width, height, pixels, "four_dimensional")
def wee_wee(width, height, pixels):
for y in range(height):
for x in range(width):
pixels[y * width + x] = (
((x < 160) * (x > 96) * (y < 196) * (y > 64))
+ (((x - 128) * (x - 128) + (y - 64) * (y - 64)) < 32 * 32)
+ (((x - 96) * (x - 96) + (y - 196) * (y - 196)) < 32 * 32)
+ (((x - 160) * (x - 160) + (y - 196) * (y - 196)) < 32 * 32)
> 0
) * 0x634D84
save_as_ppm(width, height, pixels, "wee_wee")
def wee_wee_with_head(width, height, pixels):
for y in range(height):
for x in range(width):
pixels[y * width + x] = (
((x < 160) * (x > 96) * (y < 196) * (y > 64))
+ (((x - 128) * (x - 128) + (y - 64) * (y - 64)) < 48 * 48)
+ (((x - 96) * (x - 96) + (y - 196) * (y - 196)) < 32 * 32)
+ (((x - 160) * (x - 160) + (y - 196) * (y - 196)) < 32 * 32)
> 0
) * 0x634D84
save_as_ppm(width, height, pixels, "wee_wee_with_head")
def checker_pattern(
pixels: hex, height, width, tile_size, background, foreground
):
for y in range(height):
for x in range(width):
if (x // tile_size + y // tile_size) % 2 == 0:
pixels[y * width + x] = background
else:
pixels[y * width + x] = foreground
save_as_ppm(width, height, pixels, "checker")
def stripes_pattern(
pixels: hex, height, width, tile_size, background, foreground
):
for y in range(height):
for x in range(width):
pixels[y * width + x] = (
background if ((x + y) // tile_size) % 2 == 0 else foreground
)
save_as_ppm(width, height, pixels, "stripes")
def uv_gradient_pattern(height, width, pixels):
for y in range(height):
for x in range(width):
u = x / width
v = y / height
r = int(u * 255)
g = int(v * 255)
pixels[y * width + x] = (r << 8 * 2) | (g << 8 * 1)
save_as_ppm(width, height, pixels, "uv_gradient")
def main():
width = 256
height = 256
color = 0x000000
pixels = [color] * width * height
foreground = 0x634D84
background = 0x000000
hollow_circle(width, height, pixels, foreground)
checker_pattern(pixels, width, height, width // 16, foreground, background)
stripes_pattern(pixels, width, height, width // 16, foreground, background)
wee_wee(width, height, pixels)
wee_wee_with_head(width, height, pixels)
four_dimensional(width, height, pixels)
circle(width, height, pixels, foreground, background)
uv_gradient_pattern(height, width, pixels)
main()