-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw2d.py
More file actions
434 lines (408 loc) · 12.7 KB
/
draw2d.py
File metadata and controls
434 lines (408 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#-*- coding: cp932 -*-
#from visual import * # for vpython6
from vpython import * # for vpython7
import numpy as np
# size of the world (-max <= x <= max and -max <= y <= max)
max=3000.0
class Point:
def __init__(self,x=0.0,y=0.0,name=""):
self.x = x
self.y = y
self.name = name
self.pos = vector(x,y,0)
self.arr = np.array([x,y])
self.pic = sphere(pos=self.pos, radius=3,color=color.red)
self.label = label(pos=self.pos,text=self.name)
self.pic.visible = False
self.label.visible = False
def name(self,name):
self.name = name
def draw(self,label=True):
self.pic.visible = True
if label and self.name != "": self.label.visible = True
return
def hide(self):
self.pic.visible = False
self.label.visible = False
return
def show(self):
self.pic.visible = True
return
class Line:
def __init__(self,p1=Point(0,0),p2=Point(0,0)):
self.p1 = p1
self.p2 = p2
self.pos1 = p1.pos
self.pos2 = p2.pos
self.arr1 = p1.arr
self.arr2 = p2.arr
self.name = p1.name + p2.name
self.pic = None
# [a,b,c] means ax+by=c
if p1.pos == p2.pos: self.abc = [] # No line
else:
(x1,y1) = (p1.x,p1.y)
(x2,y2) = (p2.x,p2.y)
if x1 == x2:
self.abc = [1,0,x1]
else:
self.abc = [y1-y2,x2-x1,y1*x2-x1*y2]
return
def drawSeg(self,anime=250): # draw the line segment
v1 = self.pos1
v2 = self.pos2
if 0 < anime:
diff = 500
mytrail = []
for t in range(diff):
rate(anime)
posDot = v1 + 0.002*t*(v2-v1)
if t % 20 == 0:
dot = sphere(pos=posDot,radius=3,color=color.cyan)
mytrail.append(dot)
for i in range(len(mytrail)):
mytrail[i].visible=False
pic = curve(pos=[self.pos1,self.pos2],radius=1,color=color.cyan)
self.pic = pic
return
def draw(self,anime=250): # draw the line
p1=self.p1
p2=self.p2
v1=self.p1.pos
v2=self.p2.pos
if p1 == p2: return
if p1.x == p2.x:
v1=vector(p1.x,-max,0)
v2=vector(p1.x,max,0)
pic = curve(pos=[v1,v2],radius=1,color=color.cyan)
self.pic = pic
return
if p1.y == p2.y:
v1=vector(-max,p1.y,0)
v2=vector(max,p1.y,0)
pic = curve(pos=[v1,v2],radius=1,color=color.cyan)
self.pic = pic
return
t1=(1.0*max-p2.x)/(p1.x-p2.x)
u1=(1.0*max+p2.x)/(p2.x-p1.x)
t2=(1.0*max-p2.y)/(p1.y-p2.y)
u2=(1.0*max+p2.y)/(p2.y-p1.y)
ls=[t1,u1,t2,u2]
ls.sort()
tM=ls[1] # minus t
tP=ls[2] # plus t
vX=tM*(v1-v2)+v2
vY=tP*(v1-v2)+v2
if 0 < anime:
diff = 500
mytrail = []
for t in range(diff):
rate(anime)
posDot = v1 + 0.002*t*(v2-v1)
if t % 20 == 0:
dot = sphere(pos=posDot,radius=3,color=color.cyan)
mytrail.append(dot)
for i in range(len(mytrail)):
mytrail[i].visible=False
pic = curve(pos=[vX,vY],radius=0.5,color=color.cyan)
self.pic = pic
return
def hide(self):
self.pic.visible = False
return
def show(self):
self.pic.visible = True
return
class Circle:
def __init__(self,c=Point(0,0),p=Point(0,0)):
self.c = c # center
self.p = p # a point on the circle
self.name = c.name + p.name
r = np.linalg.norm(p.arr - c.arr)
self.radius = r
self.pic = ring(pos=c.pos,radius=r,axis=vector(0,0,1),thickness=1,color=color.cyan)
self.pic.visible = False
return
def draw(self,anime=250): # draw the circle (with animation)
if anime:
c = self.c.pos
p = self.p.pos
r = self.radius
diff = 500
ball = sphere(pos=p, radius=0,color=color.cyan,make_trail=False)
ball.make_trail=False
mytrail = []
for t in range(diff):
rate(anime)
vecC = self.c.arr
vecP = self.p.arr
theta = 2*pi*t/diff
vecPC = vecP-vecC
rotM = np.matrix(
[[cos(theta),-sin(theta)],
[sin(theta),cos(theta)]])
vecQ = np.dot(rotM,vecPC)+vecC
ball.pos = vector(vecQ[0,0],vecQ[0,1],0)
if t % 20 == 0:
s = sphere(pos=ball.pos,radius=3,color=color.cyan)
mytrail.append(s)
for i in range(len(mytrail)):
mytrail[i].visible=False
self.pic.visible = True
return
def hide(self):
self.pic.visible = False
return
def show(self):
self.pic.visible = True
return
def mkWorld():
#display(width=1000,height=800)
box(pos=vector(0,0,-0.3),width=0.1,height=max*2,length=max*2)
curve(pos=[vector(-max,0,0),vector(max,0,0)],radius=0.5,color=color.blue)
curve(pos=[vector(0,-max,0),vector(0,max,0)],radius=0.5,color=color.blue)
return
# put a point
def point(x,y,name="",draw=True):
p = Point(x,y,name)
if draw:
p.draw()
return p
# put a line segment between p1 and p2
def lineSeg(p1,p2,anime=250,draw=True):
ln = Line(p1,p2)
if draw:
ln.drawSeg(anime)
return ln
# put a line on p1 and p2
def line(p1,p2,anime=250,draw=True):
ln = Line(p1,p2)
if draw:
ln.draw(anime)
return ln
# put a circle on p2 with center p1
def circle(p1,p2,anime=250,draw=True):
circ = Circle(p1,p2)
if draw:
circ.draw(anime)
return circ
# cross point of two lines
# p = crossPointLL(ln1,ln2,"P") means
# p is the cross point of ln1 and ln2 with name "P"
def crossPointLL(ln1,ln2,name="",draw=True):
if ln1.abc == [] or ln2.abc == []:
return # ln1 or ln2 is no-line
[a1,b1,c1] = ln1.abc
[a2,b2,c2] = ln2.abc
a1=a1*1.0
b1=b1*1.0
c1=c1*1.0
a2=a2*1.0
b2=b2*1.0
c2=c2*1.0
if b1 == 0 and b2 == 0:
return # no cross point
elif b1 == 0:
(x,y) = (c1,(c2-a2*c1)/b2)
p = Point(x,y,name)
elif b2 == 0:
(x,y) = (c2,(c1-a1*c2)/b1)
p = Point(x,y,name)
else:
det = a1*b2-a2*b1
(x,y) = ((c1*b2-c2*b1)/det,(a1*c2-c1*a2)/det)
p = Point(x,y,name)
if draw:
p.draw()
return p
# Cross-points
# This calculates cross-points of circ and ax+by=c
# name1 and name2 are names of the cross-points
# It returns
# [] if no cross point exists
# [p1] if one cross point exists
# [p1,p2] if two cross points exist
# Note: (p1.x,p1.y) > (p2.x,p2.y) in the lexicographic order
def crossPointABC(a,b,c,objName,circ,name1,name2,draw=True):
r = circ.radius
vecC = circ.c.arr
cx = vecC[0]
cy = vecC[1]
a=a*1.0
b=b*1.0
c=c*1.0
if round(b,3) == 0.0:
x = c/a
y1 = cy + sqrt(r**2-(x-cx)**2)
y2 = cy - sqrt(r**2-(x-cx)**2)
p1 = Point(x,y1,name1)
p2 = Point(x,y2,name2)
if draw:
p1.draw()
p2.draw()
return [p1,p2]
else:
a = a/b
c = c/b
b = 1.0
aa = a**2
bb = b**2
rr = r**2
p = cx
q = cy
pp = p**2
k1 = aa+bb
k2 = a*b*q-a*c-p*bb
k3 = pp*bb-rr*bb+(b*q-c)**2
discrim = k2**2-k1*k3
if discrim < 0:
print("NoCross: " + objName + " and " + "Circ-" + circ.name)
return []
x1 = (-k2+sqrt(discrim))/k1
x2 = (-k2-sqrt(discrim))/k1
y1 = -a*x1/b+c
y2 = -a*x2/b+c
p1 = Point(x1,y1,name1)
if draw:
p1.draw()
if discrim == 0:
print("Tangent")
return [p1]
p2 = Point(x2,y2,name2)
if draw:
p2.draw()
return [p1,p2]
# cross points of a line and a circle
def crossPointLC(ln,circ,name1,name2,draw=True):
[a,b,c] = ln.abc # ln is ax+by=c
a=a*1.0
b=b*1.0
c=c*1.0
return crossPointABC(a,b,c,"Line-"+ln.name,circ,name1,name2,draw=draw)
def crossPointCC(circ1,circ2,name1,name2,draw=True):
(p1,q1) = (circ1.c.pos.x,circ1.c.pos.y)
(p2,q2) = (circ2.c.pos.x,circ2.c.pos.y)
r1 = circ1.radius
r2 = circ2.radius
(pp1,pp2) = (p1**2,p2**2)
(qq1,qq2) = (q1**2,q2**2)
(rr1,rr2) = (r1**2,r2**2)
a = p2-p1
b = q2-q1
c = 0.5*(rr1-rr2-pp1-qq1+pp2+qq2)
return crossPointABC(a,b,c,"Circ-"+circ2.name,circ1,name1,name2,draw=draw)
##############################################
# いくつかのショートカット
##############################################
# 垂直二等分線.返り値は,pA と pB を結ぶ線分の垂直二等分線
def bisectorLine(pA,pB,anime=250,draw=True):
if draw==False:
anime = 0
circAB = circle(pA,pB,draw=False)
circBA = circle(pB,pA,draw=False)
[pC,pD] = crossPointCC(circAB,circBA,"","",draw=False)
lnCD = line(pC,pD,anime=anime,draw=draw)
return lnCD
# 中点.返り値は,pA と pB の中点
def middlePoint(pA,pB,name,draw=True):
circAB = circle(pA,pB,draw=False)
circBA = circle(pB,pA,draw=False)
[pC,pD] = crossPointCC(circAB,circBA,"","",draw=False)
lnAB = line(pA,pB,draw=False)
lnCD = line(pC,pD,draw=False)
p = crossPointLL(lnAB,lnCD,name,draw=draw)
return p
# 垂線.返り値は,[点p を通る直線lnの垂線,その垂線の足]
def suisenLinePoint(ln,p,name,anime=250,draw=True):
if draw==False:
anime = 0
p1 = ln.p1
p2 = ln.p2
circ = circle(p,p1,draw=False)
pp = crossPointLC(ln,circ,"","",draw=False)
if len(pp) < 2:
circ = circle(p,p2,draw=False)
pp = crossPointLC(ln,circ,"","",draw=False)
pA = pp[0]
pB = pp[1]
lnS = bisectorLine(pA,pB,anime=anime,draw=draw)
pS = crossPointLL(lnS,ln,name,draw=draw)
return [lnS,pS]
# 垂線.返り値は,点p を通る直線lnの垂線
def suisenLine(ln,p,anime=250,draw=True):
if draw==False:
anime = 0
p1 = ln.p1
p2 = ln.p2
circ = circle(p,p1,draw=False)
pp = crossPointLC(ln,circ,"","",draw=False)
if len(pp) < 2:
pp[0].hide()
circ = circle(p,p2,draw=False)
pp = crossPointLC(ln,circ,"","",draw=False)
pA = pp[0]
pB = pp[1]
lnS = bisectorLine(pA,pB,anime=anime,draw=draw)
return lnS
# 平行線.返り値は,「点p を通り直線lnと平行な直線」
def heikoLine(ln,p,anime=250,draw=True):
if draw==False:
anime = 0
lnSui = suisenLine(ln,p,draw=False)
lnHeiko = suisenLine(lnSui,p,anime=anime,draw=draw)
return lnHeiko
# 直線上の異なる3点が順番通りかを調べる.p1,p2,p3 が1つの直線上にあることが前提.p2がp1とp3の間にあれば True,そうでなければ False
def checkLineOrder(p1,p2,p3):
if (p1.x < p2.x and p2.x < p3.x) or (p1.x > p2.x and p2.x > p3.x):
return True
elif p1.x == p2.x and p2.x == p3.x and ((p1.y < p2.y and p2.y < p3.y) or (p1.y > p2.y and p2.y > p3.y)):
return True
else:
return False
# 弧の中心点.返り値は,[劣弧の中心点,優弧の中心点]
# まず中心点を2つ得て,どちらの点がどちらの弧の上の点かを判定するのに checkLineOrder を使う
def arcCenterPoint(circ,p1,p2,name1,name2,draw=True):
ln = line(p1,p2,draw=False)
[lnA,pA] = suisenLinePoint(ln,circ.c,"",draw=False)
p1.show()
p2.show()
[pX,pY] = crossPointLC(lnA,circ,"","",draw=False)
if checkLineOrder(circ.c,pA,pX):
pX.name = name1
pY.name = name2
if draw:
pX.show()
pY.show()
return [pX,pY]
else:
pY.name = name1
pX.name = name2
if draw:
pY.show()
pX.show()
return [pY,pX]
# 2点間の三等分.返り値は p1 と p2 を3等分する点 [pM1,pM2]. p1-pM1-pM2-p2 の順
def mid3Points(p1,p2,name1,name2,draw=True):
ln12 = lineSeg(p1,p2,draw=False)
c1 = circle(p1,p2,draw=False)
c2 = circle(p2,p1,draw=False)
[pX,p] = crossPointCC(c1,c2,"X","",draw=False)
ln1X = line(p1,pX,draw=False)
circ1 = circle(pX,p1,draw=False)
[pY1,pY2] = crossPointLC(ln1X,circ1,"Y","",draw=False)
pY = pY1 if checkLineOrder(p1,pX,pY1) else pY2
circ2 = circle(pY,pX,draw=False)
[pZ1,pZ2] = crossPointLC(ln1X,circ2,"Z","",draw=False)
pZ = pZ1 if checkLineOrder(pX,pY,pZ1) else pZ2
lnZ2 = lineSeg(pZ,p2,draw=False)
lnY = heikoLine(lnZ2,pY,draw=False)
pM2 = crossPointLL(lnY,ln12,name2,draw=draw)
lnX = heikoLine(lnZ2,pX,draw=False)
pM1 = crossPointLL(lnX,ln12,name1,draw=draw)
return [pM1,pM2]
###############################################
mkWorld()
pO = Point(0,0,"O")
pO.draw()
pI = Point(1000,0,"I")
pI.draw()