Commits

committed b1d4844

init

• Participants
• Branches default

File common.py

`+#coding=utf8`
`+'''`
`+Created on 2012-1-2`
`+`
`+@author: WeakDancer`
`+'''`
`+`
`+import math`
`+def mapPos(pos):`
`+	return (pos[0]+40,320-pos[1]-100)`
`+`
`+def space(a,b):`
`+	#计算两点的距离`
`+	space=math.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)`
`+	#print space`
`+	return space`
`+def state(a,b):`
`+	#计算两点的直线的角度`
`+	try:`
`+		state=math.atan((a[0]-b[0])/(a[1]-b[1]))*360/math.pi`
`+	except:`
`+		return 90`
`+	#print space`
`+	if state<0:`
`+		state=state+360`
`+	return int(state)`
`+def draw_arc_point(R,alpha,center=(0,0)):`
`+	a=R*math.cos(alpha)`
`+	b=R*math.sin(alpha)`
`+	return (int(center[0]+a),int(center[1]+b))`
`+`
`+def get_track(center,R,piece=36):`
`+	allPos=[]`
`+	for i in range(-piece,piece):`
`+		allPos.append(draw_arc_point(R,math.pi*i/piece,center))`
`+	return allPos`
`+`
`+def slope(b,a):`
`+	return math.atan((a[0]-b[0])/(a[1]-b[1]))`
`+`
`+#print math.tan(slope((5,5),(10,10)))`

File mechanism.py

`+#coding=utf8`
`+'''`
`+Created on 2012-1-2`
`+`
`+@author: WeakDancer`
`+'''`
`+`
`+`
`+from math import *`
`+from common import space,slope`
`+class Pair(object):`
`+	"""`
`+	Pair 运动副`
`+	"""`
`+	TYPE_DIC={"zhuan_dong":0}`
`+	type=TYPE_DIC["zhuan_dong"]`
`+	def __init__(self,link,position,name):`
`+		self.name=name`
`+		self.position_index=0`
`+		self.link=link`
`+		self.free=link[0].free and link[1].free`
`+		self.position=position`
`+		#print self.position`
`+		if self.free:`
`+			self.allPos=[position]`
`+		for _link in link:`
`+			_link.add_pair(self)`
`+		pass`
`+	def get_space(self,another_pair):`
`+		return space(self.position,another_pair.position)`
`+				`
`+`
`+class Link(object):`
`+	"""`
`+	Link 构件`
`+	"""`
`+	def __init__(self,*args):`
`+		self.pairs=[]#the collection of pairs`
`+		self.free=True`
`+		self.run=False`
`+		`
`+		pass`
`+	def set_rack(self):`
`+		self.free=False`
`+	def get_two_pair_position(self):`
`+		return (self.pairs[0].position,self.pairs[1].position)`
`+	def add_pair(self,pair):`
`+		self.pairs.append(pair)`
`+		#print self`
`+		#print len(self.pairs)`
`+	def move_init(self):`
`+		for _pair in self.pairs:`
`+			print _pair.name,_pair.free`
`+			if not _pair.free:`
`+				center=_pair.position`
`+			else:`
`+				self.free_trial=_pair`
`+		self.free_trial.allPos=get_track(center,space(self.get_two_pair_position()[0],self.get_two_pair_position()[1]))`
`+	def move(self,direction="shun"):`
`+		trial=self.free_trial`
`+		#print trial.name`
`+		if direction=="shun":`
`+			trial.position_index+=1`
`+			if trial.position_index==len(trial.allPos):`
`+				trial.position_index=0`
`+			trial.position=trial.allPos[trial.position_index]`
`+`
`+`
`+class Mechanism(object):`
`+	"""`
`+	Mechanism 机构`
`+	"""`
`+	links=[]#the collection of links`
`+	pairs=[]#the collection of pairs`
`+	def __init__(self,*args):`
`+		pass`
`+	def move_init(self):`
`+		for _link in self.links:`
`+			if _link.run:`
`+				self.runner=_link`
`+				_link.move_init()`
`+	def add_link(self,link):`
`+		self.links.append(link)`
`+	def move(self):`
`+		self.runner.move()`
`+	def draw_img(self,img):`
`+		for _link in self.links:`
`+			if _link.free:`
`+				_pos=_link.get_two_pair_position()`
`+				img.line((mapPos(_pos[0]),mapPos(_pos[1])),0x00ffff,width=4)`
`+		for _pair in self.pairs:`
`+			img.text(mapPos(_pair.position),unicode(_pair.name),0xff00ff)`
`+			img.point(mapPos(_pair.position),0xffff00,width=6)`
`+			`
`+`
`+class SiGan(Mechanism):`
`+	"""`
`+	四杆机构`
`+	ABCD顺时针`
`+	默认AD为水平,AB60度`
`+	"""`
`+	def __init__(self,AB=50,BC=60,CD=80,AD=100):`
`+		self.AB,self.BC,self.CD,self.AD=AB,BC,CD,AD`
`+		ab=Link()`
`+		bc=Link()`
`+		cd=Link()`
`+		ad=Link()`
`+		ab.run=True`
`+		ad.set_rack()`
`+		self.links=[ab,bc,cd,ad]`
`+		_pos=self._calculate_pair_position(AB,BC,CD,AD)`
`+		#print _pos`
`+		a=Pair((ad,ab),_pos[0],"A")`
`+		b=Pair((ab,bc),_pos[1],"B")`
`+		c=Pair((bc,cd),_pos[2],"C")`
`+		d=Pair((cd,ad),_pos[3],"D")`
`+		self.pairs=[a,b,c,d]`
`+		pass`
`+	def _calculate_C(self,B):`
`+		pass`
`+	def _calculate_pair_position(self,AB,BC,CD,AD,_BAD=pi/2):`
`+		A=(0,0)`
`+		D=(AD,0)`
`+		B=(0,AB)`
`+		self.BD=space(B,D)`
`+		#print BD`
`+		_CBD=acos((BC**2+BD**2-CD**2)/(2*BC*BD))`
`+		#print tan(abs(slope(B,D)))`
`+		_CDB=acos((CD**2+BD**2-BC**2)/(2*CD*BD))`
`+		C=(B[0]+BC*cos(pi/2-abs(slope(B,D)-_CBD)),B[1]+BC*cos(abs(slope(B,D)-_CDB)))`
`+		return A,B,C,D`
`+	def print_all(self):`
`+		for _link in self.links:`
`+			#print _link.get_two_pair_position()`
`+			for _pair in _link.pairs:`
`+				print _pair.position,_pair.name`
`+from pys60onpc import appuifw`
`+from pys60onpc.graphics import *`
`+from pys60onpc import e32`
`+from pys60onpc.key_codes import *`
`+`
`+`
`+class Simulate(object):`
`+	def __init__(self,**kw):`
`+		appuifw.app.screen='full'`
`+		self.img=None`
`+		self.myfont=u'Sans MT 936_S60'`
`+		self.running=1`
`+		self.canvas=appuifw.Canvas(`
`+			event_callback=self._handle_event,`
`+			redraw_callback=self._handle_redraw)`
`+		appuifw.app.body=self.canvas`
`+		self.img=Image.new(self.canvas.size)`
`+		appuifw.app.body.bind(55,lambda:self.quit())`
`+	def quit(self):`
`+		self.running=0`
`+`
`+	def _handle_redraw(self,rect):`
`+		if self.img:`
`+			self.canvas.blit(self.img)`
`+	def _handle_event(self,event):`
`+		pass`
`+		#print event`
`+		# keyboard_state={}`
`+	def run(self,cycleTime=0):`
`+		self.m=SiGan()`
`+		self.m.move_init()`
`+		while self.running:`
`+				self.img.clear(0)`
`+				self.m.move()`
`+				self.m.draw_img(self.img)`
`+				self._handle_redraw(())`
`+				e32.ao_yield()`
`+			`
`+`
`+`
`+s=Simulate()`
`+s.run()`
`+#s.m.print_all()`
`+e32.ao_yield()`

File pys60onpc/__init__.py

`+#coding=utf8`
`+'''`
`+Created on 2012-1-2`
`+`
`+@author: WeakDancer`
`+'''`
`+`
`+class Dummy:`
`+    def __getattr__(self,*args,**kargs):`
`+        print "args",repr(args),"kargs",repr(kargs)`
`+`
`+    def __setattr__(self,*args,**kargs):`
`+        print "args",repr(args),"kargs",repr(kargs)`
`+    def __call__(self,*args,**kargs):`
`+        print "args",repr(args),"kargs",repr(kargs)`
`+e32=Dummy()`
`+`
`+class App(Dummy):`
`+	body=Dummy()`
`+class Appuifw(Dummy):`
`+	app=App()`
`+	Canvas=Dummy()`
`+`
`+        `
`+`
`+appuifw=Appuifw()`

File pys60onpc/graphics.py

`+#coding=utf8`
`+'''`
`+Created on 2012-1-2`
`+`
`+@author: WeakDancer`
`+'''`
`+`
`+from Tkinter import Canvas,Tk`
`+`
`+Draw=_graphics.Draw`
`+`
`+def _revdict(d):`
`+    return dict([(d[k],k) for k in d.keys()])`
`+`
`+SDK12=True`
`+`
`+if not SDK12:`
`+    FLIP_LEFT_RIGHT=_graphics.FLIP_LEFT_RIGHT`
`+    FLIP_TOP_BOTTOM=_graphics.FLIP_TOP_BOTTOM`
`+    ROTATE_90=_graphics.ROTATE_90`
`+    ROTATE_180=_graphics.ROTATE_180`
`+    ROTATE_270=_graphics.ROTATE_270`
`+`
`+class Image(object):`
`+    _twips = sysinfo.display_twips()`
`+    _pixels = sysinfo.display_pixels()`
`+    _default_density = (float(_twips[0])/_pixels[0],`
`+                        float(_twips[1])/_pixels[1])`
`+    _modemap={'1': _graphics.EGray2,`
`+              'L': _graphics.EGray256,`
`+              'RGB12': _graphics.EColor4K,`
`+              'RGB16': _graphics.EColor64K,`
`+              'RGB': _graphics.EColor16M}`
`+    _moderevmap=_revdict(_modemap)`
`+	def new(size,mode='RGB16'):`
`+		if not Image._modemap.has_key(mode):`
`+			raise ValueError('invalid mode')`
`+		return Image(_graphics.ImageNew(size,Image._modemap[mode]))`
`+    def  __init__(self,canvas,width=320, height=240):`
`+        self.width,self.height,self.canvas=width,height,canvas`
`+    def point(self,pos,color=0xffffff,width=1):`
`+        self.canvas.create_oval(pos[0]-width,pos[1]-width,pos[0]+width,pos[1]+width,width=0,fill=int2rgb(color))`
`+    def line(self,pos,color=0xffffff,width=1):`
`+        self.canvas.create_line(pos[0],pos[1],pos[0],pos[1],`
`+                                          width=width,`
`+                                          arrow="last",`
`+                                          capstyle="round",`
`+                                          fill=int2rgb(color))`
`+    def clear(self,flag=0):`
`+        if flag==0:`
`+            self.canvas.delete('all')`

File pys60onpc/initaile_utiles.py

`+#coding=utf8`

File pys60onpc/key_codes.py

`+#coding=utf8`
`+'''`
`+Created on 2012-1-2`
`+`
`+@author: WeakDancer`
`+'''`

File pys60onpc/keycapture.py

`+#coding=utf8`
`+'''`
`+Created on 2012-1-2`
`+`
`+@author: WeakDancer`
`+'''`

File pys60onpc/topwindow.py

`+#coding=utf8`
`+'''`
`+Created on 2012-1-2`
`+`
`+@author: WeakDancer`
`+'''`