HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Class representing both cartesian and spherical coordinates

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
coordinatesbothcartesianandsphericalclassrepresenting

Problem

I have had little experience with OO programming, being a typical engineer I tend to hack together functional code. However, I'm trying to change that and I see value in implementing OO when modelling the physical world. I've written my first ever class for a position vector. I want an instance of the class to be able to store both the cartesian [x, y, z] and polar [z, theta, phi] coordinates of a position. Updating one attribute automatically updates the others. This class will be later be reused in other classes via composition.

I want an instance of my class to do the following:

>> p = PositionVector([15,5,5])
>> p.cartesian
[15,5,5]
>> p.polar
[16.583123951777, 0.0, 1.2645189576252271]
>> p.x = 3
>> p.cartesian
[3,5,5]
>> p.polar
[7.681145747868608, 0.7853981633974483, 0.8619682853367363]
>> p.theta = 0.5
>> p.cartesian
[5.117141702137862, 2.7955072527614058, 5.0]
>> p.polar
[7.681145747868608, 0.5, 0.8619682853367363]


The class I've written so far is below and while it works as expected, my limited understanding of Python classes and the property function tells me there's a better way to do this. I don't know if the methods for x y z r theta and phi need to be fully written since they are simply elements within each list/array but can't figure out how to get around this. I also don't know if this is the safest and most pythonic way of structuring a class object:

```
class PositionVector(object):
def __init__(self, vector, system='cartesian'):
if system == 'cartesian':
self.cartesian = vector
self.polar = self.position_vector_polar(self.cartesian)
if system == 'polar':
self.polar = vector
self.cartesian = self.position_vector_cartesian(self.polar)

@property
def cartesian(self):
return self._cartesian
@cartesian.setter
def cartesian(self, value):
self._cartesian = value
self._polar = self.position_vector_polar(self._cartesian)
self._x, self._y, self._z = self._cartesian
self._r, self._

Solution

As far as I understand the code, a PositionVector object holds both cartesian and polar coordinates. This is surely redundant: multiple representations of the same position hold no more information than a single one (note that there are many more coordinate systems, and you cannot possibly account for all of them). I recommend to keep the position in a preferred coordinate system, and provide methods to convert it to and from other representations.

Edit: addressing concerns in the comment:


What about if I want to set a new radius, r? What would be the most
appropriate way to update [x, y, z]?

This is a very important question I didn't elaborate on in the answer. In the algebraic world we want to reason in terms of objects, as opposed to coordinates. Coordinates are secondary - they are just instrumental for calculations.

In other words, you do not want to set new radius. You want to apply a transformation (translate, rotate, scale, etc).


I wrote the class with the
ability to apply vector algebra in mind, if I follow your suggestion,
would vector algebra be straight forward, regardless of which
ordinates I am working with?

I seriously hope so.

Context

StackExchange Code Review Q#71908, answer score: 3

Revisions (0)

No revisions yet.