|
Example 1: Usage of ScorpionGeometry def TSGeometry(): import ScorpionGeometry reload(ScorpionGeometry) from ScorpionGeometry import Vec v0 = Vec() v0.getVecValue('b4.Center[1]') print v0 v0.draw('Image','') l0 = ScorpionGeometry.Lin(v0,Vec(0,100)) l0.draw('Image','') print l0.len() l0.setLinValue('ed.l1') l1 = l0.midnorm() l1.draw('Image','') print l0.angle(),l1.angle() print l0 ScorpionGeometry.py from math import * from Scorpion import GetTool, GetValue, SetValue,DrawMarker,DrawAt,DrawLine,DrawLineEx def deg2rad(x): return pi*x/180 def rad2deg(x): return 180*x/pi class Vec: def __init__(self,*x): if len(x)==2: self.x=x[0] self.y=x[1] elif len(x)==1: x = deg2rad(x[0]) self.x=cos(x) self.y=sin(x) else: self.x=0 self.y=0 def getVecValue(self,name): self.x = GetValue(name+'_x') self.y = GetValue(name+'_y') def setVecValue(self,name): SetValue(name+'_x',self.x) SetValue(name+'_y',self.y) def draw(self,image,refName, color='Yellow', markerSize=3, vertexStyle=5): DrawAt(image) DrawMarker(refName, self.x, self.y, color, markerSize, vertexStyle) def __repr__(self): return 'Vec(%s,%s)' % (self.x,self.y) def __str__(self): return '(%g,%g)' % (self.x,self.y) def __neg__(self): return Vec(-self.x, -self.y) def __add__(self,other): if isinstance(other,Vec): return Vec(self.x+other.x, self.y+other.y) else: return Vec(self.x+other, self.y+other) def __sub__(self,other): if isinstance(other,Vec): return Vec(self.x-other.x, self.y-other.y) else: return Vec(self.x-other, self.y-other) def __mul__(self,other): # Dot product if isinstance(other,Vec): return self.x*other.x+self.y*other.y else: return Vec(self.x*other, self.y*other) def mul(self,other): if isinstance(other,Vec): return Vec(self.x*other.x, self.y*other.y) else: return Vec(self.x*other, self.y*other) def __div__(self,other): if isinstance(other,Vec): return Vec(self.x/other.x, self.y/other.y) else: return Vec(self.x/other, self.y/other) def __radd__(self,other): return Vec.__add__(self,other) def __rmul__(self,other): return Vec.__mul__(self,other) def __rsub__(self,other): if isinstance(other,Vec): return Vec(other.x-self.x, other.y-self.y) else: return Vec(other-self.x, other-self.y) def __mod__(self,other): # Cross product return self.x*other.y-self.y*other.x def len(self): return hypot(self.x,self.y) def angle(self): return rad2deg(atan2(self.y,self.x)) def normal(self): return Vec(-self.y,self.x) class Lin: def __init__(self,p=Vec(0,0),v=Vec(1,0)): self.p=p self.v=v def __repr__(self): return 'Lin(%s,%s)' % (self.p,self.v) def __str__(self): return '(%s,%s)' % (str(self.p),str(self.v)) def getLinValue(self,name): self.p.getVecValue(name+'_p') self.v.getVecValue(name+'_v') def setLinValue(self,name): self.p.setVecValue(name+'_p') self.v.setVecValue(name+'_v') def draw(self,image,refName, color='Yellow', penWidth=1, penStyle=0): DrawAt(image) DrawLineEx(refName, self.p.x, self.p.y, self.v.x+self.p.x, self.v.y+self.p.y, color, penWidth, penStyle) def intersect(self,other): if isinstance(other,Lin): d = self.v % other.v; if d: return (other.v%(self.p-other.p))/d elif isinstance(other,Vec): return (other-self.p)*self.v/(self.v*self.v); def len(self): return self.v.len() def angle(self): return self.v.angle() def start(self): return self.p def end(self): return self.p+self.v def point(self,t): return self.p+self.v*t def center(self): return self.point(0.5) def normal(self): return Lin(self.p,self.v.normal()) def midnorm(self): return Lin(self.center(),self.v.normal()) def __add__(self,other): # L+t computes parametric point t on L return self.point(other) def __mul__(self,other): # L*k scales the direction vector of L return Lin(self.p,self.v*other) def __neg__(self): # Roate L 180deg around starting point return Lin(self.p, -self.v) def __and__(self,other): # L1 & L2 compute intersection L1 and L2, such that intersection point is given by L1+L1&L2 return self.intersect(other) |
|