简体   繁体   English

DrawingPanel.py绘制一个魔方

[英]DrawingPanel.py Drawing a rubik's cube

using python i'd like to draw a rubik's cube based on this picture http://vixra.files.wordpress.com/2010/0 ... s-cube.jpg 使用python我想根据这张图片绘制一个rubik的立方体http://vixra.files.wordpress.com/2010/0 ... s-cube.jpg

this is my current code http://pastebin.com/MfF07ze4 这是我目前的代码http://pastebin.com/MfF07ze4

but i'd like the code to have at least 5 for loops and 5 functions that will aid in the creation of the cube. 但我希望代码至少有5个for循环和5个函数,这将有助于创建多维数据集。 also i need help with the algorithms in creating the 3 points for the 1x1 cubes of the rubik's cube. 我还需要帮助算法为rubik立方体的1x1立方体创建3个点。

I don't have the drawingpanel module, so this is untested: 我没有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])

This is a simple isometric 3d-to-2d projection - it takes a 3d coordinate and returns the corresponding 2d screen coordinate. 这是一个简单的等距3d-to-2d投影 - 它需要一个3d坐标并返回相应的2d屏幕坐标。

proj = Projection((175,130), ( 50, -24), (-50, -24), (  0,  70)).proj

I create a specific projection - based on your image, I make the front corner of the cube the origin at (175,130). 我创建了一个特定的投影 - 基于你的图像,我使立方体的前角成为原点(175,130)。 +X runs to the top right corner of the cube, and I make that corner point (3,0,0) to make it easy to subdivide the cube, which means that the screen projection of (1,0,0) is (215, 106), making dx (50, -24); + X运行到立方体的右上角,我制作角点(3,0,0)以便于细分立方体,这意味着(1,0,0)的屏幕投影是( 215,106),制作dx(50,-24); then similarly for +Y to the top left corner and +Z to the bottom front corner. 然后类似地,+ Y到左上角,+ Z到左下角。

def make_poly_pts(*args):
    return [coord for pt in args for coord in proj(*pt)]

This is a utility function - it takes a list of 3d points and returns a list of [x1, y1, x2, y2, ... xN, yN] coordinates to feed to create_polygon. 这是一个实用函数 - 它获取一个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')

... then the other two faces can be created similarly by swapping axes. ...然后可以通过交换轴类似地创建其他两个面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM