| | |
Spherical to Cartesian function
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
I don't know of one. This code requires a radial coordinate.
Python Syntax (Toggle Plain Text)
class Pt(object): def __init__(self, x=0.0, y=0.0, z=0.0): self.x = x self.y = y self.z = z def __str__(self): return '(%0.4f, %0.4f, %0.4f)' % (self.x, self.y, self.z) def __repr__(self): return 'Pt(%f, %f, %f)' % (self.x, self.y, self.z) def __add__(self, other): return Point(self.x+other.x, self.y+other.y, self.z+other.z) def __sub__(self, other): return Point(self.x-other.x, self.y-other.y, self.z-other.z) def __mul__(self, f): return Point(self.x*f, self.y*f, self.z*f) def dist(self, other): p = self-other return (p.x**2 + p.y**2 + p.z**2)**0.5 def toSpherical(self): r = mag(self) theta = atan2(sqrt(self.x**2+self.y**2), self.z) phi = atan2(self.y, self.x) return SphericalPt(r, theta, phi) class SphericalPt(object): def __init__(self, r, theta, phi): # radial coordinate, zenith angle, azimuth angle self.r = r self.theta = theta self.phi = phi def __str__(self): return '(%0.4f, %0.4f, %0.4f)' % (self.r, self.theta, self.phi) def __repr__(self): return 'SphericalPt(%f, %f, %f)' % (self.r, self.theta, self.phi) def toCartesian(self): x = self.r*cos(self.phi)*sin(self.theta) y = self.r*sin(self.phi)*sin(self.theta) z = self.r*cos(self.theta) return Pt(x,y,z)
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
Thanks, I haven't used classes in python yet so this is a good example for me.. For future readers, there is a small typo. In the Pt class, the return statements say Point(...) when they should say Pt(...).
It's interesting this isn't in a library anywhere though, it seems like deg2rad, cart2sphere, etc type functions would be pretty handy.
Dave
It's interesting this isn't in a library anywhere though, it seems like deg2rad, cart2sphere, etc type functions would be pretty handy.
Dave
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
No. The work I have done interfaces with SDS/2 software. SDS/2 has a built-in interpreter, and we run scripts inside the SDS/2 3D model to automate tasks, extract and manipulate information, etc. Some of the code can be viewed here, but I have not updated it in several months.
What type of geometry work are you interested in? Following are some of my geometry applications:
LineLineIntersect3D (two point pairs)
Plane3D (three points)
LinePlaneIntersect3D
DistancePointPlane3D
PlanePlaneIntersect3D
BasisTransToGlobal - translate a point in a defined orthonormal basis to a point in the standard basis set
BasisTransToLocal - translate a point in the standard basis set to a point in a defined orthonormal basis
CircleCircleIntersect3D
I have no special knowledge nor am I an expert in geometry. Everything I have developed was from research done at various sites on the internet, particularly here.
What type of geometry work are you interested in? Following are some of my geometry applications:
LineLineIntersect3D (two point pairs)
Plane3D (three points)
three point circleDistancePointLine3D
rotate a point about an arbitrary axis
LinePlaneIntersect3D
DistancePointPlane3D
PlanePlaneIntersect3D
BasisTransToGlobal - translate a point in a defined orthonormal basis to a point in the standard basis set
BasisTransToLocal - translate a point in the standard basis set to a point in a defined orthonormal basis
CircleCircleIntersect3D
I have no special knowledge nor am I an expert in geometry. Everything I have developed was from research done at various sites on the internet, particularly here.
![]() |
Other Threads in the Python Forum
- Previous Thread: passing variables to sqlite
- Next Thread: Understanding functions
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wx.wizard wxpython xlwt





