Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > tuples and cartesian coordinates

Reply
Thread Tools

tuples and cartesian coordinates

 
 
Gerrit Holl
Guest
Posts: n/a
 
      12-17-2003
Hi,

the FAQ says:

> For example, a Cartesian coordinate is appropriately represented as a
> tuple of two or three numbers.


I find it strange to use tuples for a coordinate. After all, a
coordinate represents a position of an object. Suppose I have a game
where the player has a position: isn't it stupid to use tuples rather
than lists or another type (maybe complex numbers?), because I want to
be able to change the position?

Shouldn't coordinates be mutable?

yours,
Gerrit.

--
231. If it kill a slave of the owner, then he shall pay slave for slave
to the owner of the house.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-17-2003
Gerrit Holl wrote:
> Hi,
>
> the FAQ says:
>
>
>>For example, a Cartesian coordinate is appropriately represented as a
>>tuple of two or three numbers.

>
>
> I find it strange to use tuples for a coordinate. After all, a
> coordinate represents a position of an object. Suppose I have a game
> where the player has a position: isn't it stupid to use tuples rather
> than lists or another type (maybe complex numbers?), because I want to
> be able to change the position?
>
> Shouldn't coordinates be mutable?


Coordinates are used to identify a point in a plane or space. The fact
that some object move from point A to point B doesn't mean that point A
has moved to point B !-)

class Movable:
def __init__(self, initialPosition):
self.position = initialPosition

def moveTo(self, newPosition):
self.position = newPosition

def moveBy(self, deltaX, deltaY):
self.position = (self.position[0] + deltaX,
self.position[1] + deltaY)

def getPosition(self):
return self.position

def getPositionX(self):
return self.position[0]

def getPositionY(self):
return self.position[1]

points = [(x, y) for x in range(10) for y in range(10)]

m = Movable(points[0])
m.moveTo(points[55])
m.moveBy(-1, 1)
m.getPosition()
-> (4,6)

Bruno

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-17-2003
Gerrit Holl wrote:

> the FAQ says:
>
>> For example, a Cartesian coordinate is appropriately represented as a
>> tuple of two or three numbers.

>
> I find it strange to use tuples for a coordinate. After all, a
> coordinate represents a position of an object. Suppose I have a game
> where the player has a position: isn't it stupid to use tuples rather
> than lists or another type (maybe complex numbers?), because I want to
> be able to change the position?
>
> Shouldn't coordinates be mutable?


One benefit of immutable coordinates: you can safely assign one coordinate
to multiple positions:

class Shape: pass
s1 = Shape()
s2 = Shape()

origin = ImmutableCoord(0, 0)
s1.position = origin
s2.position = origin

Whereas with mutable coordinates you would need

class Shape:
def __init__(self, pos):
self.pos = MutableCoord(pos)
def setPosition(self, pos)
self.pos.x = pos.x
self.pos.y = pos.y
s1.setPosition(origin)
s2.setPosition(origin)

There are several variants of this scheme, but I think they all tend to
complicate the design, because not only rebinding of pos needs to be
tracked, but also the changing of pos.x and pos.y.

By the way, complex numbers are immutable:

>>> z = 1 + 3j
>>> z.imag = 5

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: readonly attribute

Peter
 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      12-17-2003
Gerrit Holl wrote:
> Hi,
>
> the FAQ says:
>
>
>>For example, a Cartesian coordinate is appropriately represented as a
>>tuple of two or three numbers.

>
>
> I find it strange to use tuples for a coordinate. After all, a
> coordinate represents a position of an object. Suppose I have a game
> where the player has a position: isn't it stupid to use tuples rather
> than lists or another type (maybe complex numbers?), because I want to
> be able to change the position?
>
> Shouldn't coordinates be mutable?
>
> yours,
> Gerrit.
>


Depends. Does it make logical sense for the coordinate to be mutable
in the context of your app?

If an object is returning an instrumentation value, as in
positionNow = spaceship.getPosition()
then it does make sense to be mutable. Of course the spaceship itself
may well keep a mutable position internally that it changes in
response to
setThrusters(fullSpeedAhead)

Steve
 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      12-18-2003

"Gerrit Holl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> the FAQ says:
>
> > For example, a Cartesian coordinate is appropriately represented as a
> > tuple of two or three numbers.

>
> I find it strange to use tuples for a coordinate. After all, a
> coordinate represents a position of an object. Suppose I have a game
> where the player has a position: isn't it stupid to use tuples rather
> than lists or another type (maybe complex numbers?), because I want to
> be able to change the position?
>
> Shouldn't coordinates be mutable?


I would think not. I look at them as Value Objects: that is,
objects which serve to encapsulate a value and which don't
have any identity outside of that function.

So if you're not going to create an object for them, I'd
regard a tuple as perfectly adequate.

John Roth
>
> yours,
> Gerrit.
>
> --
> 231. If it kill a slave of the owner, then he shall pay slave for slave
> to the owner of the house.
> -- 1780 BC, Hammurabi, Code of Law
> --
> Asperger's Syndrome - a personal approach:
> http://people.nl.linux.org/~gerrit/english/
>



 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      12-18-2003

"Gerrit Holl" <(E-Mail Removed)> wrote in
message
news:(E-Mail Removed)...
> Shouldn't coordinates be mutable?


Coordinates no, positions possibly yes, as long as
you avoid the mutiple reference trap others have
described. My philosophy on the tuple vs. list
debate is that people should use what works for
their particular application.

TJR


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sperical and Cartesian Coordinates ldries46 C++ 8 04-18-2013 10:08 PM
Dictionaries with tuples or tuples of tuples Jon Reyes Python 18 02-19-2013 03:56 AM
Cartesian and polar coordinates library Bart Braem Ruby 5 08-23-2006 06:59 PM
Converting canvas coordinates to window coordinates in tkinter......... Suresh Kumar Python 0 07-04-2003 10:14 AM
Converting canvas coordinates to window coordinates....... Suresh Kumar Python 0 07-03-2003 04:53 AM



Advertisments