5

I am trying to create a class in python titled "Point." I am trying to create a point on a coordinate plane x and y and track them. As well as find the distance between the points. I have to use functions and methods. I have started and here is my code. I am just not sure how to use it when I go to execute the program. Any help will be appreciated.

EDIT: Updated Code

import math


class Point(object):
    '''Creates a point on a coordinate plane with values x and y.'''

    COUNT = 0

    def __init__(self, x, y):
        '''Defines x and y variables'''
        self.X = x
        self.Y = y

    def move(self, dx, dy):
        '''Determines where x and y move'''
        self.X = self.X + dx
        self.Y = self.Y + dy

    def __str__(self):
        return "Point(%s,%s)"%(self.X, self.Y) 


    def getX(self):
        return self.X

    def getY(self):
        return self.Y

    def distance(self, other):
        dx = self.X - other.X
        dy = self.Y - other.Y
        return math.sqrt(dx**2 + dy**2)

    def testPoint(x=0,y=0):
        '''Returns a point and distance'''
        p1 = Point(3, 4)
        print p1
        p2 = Point(3,0)
        print p2
        return math.hypot(dx, dy)

    print "distance = %s"%(testPoint()) 

I still need help understanding how to actually use the code. That's why I created the testPoint function. When I actually go to execute the code in IDLE, how do I prove that everything works? Thanks a bunch guys!!

I also need to add code to the constructor to increment COUNT by 1 every time a Point object is created. I also need to add appropriate code so that points can be compared using the comparison operators while 'points' are compared based on their distance from the origin.

2
  • 4
    don't write getters in python unless/until you have to make attributes into properties for whatever reason. Commented Sep 18, 2012 at 1:01
  • i would create x and y functions and use them as properties as shapely does it. Commented Oct 24, 2017 at 12:55

4 Answers 4

7

Don't forget math.hypot

def distance(self, p):
    dx = self.X - p.X
    dy = self.Y - p.Y
    return hypot(dx, dy)
Sign up to request clarification or add additional context in comments.

Comments

4
  • You declared distance as taking an argument p; inside the method you're referring to it as other. Change p to other in the declaration so they match.

  • sqrt() isn't a builtin; you need to do import math and refer to it as math.sqrt().

  • You aren't doing anything with the testPoint() function you declare; you can invoke it by adding a line at the end like:

print "distance = %s"%(testPoint())

At that point, your code works and computes a distance of 4.0 between your points.

Now, some style issues:

  • In Python, you don't generally privatize member variables, and you don't bother writing trivial getters and setters, so you can remove the getX() and getY() methods and just refer to p.X and p.Y directly given a Point p.

  • The math module has a convenient hypotenuse function, so in distance() you can change the return line to return math.hypot(dx,dy).

  • By default, a user defined object has an unattractive string representation:

    <__main__.Point object at 0x1004e4550>

You should define a string conversion method in your class like so:

    def __str__(self):
        return "Point(%s,%s)"%(self.X,self.Y)

This will be used when the object is printed, or otherwise needs to be converted to a string.

3 Comments

Thank you sir! Very helpful and thorough. I have applied these changes. I also need to add code to the constructor to increment COUNT by 1 every time a Point object is created. I also need to add appropriate code so that points can be compared using the comparison operators while points are compared based on their distance from the origin. Finally, I still need help understanding how to execute all this code. Thats what the testPoint function is for.
Seems like you've got a handle on it. p1.move( 2, 7 ) moves p1 from its current position to (5, 11). p2.Y is 0. Define the comparison operators described here: docs.python.org/reference/datamodel.html#object.__lt__ to compare points by distance from origin (though you can probably get away with just defining __cmp__()).
Where would I implement the p1.move and stuff. Is p1 a variable? Thats the part I am confused on
3

Hum, why not use complex instead of a point class? It has all the properties you are searching for and more (such as rotation).

Here is an example to "OOP" the complex with a pedantic notation:

https://gist.github.com/jul/9286835

Comments

1

In your Point.distance method, you reference other.X and other.Y; Other does not exists.

You should either change the distance signature to be distance(self, other) or change the code to use p.

You will also need to import math.sqrt:

from math import sqrt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.