Re: [Tutor] line class

2008-07-09 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> wrote Usually based on magnitude alone. That seems a pretty strange definition of equal, that makes (1, 0) == (0, 1). Yes I know! But actually in many engineering situations where phase is not important it's a good first approximation (for example power ca

Re: [Tutor] line class

2008-07-09 Thread Kent Johnson
On Wed, Jul 9, 2008 at 2:17 PM, Alan Gauld <[EMAIL PROTECTED]> wrote: > "Kent Johnson" <[EMAIL PROTECTED]> wrote >> >> That just raises the question of how do complex numbers compare? > > Usually based on magnitude alone. > That's why I said the results would be equivalent to the length of a point

Re: [Tutor] line class

2008-07-09 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> wrote That just raises the question of how do complex numbers compare? Usually based on magnitude alone. That's why I said the results would be equivalent to the length of a point approach. You assume that any point on the same sperical locus is equal. At

Re: [Tutor] line class

2008-07-09 Thread Kent Johnson
On Wed, Jul 9, 2008 at 3:05 AM, Alan Gauld <[EMAIL PROTECTED]> wrote: > > Rather than comparing in that manner I'd take a different approach. > I'd measure the length from the origin thus any point that was inside > the circle upon whose ciorcumference the point sits is less than > the point. Any p

Re: [Tutor] line class

2008-07-09 Thread Alan Gauld
"Christopher Spears" <[EMAIL PROTECTED]> wrote def __cmp__(self, other): if self.x == other.x and self.y == other.y: return 0 elif self.x < other.x and self.y < other.y: return -1 elif self.x > other.x and self.y > other.y: return 1 Rather than comparing in that manner I'd

Re: [Tutor] line class

2008-07-08 Thread John Fouhy
On 09/07/2008, Paul McGuire <[EMAIL PROTECTED]> wrote: > > def length(self): > > dx,dy = self.p1 - self.p2 > > return (dx**2 + dy **2) ** 0.5 > > How about: > > def length(self): > return math.hypot( *(self.p1 - self.p2) ) > > Compiled C code will be much faster than squaring and

Re: [Tutor] line class

2008-07-08 Thread Paul McGuire
> def length(self): > dx,dy = self.p1 - self.p2 > return (dx**2 + dy **2) ** 0.5 How about: def length(self): return math.hypot( *(self.p1 - self.p2) ) Compiled C code will be much faster than squaring and square rooting. -- Paul ___ T

Re: [Tutor] line class

2008-07-08 Thread Kent Johnson
On Tue, Jul 8, 2008 at 6:29 PM, Christopher Spears <[EMAIL PROTECTED]> wrote: > I have been reading everyone's comments on my line class. I have decided to > implement some of the suggestions. Someone suggested that I create a > Point.__cmp__ method. Here is what I have so far: > > def __cmp__

Re: [Tutor] line class

2008-07-08 Thread Marc Tompkins
On Tue, Jul 8, 2008 at 3:29 PM, Christopher Spears <[EMAIL PROTECTED]> wrote: > I have been reading everyone's comments on my line class. I have decided > to implement some of the suggestions. Someone suggested that I create a > Point.__cmp__ method. Here is what I have so far: > > def __cmp__(

[Tutor] line class

2008-07-08 Thread Christopher Spears
I have been reading everyone's comments on my line class. I have decided to implement some of the suggestions. Someone suggested that I create a Point.__cmp__ method. Here is what I have so far: def __cmp__(self, other): if self.x == other.x and self.y == other.y: return 0

Re: [Tutor] line class

2008-07-08 Thread Alan Gauld
"Christopher Spears" <[EMAIL PROTECTED]> wrote class Point(object): def __init__(self, x=0.0,y=0.0): self.x = float(x) self.y = float(y) def __repr__(self): coord = (self.x,self.y) return coord You could add a couple of methods here to get deltaX and deltaY values Or ev

Re: [Tutor] line class

2008-07-08 Thread Paul McGuire
> > > def __init__(self,p1,p2): > > self.p1 = p1 > > self.p2 = p2 > > > > And since a line should not have zero length (although you might argue > > with that!) you could also check if > > p1==p2 > > In this case he should define Point.__cmp__() so the comparison is by value rather than iden

Re: [Tutor] line class

2008-07-08 Thread Kent Johnson
On Tue, Jul 8, 2008 at 4:01 AM, Alan Gauld <[EMAIL PROTECTED]> wrote: > def __init__(self,p1,p2): > self.p1 = p1 > self.p2 = p2 > > And since a line should not have zero length (although > you might argue with that!) you could also check if > p1==p2 In this case he should define Point.__cmp__

Re: [Tutor] line class

2008-07-08 Thread Kent Johnson
On Tue, Jul 8, 2008 at 12:52 AM, Christopher Spears <[EMAIL PROTECTED]> wrote: > For problem 13-6 out of Core Python Programming, I created a line class that > consists of two points. The line class has the following methods: __repr__, > length, and slope. Here is the code: >def __repr__(s

Re: [Tutor] line class

2008-07-08 Thread Alan Gauld
"Christopher Spears" <[EMAIL PROTECTED]> wrote class Point(object): def __init__(self, x=0.0,y=0.0): class Line(object): def __init__(self, p1, p2): self.p1 = Point(x1,y1) self.p2 = Point(x2,y2) This is wrong I suspect. You are passing two point objects into the constructor b

Re: [Tutor] line class

2008-07-07 Thread John Fouhy
On 08/07/2008, Christopher Spears <[EMAIL PROTECTED]> wrote: > Basically, if the two the x values are the same, I will get a > ZeroDivisionError. A line in this > case would simply point straight up. What would slope be in this case? I > will admit that > this is probably a math problem not a

[Tutor] line class

2008-07-07 Thread Christopher Spears
For problem 13-6 out of Core Python Programming, I created a line class that consists of two points. The line class has the following methods: __repr__, length, and slope. Here is the code: #!/usr/bin/python import sys,math class Point(object): def __init__(self, x=0.0,y=0.0): se