私はdrawingpanel
モジュールを持っていないので、これはテストされていません:
from drawingpanel import *
panel = DrawingPanel(600, 600)
from math import *
import numpy as np
class Projection(object):
def __init__(self, origin, dx, dy, dz):
self.o = np.matrix([origin[0], origin[1], 0.])
self.p = np.matrix([
[dx[0], dx[1], 0.],
[dy[0], dy[1], 0.],
[dz[0], dz[1], 0.]
])
def proj(self, x, y, z):
res = self.o + np.matrix([x, y, z]) * self.p
return (res[0,0], res[0,1])
これは単純なアイソメトリック 3d-to-2d プロジェクションです。3d 座標を取り、対応する 2d スクリーン座標を返します。
proj = Projection((175,130), ( 50, -24), (-50, -24), ( 0, 70)).proj
特定の投影を作成します - あなたのイメージに基づいて、立方体の正面の角を (175,130) の原点にします。+X は立方体の右上隅まで伸びており、立方体を分割しやすくするためにその角を (3,0,0) にしています。つまり、(1,0,0) の画面投影は ( 215, 106)、dx (50, -24) を作成します。次に、同様に +Y を左上隅に、+Z を前面下部隅に移動します。
def make_poly_pts(*args):
return [coord for pt in args for coord in proj(*pt)]
これはユーティリティ関数です。3D ポイントのリストを受け取り、[x1、y1、x2、y2、... xN、yN] 座標のリストを返し、create_polygon にフィードします。
# allow for a gap between stickers
offs = 0.05
ooffs = 1. - offs
# draw top face (XY)
panel.canvas.create_polygon(*make_poly_pts((0,0,0), (3,0,0), (3,3,0), (0,3,0)), outline='black', fill='black')
for i in xrange(3):
for j in xrange(3):
panel.canvas.create_polygon(*make_poly_pts((i+offs,j+offs,0), (i+ooffs,j+offs,0), (i+ooffs,j+ooffs,0), (i+offs,j+ooffs,0)), outline='black', fill='yellow')
... 次に、軸を交換することで、他の 2 つの面を同様に作成できます。