954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Distance calculation using Class

Hi,
Am a total newbie to programming & trying to learn python thro pdf tutorials.

I have this working code which uses four numbers as arguments to calculate the distance between 2 points.
Tried to unsuccessfully convert it to use Class.
Can some one guide me & can I request a simple explaination or some pointers or links to very simple:) explainations for Class,objects, etc.

#Program to calculate distance so that it takes two Points as arguments instead of four numbers.

import math

class Point:
    pass

first = Point()
second = Point()

first.x = float(input('Pls enter the x co-ordinate of the first point:')) #eg 3.0
first.y = float(input('Pls enter the y co-ordinate of the first point:')) #eg 4.0

second.x = float(input('Pls enter the x co-ordinate of the second point:')) #eg 6.0
second.y = float(input('Pls enter the y co-ordinate of the second point:')) #eg 8.0

def dist(x1,y1,x2,y2):
    dist = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
    return dist

dist = dist(first.x, first.y, second.x, second.y)

print(dist)


Thanks

recyan
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Hi, Am a total newbie to programming & trying to learn python thro pdf tutorials.

I have this working code which uses four numbers as arguments to calculate the distance between 2 points. Tried to unsuccessfully convert it to use Class. Can some one guide me & can I request a simple explaination or some pointers or links to very simple:) explainations for Class,objects, etc.

#Program to calculate distance so that it takes two Points as arguments instead of four numbers.

import math

class Point:
    pass

first = Point()
second = Point()

first.x = float(input('Pls enter the x co-ordinate of the first point:')) #eg 3.0
first.y = float(input('Pls enter the y co-ordinate of the first point:')) #eg 4.0

second.x = float(input('Pls enter the x co-ordinate of the second point:')) #eg 6.0
second.y = float(input('Pls enter the y co-ordinate of the second point:')) #eg 8.0

def dist(x1,y1,x2,y2):
    dist = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
    return dist

dist = dist(first.x, first.y, second.x, second.y)

print(dist)

Thanks


Hi,

This appears to be working.

#Program to calculate distance so that it takes two Points as arguments instead of four numbers.

import math

class Point:
    pass

first = Point()
second = Point()

first.x = float(input('Pls enter the x co-ordinate of the first point:')) #eg 3.0
first.y = float(input('Pls enter the y co-ordinate of the first point:')) #eg 4.0

second.x = float(input('Pls enter the x co-ordinate of the second point:')) #eg 6.0
second.y = float(input('Pls enter the y co-ordinate of the second point:')) #eg 8.0

def dist(original,final):
    dist = math.sqrt((original.x - final.x) * (original.x - final.x) + (original.y - final.y) * (original.y - final.y))
    return dist

dist = dist(first, second)

print(dist)


Request you guys to correct me if I got it wrong as presently I have a very shallow understanding of the basic concepts. Also "can I request a simple explaination or some pointers or links to very simple:) explainations for Class,objects, etc."

Thanks

recyan
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Although your method works, you are not really using the features of class. You can google for "Byte of Python". It is in my opinion one of the best tutorials for beginners.

cghtkh
Junior Poster
102 posts since Sep 2010
Reputation Points: 21
Solved Threads: 38
 

A good example of using a class is in this tutorial . Note especially the use of "self" and the indentation.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Hi Guys,

cghtkh
Although your method works, you are not really using the features of class. You can google for "Byte of Python". It is in my opinion one of the best tutorials for beginners.

I fully agree with you. I am trying to use class without really having understood what it is. I have "Byte of Python" by "Swaroop C H" but had not got down to reading it. Will start off immediately.

woooeeA good example of using a class is in this tutorial. Note especially the use of "self" and the indentation.

Thanks for the link.

recyan
Newbie Poster
3 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You