entry #1
written by essaie
submitted at
0 likes
guesses
- LyricLy (by taswelll)
- essaie (by LyricLy)
- essaie (by Dolphy)
- essaie (by luatic)
- essaie (by kimapr)
comments 0
three-dimensional_rendering.py ASCII text, with CRLF line terminators
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | from turtle import Turtle # We will be using turtle to draw! from turtle import Screen # :D import math import time #import space # delete later (they mustn't learn our secrets) dot_product = lambda v1, v2: sum(map(lambda v1i, v2i: v1i * v2i, v1, v2)) vector_add = lambda v1, v2: list(map(lambda v1i, v2i: v1i + v2i, v1, v2)) magnitude = lambda v: math.sqrt(dot_product(v, v)) # Euclidean norm! direction = lambda v: (lambda r: [vi / r if r else 0 for vi in v]) (magnitude(v)) ## --- Defining useful classes --- ## class Vertex: def __init__(self, loc): # List of numbers self.loc = loc self.dim = len(loc) def copy(self): return Vertex(self.loc) def rotate(self, rotation, center): # Assume rotation is a square matrix targ = len(rotation) truncated = self.dim > targ if truncated: ws = self.loc[:targ] else: ws = self.loc + [0] * (targ - self.dim) if center.dim > targ: wc = center.loc[:targ] else: wc = center.loc + [0] * (targ - center.dim) pulled = vector_add( ws, [-wci for wci in wc] ) rotated = [dot_product(pulled, ro) for ro in rotation] pushed = vector_add(rotated, wc) if truncated: self.loc = pushed + self.loc[targ:] else: self.loc = pushed self.dim = max(self.dim, targ) def translate(self, translation): if self.dim < translation.dim: ws = self.loc + [0] * (translation.dim - self.dim) wt = translation.loc else: ws = self.loc wt = translation.loc + [0] * (self.dim - translation.dim) self.loc = vector_add(ws, wt) def stretch(self, scale, center): # Could be done with a matrix also if self.dim < center.dim: ws = self.loc + [0] * (center.dim - self.dim) wc = center.loc else: ws = self.loc wc = center.loc + [0] * (self.dim - center.dim) radius = vector_add( ws, [-wci for wci in wc] ) self.loc = vector_add( wc, [scale * ri for ri in radius] ) def flatten(self): if self.dim <3 or self.loc[2] == 0: sign = 0 else: sign = -.02 if self.loc[2] < 0 else .02 # Magic number :P # Funny perspective simulation scale = math.exp(sign * math.sqrt(magnitude(self.loc[2:]))) x_funny = sum(p * (-1 if i % 4 < 2 else 1) / math.sqrt(i + 3) for i, p in enumerate(self.loc[3:])) y_funny = sum(p * (-1 if (i + 1) % 4 < 2 else 1) / math.sqrt(i + 3) for i, p in enumerate(self.loc[3:])) #return (self.loc[0], self.loc[1]) # Uncomment this to get isometric stuff! return (scale * (self.loc[0] + x_funny), scale * (self.loc[1] + y_funny)) class Edge: def __init__(self, head, tail): # Two Vertex objects self.head = head self.tail = tail def copy(self): return Edge(self.head.copy(), self.tail.copy()) def rotate(self, rotation, center): self.head.rotate(rotation, center) self.tail.rotate(rotation, center) def translate(self, translation): self.head.translate(translation) self.tail.translate(translation) def stretch(self, scale, center): self.head.stretch(scale, center) self.tail.stretch(scale, center) def flatten(self): return (self.head.flatten(), self.tail.flatten()) class EdgeBunch: def __init__(self, edges, center): # List of Edge objects self.lines = edges self.center = center self.verts = set([]) for l in edges: self.verts.add(l.head) self.verts.add(l.tail) def copy(self): return EdgeBunch([l.copy() for l in self.lines], self.center.copy()) def rotate(self, rotation, center): for line in self.lines: line.rotate(rotation, center) self.center.rotate(rotation, center) def translate(self, translation): for line in self.lines: line.translate(translation) self.center.translate(translation) def stretch(self, scale, center): for line in self.lines: line.stretch(scale, center) self.center.stretch(scale, center) def flatten(self): return (line.flatten() for line in self.lines) # Iterator is easier later! def absorb(self, other): for line in other.lines: self.lines.append(line.copy()) class Programme: def __init__(self, cast, script): self.cast = cast # List of EdgeBunch objects self.script = script # List of tuples def do(self): for who, action, args, reps in self.script: thing = self.cast[who] call = len(args) for i in range(reps): time.sleep(.05) t.clear() if call == 2: eval("thing." + action + "(args[0], args[1])") elif call == 3: eval("thing." + action + "(args[0], args[1], args[2])") else: raise ValueError() # I really can't be bothered draw(t, thing) # see? ## --- Defining useful functions --- ## # These ones are the shape-makers def regular_polygon(sides, radius, center): first = Vertex([0, radius]) first.translate(center) points = [first] angle = 2 * math.pi / sides turn = [[math.cos(angle), -math.sin(angle)], [math.sin(angle), math.cos(angle)]] for i in range(sides - 1): another = points[-1].copy() another.rotate(turn, center) points.append(another) return EdgeBunch([Edge(points[i - 1], points[i]) for i in range(sides)], center.copy()) def new_simplex(radius, center, dim): # May not actually, er, be centered at the center :P if dim == 2: return regular_polygon(3, radius, center.copy()) prev = new_simplex(radius * math.sqrt(1 - (1 / dim)**2), center, dim - 1) depth = [0] * (dim - 1) + [- 1 / dim] prev.translate(Vertex(depth)) addition = Vertex(depth[ :-1 ] + [radius]) lines = [] for line in prev.lines: lines.append(line) for point in prev.verts: lines.append(Edge(addition, point)) return EdgeBunch(lines, center.copy()) def new_cube(radius, center, dim): if dim == 2: return regular_polygon(4, radius, center.copy()) halfside = radius / math.sqrt(dim) #print(radius, halfside) near = new_cube(halfside * math.sqrt(dim - 1), center, dim - 1) far = near.copy() depth = [0] * (dim - 1) near.translate(Vertex(depth + [halfside/2])) # This doesn't make sense far.translate(Vertex(depth + [-halfside])) lines = [] for nline, fline in zip(near.lines, far.lines): lines.append(Edge(nline.tail.copy(), fline.tail.copy())) lines.append(nline.copy()) lines.append(fline.copy()) frame = EdgeBunch(list(set(lines)), center.copy()) return frame def new_orthoplex(radius, center, dim): if dim == 2: return regular_polygon(4, radius, center) base = new_orthoplex(radius, center, dim - 1) above = Vertex([0] * (dim - 1) + [radius]) below = Vertex([0] * (dim - 1) +[-radius]) lines = [] for vert in base.verts: lines.append(Edge(above.copy(), vert.copy())) lines.append(Edge(below.copy(), vert.copy())) pot = EdgeBunch(lines, center.copy()) pot.absorb(base) return pot def tetrahedron(radius, center): return new_simplex(radius, center, 3) def hexahedron(radius, center): return new_cube(radius, center, 3) def octahedron(radius, center): return new_orthoplex(radius, center, 3) def icosahedron(radius, center): phi = (1 + math.sqrt(5)) / 2 pent_rad = math.sqrt((5 + math.sqrt(5)) / 10) * radius top = regular_polygon(5, pent_rad, center) bottom = regular_polygon(5, pent_rad, center) # (; angle = 9 * math.pi / 10 turn = [[math.cos(angle), -math.sin(angle)], [math.sin(angle), math.cos(angle)]] bottom.rotate(turn, center) # ;) top.translate(Vertex([0, 0, radius * math.sqrt(3) / 8])) bottom.translate(Vertex([0, 0, -radius * math.sqrt(3) / 8])) crown = Vertex([0, 0, radius * math.sqrt(phi**2 + 1) / 2]) boots = Vertex([0, 0,-radius * math.sqrt(phi**2 + 1) / 2]) lines = [] for tline, bline in zip(top.lines, bottom.lines): lines.append(tline.copy()) lines.append(bline.copy()) lines.append(Edge(tline.head.copy(), bline.head.copy())) lines.append(Edge(tline.head.copy(), bline.tail.copy())) lines.append(Edge(tline.head.copy(), crown.copy())) lines.append(Edge(bline.tail.copy(), boots.copy())) return EdgeBunch(lines, center.copy()) def dodecahedron(radius, center): # Bleh. pass def pentachoron(radius, center): return new_simplex(radius, center, 4) def octachoron(radius, center): return new_cube(radius, center, 4) def hexadecachoron(radius, center): return new_orthoplex(radius, center, 4) def icositetrachoron(radius, center): pass def dodecacontachoron(radius, center): # Sorry, 120-cell. pass def hexacosichoron(radius, center): pass # These ones are the water closets def draw_line(t, line, color=None): head, tail = line if color: t.pencolor(color) t.pu() t.goto(head[0], head[1]) t.pd() t.goto(tail[0], tail[1]) def draw(t, edgebunch, color=None): for line in edgebunch.flatten(): draw_line(t, line, color) def next_render(x, y): global track_number, programmes track_number = (track_number + 1) % len(programmes) def prev_render(x, y): global track_number, programmes track_number = (track_number - 1) % len(programmes) ## --- Starting the Turtle stuff --- ## s = Screen() H = s.window_height() W = s.window_width() s.setworldcoordinates(- W / 2, - H / 2, W / 2, H / 2) s.delay(0) t = Turtle() t.speed(0) t.ht() s.title("Click to change model! Escape to escape!") s.onkey(s.bye, "Escape") s.onclick(next_render, btn=1) s.onclick(prev_render, btn=2) s.listen() ## --- Everything we will be showcasing! --- ## track_number = 0 programmes = [] origin = Vertex([0, 0]) lpoint = Vertex([-75, 0]) rpoint = Vertex([0, 75]) test_rotat = [ [math.cos(math.pi/58), 0, math.sin(math.pi/58)], [ 0, 1, 0], [-math.sin(math.pi/58), 0, math.cos(math.pi/58)] ] fast_rotat = [ [math.cos(math.pi/18), 0, math.sin(math.pi/18)], [ 0, 1, 0], [-math.sin(math.pi/18), 0, math.cos(math.pi/18)] ] oter_rotat = [ [math.cos(math.pi/48), -math.sin(math.pi/48), 0], [math.sin(math.pi/48), math.cos(math.pi/48), 0], [ 0, 0, 1] ] four_rotat = [ [math.cos(math.pi/58), 0, -math.sin(math.pi/58), 0], [0, math.cos(math.pi/38), 0, -math.sin(math.pi/38)], [math.sin(math.pi/58), 0, math.cos(math.pi/58), 0], [0, math.sin(math.pi/38), 0, math.cos(math.pi/38)], ] square_rotating = Programme([regular_polygon(4, 300, origin)], [(0, 'rotate', [oter_rotat, origin], 1)]) square_3d = Programme([regular_polygon(4, 300, origin)], [(0, 'rotate', [test_rotat, origin], 1)]) funny_square = regular_polygon(4, 150/math.sqrt(2), lpoint) funny_square.rotate([[math.cos(math.pi/8),-math.sin(math.pi/8)],[math.sin(math.pi/8),math.cos(math.pi/8)]], lpoint) funny_rotate = Programme([funny_square], [ (0, 'rotate', [fast_rotat, rpoint], 7), (0, 'rotate', [fast_rotat, lpoint], 7)]) boring_tetra = Programme([new_simplex(200, origin, 3)], [(0, 'rotate', [test_rotat, origin], 1)]) fun_cube = Programme([icosahedron(200, origin)], [(0, 'rotate', [test_rotat, origin], 1)]) actual_cube = Programme([new_cube(200, origin, 3)], [(0, 'rotate', [test_rotat, origin], 1), (0, 'rotate', [oter_rotat, origin], 1)]) roll_d8 = Programme([new_orthoplex(200, origin, 3)], [(0, 'rotate', [test_rotat, origin], 1), (0, 'rotate', [oter_rotat, origin], 1)]) guess_what = Programme([new_cube(200, origin, 4)], [(0, 'rotate', [four_rotat, origin], 1), (0, 'rotate', [oter_rotat, origin], 1)]) i_am_going_insane = Programme([new_simplex(200, origin, 4)], [(0, 'rotate', [four_rotat, origin], 1), (0, 'rotate', [oter_rotat, origin], 1)]) #please_send_help = Programme([new_orthoplex(200, origin, 4)], [(0, 'rotate', [four_rotat, origin], 1), (0, 'rotate', [oter_rotat, origin], 1)]) # Lol this one isn't working nicely programmes = [ square_rotating, square_3d, funny_rotate, boring_tetra, fun_cube, actual_cube, roll_d8, guess_what, i_am_going_insane, #please_send_help, ] while True: programmes[track_number].do() # With some modification, you can easily make your own programmes! # There's room available for shows with multiple actors, if you can get # turtle drawing fast enough :3 |
post a comment