plotille:rotating_cube
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
| plotille:rotating_cube [2020/06/16 16:43] – 만듦 rex8312 | plotille:rotating_cube [2024/03/23 02:42] (현재) – 바깥 편집 127.0.0.1 | ||
|---|---|---|---|
| 줄 9: | 줄 9: | ||
| # drawille의 rotating cube를 plotille를 사용하도록 포팅 | # drawille의 rotating cube를 plotille를 사용하도록 포팅 | ||
| - | import sys | ||
| import math | import math | ||
| - | import time | ||
| - | from IPython import embed | ||
| import random | import random | ||
| - | import | + | import |
| + | import threading | ||
| + | import time | ||
| + | from queue import Queue | ||
| + | import numpy as np | ||
| import plotille | import plotille | ||
| + | from IPython import embed | ||
| from plotille import Canvas | from plotille import Canvas | ||
| + | |||
| + | def keyboard(queue): | ||
| + | import platform | ||
| + | |||
| + | if platform.system() == ' | ||
| + | import msvcrt | ||
| + | from msvcrt import kbhit | ||
| + | from msvcrt import getch | ||
| + | | ||
| + | while True: | ||
| + | if msvcrt.kbhit(): | ||
| + | queue.put(msvcrt.getch()) | ||
| + | | ||
| + | elif platform.system() == ' | ||
| + | import curses | ||
| + | import time | ||
| + | |||
| + | stdscr = curses.initscr() | ||
| + | stdscr.refresh() | ||
| + | |||
| + | while True: | ||
| + | queue.put(stdscr.getch()) | ||
| + | | ||
| class Screen: | class Screen: | ||
| 줄 57: | 줄 82: | ||
| def project(self, | def project(self, | ||
| """ | """ | ||
| - | | + | factor = fov / (viewer_distance + self.z) |
| - | y = self.y - 0.5 | + | x = self.x * factor + win_width / 2 |
| - | z = self.z | + | y = -self.y * factor + win_height / 2 |
| - | | + | return Point3D(x / win_height, y / win_width, 1) |
| - | x = x * factor + win_width / 2 | + | |
| - | y = -y * factor + win_height / 2 | + | |
| - | return Point3D(x / win_width, y / win_height, 1) | + | |
| def __repr__(self): | def __repr__(self): | ||
| 줄 111: | 줄 133: | ||
| vs = [ | vs = [ | ||
| - | | + | Point3D(-20, |
| - | | + | Point3D(20, |
| - | | + | Point3D(20, |
| - | | + | Point3D(-20, |
| - | | + | Point3D(-20, |
| - | | + | Point3D(20, |
| - | | + | Point3D(20, |
| - | | + | Point3D(-20, |
| ] | ] | ||
| 줄 137: | 줄 159: | ||
| screen = Screen(height=20, | screen = Screen(height=20, | ||
| screen.clear() | screen.clear() | ||
| - | projection = False | ||
| # 변환 행렬 | # 변환 행렬 | ||
| - | TR = T(0.5, 0.5, 0.5) @ R(2, 3, 5) @ T(-0.5, -0.5, -0.5) | + | TR = R(2, 3, 5) @ T(0, 0, 0) |
| + | |||
| + | key = Queue() | ||
| + | threading.Thread(target=keyboard, | ||
| + | |||
| + | colors = [' | ||
| while True: | while True: | ||
| + | if not key.empty() and key.get() == b' ': | ||
| + | colors = colors[1:] + colors[:1] | ||
| + | | ||
| canvas = Canvas(height=screen.height, | canvas = Canvas(height=screen.height, | ||
| 줄 149: | 줄 178: | ||
| # Rotate the point around X axis, then around Y axis, and finally around Z axis. | # Rotate the point around X axis, then around Y axis, and finally around Z axis. | ||
| v.transform(TR) | v.transform(TR) | ||
| - | | + | # Transform the point from 3D to 2D |
| - | | + | v = v.project( |
| - | v = v.project( | + | win_width=50, |
| - | win_width=50, | + | win_height=50, |
| - | win_height=50, | + | fov=50, |
| - | fov=50, | + | viewer_distance=100 |
| - | viewer_distance=1 | + | ) |
| - | ) | + | |
| t.append(v) | t.append(v) | ||
| for f in faces: | for f in faces: | ||
| - | canvas.line(t[f[0]].x, | + | canvas.line(t[f[0]].x, |
| - | canvas.line(t[f[1]].x, | + | canvas.line(t[f[1]].x, |
| - | canvas.line(t[f[2]].x, | + | canvas.line(t[f[2]].x, |
| - | canvas.line(t[f[3]].x, | + | canvas.line(t[f[3]].x, |
| screen.write(canvas.plot()) | screen.write(canvas.plot()) | ||
| </ | </ | ||
plotille/rotating_cube.1592325826.txt.gz · 마지막으로 수정됨: (바깥 편집)