Text Size
Text Size
Exit Full Screen
Python Mini
Show
Source Code
View Source Code in Full Screen
Open in New Tab
from processing import * from math import cos, sin, radians, atan2, degrees, acos, asin from random import uniform b = 0 mb = 0 spc = 0 keys = [] selected = None class Particle: instances = [] def __init__(self,x,y,vx,vy,size,life=20): self.size = size self.x = float(x) self.y = float(y) self.vx = float(vx) self.vy = float(vy) self.life = life Particle.instances.append(self) def drawSelf(self): noStroke() fill(255,float(self.life)*12.75) ellipse(self.x,self.y,self.size,self.size) fill(255) stroke(0) Objects = [] class Angle(): instances = [] def __init__(self,x,y,r=0,grabbed=False,value=60): self.x = x self.y = y self.r = r self.grabbed = grabbed self.value = 60 self.im = 'angle' self.inconglom = False Angle.instances.append(self) Objects.append(self) def drawSelf(self): pushMatrix() strokeWeight(6) translate(self.x,self.y) rotate(radians(self.r)) if self.value != 90: arc(0,0,40,40,0,radians(self.value)) else: rect(0,0,20,20) line(0,0,30,0) line(0,0,30*cos(radians(self.value)),30*sin(radians(self.value))) popMatrix() class Lineobj(): instances = [] def __init__(self,x,y,r=0,grabbed=False,value=60): self.x = x self.y = y self.r = r self.grabbed = grabbed self.value = 60 self.im = 'line' self.inconglom = False Lineobj.instances.append(self) Objects.append(self) def drawSelf(self): pushMatrix() strokeWeight(6) translate(self.x,self.y) rotate(radians(self.r)) line(0,0,self.value,0) ellipse(0,0,10,10) ellipse(self.value,0,5,5) popMatrix() class Conglom(): instances = [] def __init__(self,inside): self.inside = inside for i in inside: i.inconglom = self Conglom.instances.append(self) def connect(self,other,side,backwards): if side == 0: current = self.inside[0] if current.im == 'angle': if other.im == 'line': other.r = current.r + 180 other.x = current.x + cos(radians(current.r))*(30+other.value) other.y = current.y + sin(radians(current.r))*(30+other.value) elif current.im == 'line': if other.im == 'line': other.r += 180*backwards other.x = current.x - cos(radians(other.r))*(other.value) other.y = current.y - sin(radians(other.r))*(other.value) else: other.r = current.r - other.value other.x = current.x - cos(radians(current.r))*30 other.y = current.y - sin(radians(current.r))*30 self.inside.insert(0,other) else: current = self.inside[-1] if current.im == 'angle': if other.im == 'line': other.r = current.r + current.value other.x = current.x + cos(radians(current.value+current.r))*30 other.y = current.y + sin(radians(current.value+current.r))*30 elif current.im == 'line': if other.im == 'line': other.r += 180*backwards other.x = current.x + cos(radians(current.r))*(current.value) other.y = current.y + sin(radians(current.r))*(current.value) else: other.r = current.r - 180 other.x = current.x + cos(radians(current.r))*(current.value+30) other.y = current.y + sin(radians(current.r))*(current.value+30) self.inside.append(other) other.inconglom = self def exphone(var,target,mult,snap): temp = var if var < snap+target and var > target-snap and var != target: var = target else: var += (target-var)/mult if var == temp: var = target return var def dashedLineLength(x1,y1,x2,y2,lenofdash,offset=0): offset *= 2.0 offset %= 2 dist = ((x2-x1)**2+(y2-y1)**2)**.5 if dist != 0: temp = lenofdash/dist fullx = x2-x1 fully = y2-y1 piecex = fullx*temp piecey = fully*temp pushMatrix() #offset should be from 0 to 1 translate(x1,y1) full = [] i = offset*piecex j = offset*piecey state = False while abs(i) < abs(fullx) or abs(j) < abs(fully): state = not state full.append([i,j]) i += piecex j += piecey if offset >= 1: full.insert(0,[offset*piecex-piecex,offset*piecey-piecey]) full.insert(0,[0,0]) if state: full.append([fullx,fully]) while len(full) > 0: temp = full.pop() temp2 = full.pop() line(temp[0],temp[1],temp2[0],temp2[1]) popMatrix() def line_intersection(line1, line2): xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0]) ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) def det(a, b): return a[0] * b[1] - a[1] * b[0] div = det(xdiff, ydiff) if div == 0: return None d = (det(*line1), det(*line2)) x = det(d, xdiff) / div y = det(d, ydiff) / div return x, y def setup(): size(500,500) def mousePressed(): global mb mb += 1 def mouseReleased(): global mb mb = -1 def keyPressed(): global keys, spc if keyboard.keyCode not in keys: keys.append(keyboard.keyCode) if keyboard.key == '1': Lineobj(220,400,0,False,60) elif keyboard.key == '0': Angle(235,100,0,False,60) if keyboard.keyCode == 32: spc += 1 def keyReleased(): global keys, spc, selected if keyboard.keyCode in keys: keys.remove(keyboard.keyCode) if keyboard.keyCode == 32: spc = -1 selected = None def draw(): background(200,200,255) global b, mb, spc, selected strokeWeight(6) alreadydone = False stroke(100,100,255) for them in Conglom.instances: if len(them.inside) == 3: temp = '' for i in them.inside: temp += i.im[0] if temp == 'lal': dashedLineLength(them.inside[0].x,them.inside[0].y,them.inside[-1].x+cos(radians(them.inside[-1].r))*them.inside[-1].value,them.inside[-1].y+sin(radians(them.inside[-1].r))*them.inside[-1].value,7,b) elif temp == 'ala': temp = line_intersection((((them.inside[0].x,them.inside[0].y),(them.inside[0].x+cos(radians(them.inside[0].r))*30,them.inside[0].y+sin(radians(them.inside[0].r))*30))),(((them.inside[-1].x,them.inside[-1].y),(them.inside[-1].x+cos(radians(them.inside[-1].r+them.inside[-1].value))*30,them.inside[-1].y+sin(radians(them.inside[-1].r+them.inside[-1].value))*30)))) if temp != None: dashedLineLength(them.inside[0].x,them.inside[0].y,temp[0],temp[1],7,b) dashedLineLength(them.inside[-1].x,them.inside[-1].y,temp[0],temp[1],7,b) else: pass elif temp == 'laa': used = them.inside dashedLineLength(used[2].x,used[2].y,used[1].x,used[1].y,7,b) dashedLineLength(used[2].x,used[2].y,used[0].x,used[0].y,7,b) elif temp == 'aal': used = them.inside endpint = [used[2].x+cos(radians(used[2].r))*used[2].value,used[2].y+sin(radians(used[2].r))*used[2].value] dashedLineLength(endpint[0],endpint[1],used[0].x,used[0].y,7,b) dashedLineLength(used[1].x,used[1].y,used[0].x,used[0].y,7,b) elif temp == 'all': if them.inside[0].value == 90 and them.inside[2].value >= them.inside[1].value+30: temp = degrees(acos((them.inside[1].value+30.0)/them.inside[2].value)) them.inside[2].r = them.inside[1].r - 180 + temp dashedLineLength(them.inside[0].x,them.inside[0].y,them.inside[2].x+cos(radians(them.inside[2].r))*them.inside[2].value,them.inside[2].y+sin(radians(them.inside[2].r))*them.inside[2].value,7,b) elif temp == 'lla': if them.inside[2].value == 90 and them.inside[0].value >= them.inside[1].value+30: temp = degrees(acos((them.inside[1].value+30.0)/them.inside[0].value)) them.inside[0].r = them.inside[1].r + 180 - temp them.inside[0].x = them.inside[1].x+cos(radians(them.inside[0].r-180))*them.inside[0].value them.inside[0].y = them.inside[1].y+sin(radians(them.inside[0].r-180))*them.inside[0].value dashedLineLength(them.inside[0].x,them.inside[0].y,them.inside[2].x,them.inside[2].y,7,b) stroke(0) for this in Objects: this.drawSelf() this.r %= 360 if this == selected and spc > 0 and this.inconglom == False: alreadydone = True this.grabbed = False stroke(255,170,0) selected = this temp = 0 if this.im == 'angle': temp = this.value/2.0 if this.x != mouse.x: this.r = degrees(atan2((mouse.y-this.y),(mouse.x-this.x)))-temp else: if this.y < mouse.y: this.r = 90-(temp) else: this.r = 270-(temp) dashedLineLength(this.x,this.y,mouse.x,mouse.y,8,b) stroke(0) else: #if grabbed if this.grabbed: if this.inconglom != False: for i in this.inconglom.inside: i.x += mouse.x-mouse.px i.y += mouse.y-mouse.py else: this.x += mouse.x-mouse.px this.y += mouse.y-mouse.py if mb == 0: this.grabbed = False if this.inconglom == False: if 65 in keys and 68 in keys: if this.r < 180: this.r = exphone(this.r,0,2,1) else: this.r = exphone(this.r,360,2,1) elif 65 in keys: this.r -= 1 elif 68 in keys: this.r += 1 if 87 in keys and 83 in keys: this.value = exphone(this.value,90,2,1) elif 87 in keys: this.value += 1 elif 83 in keys: this.value -= 1 else: if 65 in keys: for i in this.inconglom.inside: i.r -= 1 if i == this: continue dis = ((((i.x-this.x)**2)+((i.y-this.y)**2))**.5) i.x = this.x + ((i.x-this.x)*cos(radians(-1)) - (i.y-this.y)*sin(radians(-1))) i.y = this.y + ((i.y-this.y)*cos(radians(-1)) + (i.x-this.x)*sin(radians(-1))) i.x = this.x - cos(atan2(this.y-i.y,this.x-i.x))*dis i.y = this.y - sin(atan2(this.y-i.y,this.x-i.x))*dis elif 68 in keys: for i in this.inconglom.inside: i.r += 1 if i == this: continue dis = ((((i.x-this.x)**2)+((i.y-this.y)**2))**.5) i.x = this.x + ((i.x-this.x)*cos(radians(1)) - (i.y-this.y)*sin(radians(1))) i.y = this.y + ((i.y-this.y)*cos(radians(1)) + (i.x-this.x)*sin(radians(1))) i.x = this.x - cos(atan2(this.y-i.y,this.x-i.x))*dis i.y = this.y - sin(atan2(this.y-i.y,this.x-i.x))*dis if spc == 1 and this.inconglom == False: selected = this if this.inconglom == False: stroke(255,0,0) if this.im == 'line': for i in Objects: if i == this: continue if i.im == 'line': if ((this.x-i.x)**2+(this.y-i.y)**2)**.5 <= 20: dashedLineLength(this.x,this.y,i.x,i.y,7,b) if mb == 0: if i.inconglom == False: temp = Conglom([i]) temp.connect(this,0,1) else: i.inconglom.connect(this,0,1) break elif (((this.x+cos(radians(this.r))*this.value)-i.x)**2+((this.y+sin(radians(this.r))*this.value)-i.y)**2)**.5 <= 20: dashedLineLength(this.x+cos(radians(this.r))*this.value,this.y+sin(radians(this.r))*this.value,i.x,i.y,7,b) if mb == 0: if i.inconglom == False: temp = Conglom([i]) temp.connect(this,0,0) else: i.inconglom.connect(this,0,0) break elif ((this.x-(i.x+cos(radians(i.r))*i.value))**2+(this.y-(i.y+sin(radians(i.r))*i.value))**2)**.5 <= 20: dashedLineLength(this.x,this.y,i.x+cos(radians(i.r))*i.value,i.y+sin(radians(i.r))*i.value,7,b) if mb == 0: if i.inconglom == False: temp = Conglom([i]) temp.connect(this,1,0) else: i.inconglom.connect(this,1,0) break elif (((this.x+cos(radians(this.r))*this.value)-(i.x+cos(radians(i.r))*i.value))**2+((this.y+sin(radians(this.r))*this.value)-(i.y+sin(radians(i.r))*i.value))**2)**.5 <= 20: dashedLineLength((this.x+cos(radians(this.r))*this.value),(this.y+sin(radians(this.r))*this.value),i.x+cos(radians(i.r))*i.value,i.y+sin(radians(i.r))*i.value,7,b) if mb == 0: if i.inconglom == False: temp = Conglom([i]) temp.connect(this,1,1) else: i.inconglom.connect(this,1,1) break else: if ((this.x-(i.x+cos(radians(i.r))*30))**2+(this.y-(i.y+sin(radians(i.r))*30))**2)**.5 <= 20:# and (abs(this.r - i.r) <= 20 or abs(this.r-i.r-360) <= 20): dashedLineLength(this.x,this.y,(i.x+cos(radians(i.r))*30),(i.y+sin(radians(i.r))*30),7,b) if mb == 0: if i.inconglom == False: temp = Conglom([i]) temp.connect(this,0,1) else: i.inconglom.connect(this,0,1) break elif ((this.x-(i.x+cos(radians(i.r+i.value))*30))**2+(this.y-(i.y+sin(radians(i.r+i.value))*30))**2)**.5 <= 20:# and (abs(this.r - (i.r+i.value)) <= 20 or abs(this.r - (i.r+i.value)-360) <= 20): dashedLineLength(this.x,this.y,(i.x+cos(radians(i.r+i.value))*30),(i.y+sin(radians(i.r+i.value))*30),7,b) if mb == 0: if i.inconglom == False: temp = Conglom([i]) temp.connect(this,1,0) else: i.inconglom.connect(this,1,0) break elif (((this.x+cos(radians(this.r))*this.value)-(i.x+cos(radians(i.r))*30))**2+((this.y+sin(radians(this.r))*this.value)-(i.y+sin(radians(i.r))*30))**2)**.5 <= 20:# and (abs((this.r-180) - i.r) <= 20 or abs((this.r+180) - i.r) <= 20): dashedLineLength((this.x+cos(radians(this.r))*this.value),(this.y+sin(radians(this.r))*this.value),i.x+cos(radians(i.r))*30,i.y+sin(radians(i.r))*30,7,b) if mb == 0: if i.inconglom == False: temp = Conglom([i]) temp.connect(this,0,0) else: i.inconglom.connect(this,0,0) break elif (((this.x+cos(radians(this.r))*this.value)-(i.x+cos(radians(i.r+i.value))*30))**2+((this.y+sin(radians(this.r))*this.value)-(i.y+sin(radians(i.r+i.value))*30))**2)**.5 <= 20:# and (abs((this.r-180) - (i.r+i.value)) <= 20 or abs((this.r+180) - (i.r+i.value)) <= 20): dashedLineLength((this.x+cos(radians(this.r))*this.value),(this.y+sin(radians(this.r))*this.value),i.x+cos(radians(i.r+i.value))*30,i.y+sin(radians(i.r+i.value))*30,7,b) if mb == 0: if i.inconglom == False: temp = Conglom([i]) temp.connect(this,1,1) else: i.inconglom.connect(this,1,1) break else: done = False for i in Objects: if i.im == 'line': if (((this.x+cos(radians(this.r))*30)-i.x)**2+((this.y+sin(radians(this.r))*30)-i.y)**2)**.5 <= 20: if i.inconglom == False: dashedLineLength((this.x+cos(radians(this.r))*30),(this.y+sin(radians(this.r))*30),i.x,i.y,7,b) if mb == 0: temp = Conglom([this]) temp.connect(i,0,1) done = True; break elif (((this.x+cos(radians(this.r+this.value))*30)-i.x)**2+((this.y+sin(radians(this.r+this.value))*30)-i.y)**2)**.5 <= 20: dashedLineLength((this.x+cos(radians(this.r+this.value))*30),(this.y+sin(radians(this.r+this.value))*30),i.x,i.y,7,b) if mb == 0: if i.inconglom == False: temp = Conglom([this]) temp.connect(i,1,1) else: i.inconglom.connect(this,0,0) done = True; break elif (((this.x+cos(radians(this.r))*30)-(i.x+cos(radians(i.r))*i.value))**2+((this.y+sin(radians(this.r))*30)-(i.y+sin(radians(i.r))*i.value))**2)**.5 <= 20: dashedLineLength((this.x+cos(radians(this.r))*30),(this.y+sin(radians(this.r))*30),i.x+cos(radians(i.r))*i.value,i.y+sin(radians(i.r))*i.value,7,b) if mb == 0: if i.inconglom == False: temp = Conglom([this]) temp.connect(i,0,0) else: i.inconglom.connect(this,1,1) done = True; break elif (((this.x+cos(radians(this.r+this.value))*30)-(i.x+cos(radians(i.r))*i.value))**2+((this.y+sin(radians(this.r+this.value))*30)-(i.y+sin(radians(i.r))*i.value))**2)**.5 <= 20: if i.inconglom == False: dashedLineLength((this.x+cos(radians(this.r+this.value))*30),(this.y+sin(radians(this.r+this.value))*30),i.x+cos(radians(i.r))*i.value,i.y+sin(radians(i.r))*i.value,7,b) if mb == 0: temp = Conglom([this]) temp.connect(i,1,0) done = True; break if not done: for them in Conglom.instances: if len(them.inside) == 2: temp = '' for i in them.inside: temp += i.im[0] if temp in ['al','la']: if temp == 'la': used = them.inside temp = line_intersection(((used[0].x,used[0].y),(used[0].x+cos(radians(used[0].r-(180-this.value-used[1].value)))*30,used[0].y+sin(radians(used[0].r-(180-this.value-used[1].value)))*30)),((used[1].x,used[1].y),(used[1].x+cos(radians(used[1].r+used[1].value))*30,used[1].y+sin(radians(used[1].r+used[1].value))*30))) if temp != None: temp2 = used[-1].r+used[-1].value+180 if ((temp[0]-this.x)**2+(temp[1]-this.y)**2)**.5 <= 20 and (abs(temp2 - this.r) <= 20 or abs(temp2 - this.r - 360) <= 20): dashedLineLength(this.x+cos(radians(this.r))*30,this.y+sin(radians(this.r))*30,used[-1].x+cos(radians(used[-1].r+used[-1].value))*30,used[-1].y+sin(radians(used[-1].r+used[-1].value))*30,7,b) dashedLineLength(this.x+cos(radians(this.r+this.value))*30,this.y+sin(radians(this.r+this.value))*30,used[0].x,used[0].y,7,b) if mb == 0: this.x = temp[0] this.y = temp[1] this.r = temp2 this.inconglom = them them.inside += [this] break if temp == 'al': used = them.inside endpint = [used[1].x+cos(radians(used[1].r))*used[1].value,used[1].y+sin(radians(used[1].r))*used[1].value] temp = line_intersection(((endpint[0],endpint[1]),(endpint[0]-cos(radians(used[1].r+(180-this.value-used[0].value)))*30,endpint[1]-sin(radians(used[1].r+(180-this.value-used[0].value)))*30)),((used[0].x,used[0].y),(used[0].x+cos(radians(used[0].r))*30,used[0].y+sin(radians(used[0].r))*30))) if temp != None: temp2 = used[0].r+180-this.value if ((temp[0]-this.x)**2+(temp[1]-this.y)**2)**.5 <= 20 and (abs(temp2 - this.r) <= 20 or abs(temp2 - this.r - 360) <= 20): dashedLineLength(this.x+cos(radians(this.r+this.value))*30,this.y+sin(radians(this.r+this.value))*30,used[0].x+cos(radians(used[0].r))*30,used[0].y+sin(radians(used[0].r))*30,7,b) dashedLineLength(this.x+cos(radians(this.r))*30,this.y+sin(radians(this.r))*30,endpint[0],endpint[1],7,b) if mb == 0: this.x = temp[0] this.y = temp[1] this.r = temp2 this.inconglom = them them.inside = [this] + them.inside break stroke(0) elif ((mouse.x-this.x)**2+(mouse.y-this.y)**2)**.5 <= 30 and not alreadydone: alreadydone = True if mb == 1: this.grabbed = True for i in range(5): Particle(this.x,this.y,uniform(-4,4),uniform(-4,4),uniform(3,10)) if this.inconglom != False: temp = mouse.x-this.x temp2 = mouse.y-this.y for i in this.inconglom.inside: i.x += temp i.y += temp2 else: this.x = mouse.x this.y = mouse.y elif mb == 0: if this.inconglom == False: if 65 in keys and 68 in keys: if this.r < 180: this.r = exphone(this.r,0,2,1) else: this.r = exphone(this.r,360,2,1) elif 65 in keys: this.r -= 1 elif 68 in keys: this.r += 1 if 87 in keys and 83 in keys: this.value = exphone(this.value,90,2,1) elif 87 in keys: this.value += 1 elif 83 in keys: this.value -= 1 else: if 65 in keys: for i in this.inconglom.inside: i.r -= 1 if i == this: continue dis = ((((i.x-this.x)**2)+((i.y-this.y)**2))**.5) i.x = this.x + ((i.x-this.x)*cos(radians(-1)) - (i.y-this.y)*sin(radians(-1))) i.y = this.y + ((i.y-this.y)*cos(radians(-1)) + (i.x-this.x)*sin(radians(-1))) i.x = this.x - cos(atan2(this.y-i.y,this.x-i.x))*dis i.y = this.y - sin(atan2(this.y-i.y,this.x-i.x))*dis elif 68 in keys: for i in this.inconglom.inside: i.r += 1 if i == this: continue dis = ((((i.x-this.x)**2)+((i.y-this.y)**2))**.5) i.x = this.x + ((i.x-this.x)*cos(radians(1)) - (i.y-this.y)*sin(radians(1))) i.y = this.y + ((i.y-this.y)*cos(radians(1)) + (i.x-this.x)*sin(radians(1))) i.x = this.x - cos(atan2(this.y-i.y,this.x-i.x))*dis i.y = this.y - sin(atan2(this.y-i.y,this.x-i.x))*dis if spc == 1 and this.inconglom == False: stroke(255,170,0) selected = this temp = 0 if this.im == 'angle': temp = this.value/2.0 if this.x != mouse.x: this.r = degrees(atan2((mouse.y-this.y),(mouse.x-this.x)))-temp else: if this.y < mouse.y: this.r = 90-(temp) else: this.r = 270-(temp) else: stroke(125,120,255) dashedLineLength(this.x,this.y,mouse.x,mouse.y,8,b) stroke(0) for i in Particle.instances: i.x += i.vx i.y += i.vy i.vx *= .9 i.vy *= .9 if i.life <= 0: Particle.instances.remove(i) i.life -= 1 i.drawSelf() b += .05 if mb != 0: mb += 1 if spc != 0: spc += 1 a1 = Angle(385,100,0,False,60) a2 = Angle(235,100,0,False,60) a3 = Angle(85,100,0,False,60) l1 = Lineobj(70,400,0,False,60) l2 = Lineobj(220,400,0,False,60) l3 = Lineobj(370,400,0,False,60) run()
TriangleProofSimulator.py
( around 537 lines python code )
Published By:
luyfru
Published on
2023-09-21T18:44:40Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here