-
1 # 平章芯事
-
2 # EndyC
沒有學過python,受邀回答就臨時抱佛腳看看API學習了一下,給出作答程式碼,此程式碼依賴python graphics圖形庫(http://mcsp.wartburg.edu/zelle/python/graphics.py):
#!/usr/bin/python# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
Function:
My First Python Program For TouTiao QA
Author: Endy
Verison:1.0_2017-03-25
-------------------------------------------------------------------------------
"""
# 引用圖形庫
from graphics import *;
def drawCube():
# 等待使用者輸入邊長
sideLen = int(input("\n\nPlease input side length of the cube."));
# 建立繪圖視窗
win = GraphWin("CanvasWin", 800, 800);
# 確定前面4個頂點座標
p1 = Point(100, 100);
p2 = Point(100 + sideLen, 100);
p3 = Point(100 + sideLen, 100 + sideLen);
p4 = Point(100, 100 + sideLen);
# 後面相對前面縮小並傾斜,產生透視效果
xOffset = sideLen * 0.4;
yOffset = -sideLen * 0.3;
sideLen = sideLen * 0.8;
p5 = Point(100 + xOffset, 100 + yOffset);
p6 = Point(100 + xOffset + sideLen, 100 + yOffset);
p7 = Point(100 + xOffset + sideLen, 100 + yOffset + sideLen);
p8 = Point(100 + xOffset, 100 + yOffset + sideLen);
frontFace = Rectangle(p1, p3);
frontFace.draw(win);
backFace = Rectangle(p5, p7);
backFace.draw(win);
# 補充前面到後面的4條邊
line = Line(p1, p5);
line.draw(win);
line = Line(p2, p6);
line.draw(win);
line = Line(p3, p7);
line.draw(win);
line = Line(p4, p8);
line.draw(win);
###############################################################################
if __name__=="__main__":
drawCube();
回覆列表
立方體的表示和程式語言無關!
若不考慮多個立方體之間的關係,則只用長寬高表示即可。
若考慮,則需要加一個角的座標。
僅此而已!