사용자 도구

사이트 도구


plotille:rotating_cube

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
plotille:rotating_cube [2020/06/16 16:43] – 만듦 rex8312plotille: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 numpy as np+import sys 
 +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() == 'Windows':
 +        import msvcrt
 +        from msvcrt import kbhit
 +        from msvcrt import getch
 +    
 +        while True:
 +            if msvcrt.kbhit():
 +                queue.put(msvcrt.getch())
 +    
 +    elif platform.system() == 'Linux':
 +        import curses
 +        import time
 +
 +        stdscr = curses.initscr()
 +        stdscr.refresh()
 +
 +        while True:
 +            queue.put(stdscr.getch())
 +    
  
 class Screen: class Screen:
줄 57: 줄 82:
     def project(self, win_width, win_height, fov, viewer_distance):     def project(self, win_width, win_height, fov, viewer_distance):
         """ Transforms this 3D point to 2D using a perspective projection. """         """ Transforms this 3D point to 2D using a perspective projection. """
-        x = self.x - 0.5 +        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 
-        factor = fov / (viewer_distance + z) +        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(0.25, 0.75, 0.25), Point3D(-20,20,-20), +    Point3D(-20,20,-20), 
-    Point3D(0.75, 0.75, 0.25), Point3D(20,20,-20), +    Point3D(20,20,-20), 
-    Point3D(0.75, 0.25, 0.25), Point3D(20,-20,-20), +    Point3D(20,-20,-20), 
-    Point3D(0.25, 0.25, 0.25), Point3D(-20,-20,-20), +    Point3D(-20,-20,-20), 
-    Point3D(0.25, 0.75, 0.75), Point3D(-20,20,20), +    Point3D(-20,20,20), 
-    Point3D(0.75, 0.75, 0.75), Point3D(20,20,20), +    Point3D(20,20,20), 
-    Point3D(0.75, 0.25, 0.75), Point3D(20,-20,20), +    Point3D(20,-20,20), 
-    Point3D(0.25, 0.25, 0.75), Point3D(-20,-20,20)+    Point3D(-20,-20,20)
 ] ]
  
줄 137: 줄 159:
     screen = Screen(height=20, width=40, fps=20)     screen = Screen(height=20, width=40, fps=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, args=(key,), daemon=True).start() 
 + 
 +    colors = ['white', 'yellow', 'green', 'blue', 'red']
  
     while True:     while True:
 +        if not key.empty() and key.get() == b' ':
 +            colors = colors[1:] + colors[:1]
 +            
         canvas = Canvas(height=screen.height, width=screen.width)         canvas = Canvas(height=screen.height, width=screen.width)
  
줄 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)
-            if projection: +            # Transform the point from 3D to 2D 
-                # 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, t[f[0]].y, t[f[1]].x, t[f[1]].y) +            canvas.line(t[f[0]].x, t[f[0]].y, t[f[1]].x, t[f[1]].y, color=colors[0]
-            canvas.line(t[f[1]].x, t[f[1]].y, t[f[2]].x, t[f[2]].y) +            canvas.line(t[f[1]].x, t[f[1]].y, t[f[2]].x, t[f[2]].y, color=colors[0]
-            canvas.line(t[f[2]].x, t[f[2]].y, t[f[3]].x, t[f[3]].y) +            canvas.line(t[f[2]].x, t[f[2]].y, t[f[3]].x, t[f[3]].y, color=colors[0]
-            canvas.line(t[f[3]].x, t[f[3]].y, t[f[0]].x, t[f[0]].y)+            canvas.line(t[f[3]].x, t[f[3]].y, t[f[0]].x, t[f[0]].y, color=colors[0])
  
         screen.write(canvas.plot())         screen.write(canvas.plot())
  
 </code> </code>
plotille/rotating_cube.1592325826.txt.gz · 마지막으로 수정됨: 2024/03/23 02:38 (바깥 편집)