Skip to content

Commit 2b32c6f

Browse files
committed
drawing
1 parent 00f8877 commit 2b32c6f

6 files changed

Lines changed: 1858 additions & 0 deletions

File tree

python2-scripts.zip

11.5 KB
Binary file not shown.

python2-scripts/mcpipy/drawing.py

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import mcpi.minecraft as minecraft
2+
import mcpi.block as block
3+
from mcpi.block import *
4+
from mcpi.entity import *
5+
import time
6+
from math import *
7+
import server
8+
9+
class Drawing:
10+
TO_RADIANS = pi / 180.
11+
TO_DEGREES = 180. / pi
12+
13+
def __init__(self,mc=None):
14+
if mc:
15+
self.mc = mc
16+
else:
17+
self.mc = minecraft.Minecraft.create(server.address)
18+
self.width = 1
19+
self.nib = [(0,0,0)]
20+
self.fan = None
21+
22+
def penwidth(self,w):
23+
self.width = int(w)
24+
if self.width == 0:
25+
self.nib = []
26+
elif self.width == 1:
27+
self.nib = [(0,0,0)]
28+
elif self.width == 2:
29+
self.nib = []
30+
for x in range(-1,1):
31+
for y in range(0,2):
32+
for z in range(-1,1):
33+
self.nib.append((x,y,z))
34+
else:
35+
self.nib = []
36+
r2 = self.width * self.width / 4.
37+
for x in range(-self.width//2 - 1,self.width//2 + 1):
38+
for y in range(-self.width//2 - 1, self.width//2 + 1):
39+
for z in range(-self.width//2 -1, self.width//2 + 1):
40+
if x*x + y*y + z*z <= r2:
41+
self.nib.append((x,y,z))
42+
43+
def point(self, x, y, z, block):
44+
for p in self.nib:
45+
self.mc.setBlock(x+p[0],y+p[1],z+p[2],block)
46+
47+
def face(self, points, block):
48+
if len(points) == 0:
49+
return
50+
51+
self.fan = points[0]
52+
self.done = {}
53+
prev = points[len(points)-1]
54+
55+
for p in points:
56+
self.line(prev[0], prev[1], prev[2], p[0], p[1], p[2], block)
57+
prev = p
58+
59+
self.fan = False
60+
61+
def line(self, x1,y1,z1, x2,y2,z2, block):
62+
def drawPoint(p):
63+
if self.width == 1 and not self.fan:
64+
self.mc.setBlock(p[0],p[1],p[2],block)
65+
else:
66+
for point in self.nib:
67+
x0 = p[0]+point[0]
68+
y0 = p[1]+point[1]
69+
z0 = p[2]+point[2]
70+
if not (x0,y0,z0) in self.done:
71+
self.mc.setBlock(x0,y0,z0,block)
72+
self.done[x0,y0,z0] = True
73+
74+
# dictinary to avoid duplicate drawing
75+
if not self.fan:
76+
self.done = {}
77+
78+
line = Drawing.getLine(x1,y1,z1, x2,y2,z2)
79+
80+
if self.fan:
81+
def fan(base,line):
82+
for a in line:
83+
fillLine = Drawing.getLine(a[0],a[1],a[2],
84+
base[0],base[1],base[2])
85+
for b in fillLine:
86+
drawPoint(b)
87+
88+
# draw the main fan
89+
fan(self.fan,line)
90+
# now fill in some possible gaps
91+
# This is faster than it seems due to the self.done dictionary
92+
fan((x1,y1,z1),Drawing.getLine(self.fan[0],self.fan[1],self.fan[2],
93+
x2,y2,z2))
94+
fan((x2,y2,z2),Drawing.getLine(self.fan[0],self.fan[1],self.fan[2],
95+
x1,y1,z1))
96+
else:
97+
for a in line:
98+
drawPoint(a)
99+
100+
@staticmethod
101+
def getLine(x1, y1, z1, x2, y2, z2):
102+
line = []
103+
x1 = int(x1)
104+
y1 = int(y1)
105+
z1 = int(z1)
106+
x2 = int(x2)
107+
y2 = int(y2)
108+
z2 = int(z2)
109+
point = [x1,y1,z1]
110+
dx = x2 - x1
111+
dy = y2 - y1
112+
dz = z2 - z1
113+
x_inc = -1 if dx < 0 else 1
114+
l = abs(dx)
115+
y_inc = -1 if dy < 0 else 1
116+
m = abs(dy)
117+
z_inc = -1 if dz < 0 else 1
118+
n = abs(dz)
119+
dx2 = l << 1
120+
dy2 = m << 1
121+
dz2 = n << 1
122+
123+
if l >= m and l >= n:
124+
err_1 = dy2 - l
125+
err_2 = dz2 - l
126+
for i in range(0,l-1):
127+
line.append((point[0],point[1],point[2]))
128+
if err_1 > 0:
129+
point[1] += y_inc
130+
err_1 -= dx2
131+
if err_2 > 0:
132+
point[2] += z_inc
133+
err_2 -= dx2
134+
err_1 += dy2
135+
err_2 += dz2
136+
point[0] += x_inc
137+
elif m >= l and m >= n:
138+
err_1 = dx2 - m;
139+
err_2 = dz2 - m;
140+
for i in range(0,m-1):
141+
line.append((point[0],point[1],point[2]))
142+
if err_1 > 0:
143+
point[0] += x_inc
144+
err_1 -= dy2
145+
if err_2 > 0:
146+
point[2] += z_inc
147+
err_2 -= dy2
148+
err_1 += dx2
149+
err_2 += dz2
150+
point[1] += y_inc
151+
else:
152+
err_1 = dy2 - n;
153+
err_2 = dx2 - n;
154+
for i in range(0, n-1):
155+
line.append((point[0],point[1],point[2]))
156+
if err_1 > 0:
157+
point[1] += y_inc
158+
err_1 -= dz2
159+
if err_2 > 0:
160+
point[0] += x_inc
161+
err_2 -= dz2
162+
err_1 += dy2
163+
err_2 += dx2
164+
point[2] += z_inc
165+
line.append((point[0],point[1],point[2]))
166+
return line
167+
168+
169+
if __name__ == "__main__":
170+
d = Drawing()
171+
pos = d.mc.player.getPos()
172+
d.face([(pos.x,pos.y,pos.z),(pos.x+10,pos.y+10,pos.z),(pos.x+10,pos.y+10,pos.z+10),(pos.x,pos.y,pos.z+10)], GLASS)

0 commit comments

Comments
 (0)