Finding the euclidean distance between the two points

..just learning Python

Moderators: KDoiron, ChrJim, mawe, python

Finding the euclidean distance between the two points

Postby xfirex on Fri Aug 28, 2009 2:58 am

Hi there, I have a question that asks;

Add a method, called distToPoint that takes another point, p, as an argument and returns the euclidean distance between the two points. Feel free to use math.sqrt.

so far all I can come up with is;

Code: Select all
def distToPoint(self, p):
    return math.sqrt


I've tried looking up methods of calculating the euclidean distance but I can't seem to find anything. Can some1 help me with this?
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am

Re: Finding the euclidean distance between the two points

Postby akaii on Fri Aug 28, 2009 3:03 am

I've tried looking up methods of calculating the euclidean distance but I can't seem to find anything. Can some1 help me with this?


...seriously? This is very basic stuff. And a quick lookup on wiki or google will quickly give you an answer.

I'm assuming you mean two points on a flat surface (ie, 2d cartesian plane)... which is simply:

sqrrt((x1-x2)^2 + (y1-y2)^2)

Google if you need the general formula for getting the distance between 2-pts with any number of coordinates.
akaii
New Python User
New Python User
 
Posts: 11
Joined: Fri Aug 28, 2009 1:18 am

Re: Finding the euclidean distance between the two points

Postby xfirex on Fri Aug 28, 2009 3:12 am

no c i've tried that alrdy but i just get this error;

Traceback (most recent call last):
File "/tmp/s4204505.py", line 24, in ?
main()
File "/tmp/s4204505.py", line 21, in main
f.write(str(foo.distToPoint(bar)))
File "/tmp/s4204505.py", line 15, in distToPoint
return math.sqrt((x1-x2)^2 + (y1-y2)^2)
NameError: global name 'x1' is not defined

I'm trying to find a method that doesn't require me to define anything
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am

Re: Finding the euclidean distance between the two points

Postby xfirex on Fri Aug 28, 2009 3:20 am

Anyway it says i only need to use 2 point so my guess would be to use just x1 and y1.

However, even when I do define them I still get an error.

Code: Select all
def distToPoint(self, p):
    x1 = Point(1,2)
    y1 = Point(3,4)
    return math.sqrt((x1-y1)^2)


TypeError: unsupported operand type(s) for -: 'instance' and 'instance'
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am

Re: Finding the euclidean distance between the two points

Postby akaii on Fri Aug 28, 2009 3:25 am

xfirex wrote:no c i've tried that alrdy but i just get this error;

Traceback (most recent call last):
File "/tmp/s4204505.py", line 24, in ?
main()
File "/tmp/s4204505.py", line 21, in main
f.write(str(foo.distToPoint(bar)))
File "/tmp/s4204505.py", line 15, in distToPoint
return math.sqrt((x1-x2)^2 + (y1-y2)^2)
NameError: global name 'x1' is not defined

I'm trying to find a method that doesn't require me to define anything


...I think, the bigger problem is that you haven't gotten the fundamentals of python (or any other language) down yet at this point, and possibly basic analytical geometry. That x1 is supposed to be the x attribute of your Point class instance, however you might have defined it. x2 is supposed to x attribute of the point p you passed to your method. Likewise for y. I have no idea how the rest of your Point class, other than the method you posted, is actually defined, so of course, I can't predict the correct identifier to use.

I didn't expect you to actually copy and paste what I posted, even though that line, coincidentally, is in correct python syntax.
akaii
New Python User
New Python User
 
Posts: 11
Joined: Fri Aug 28, 2009 1:18 am

Re: Finding the euclidean distance between the two points

Postby xfirex on Fri Aug 28, 2009 3:33 am

Well i'm sorry but I'm really struggling with understanding this "stupid" language, I don't even want to learn it but I have too for my degree.

I'm sick of all you ppl that say this is "simple", "basic", "You should be able to do this". Well, if you weren't so selfish of thinking only about your own skills, you would c that there r some ppl out there in the world that just don't understand the same things as you.

I came here to get some direct help, maybe some explanations on how to do these questions instead of spending an hour or more searching for an answer which might not even be right, but all I get is your excuses of being an "Expert".

Thanks Alot...
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am

Re: Finding the euclidean distance between the two points

Postby akaii on Fri Aug 28, 2009 3:47 am

Well i'm sorry but I'm really struggling with understanding this "stupid" language, I don't even want to learn it but I have too for my degree.


What is your course? Why did you choose to take this course if you had no interest in the course matter, and if you lacked sufficient background (basic analytical geometry) for it? If you're already having trouble at this stage, wouldn't it be prudent to reconsider changing your course while it's relatively early? Imagine how much worse it will be down the road.

I'm sick of all you ppl that say this is "simple", "basic", "You should be able to do this". Well, if you weren't so selfish of thinking only about your own skills, you would c that there r some ppl out there in the world that just don't understand the same things as you.


I (and whoever else you're referring to), am not telling you its basic out of spite or to belittle you. I'm telling you it's basic because it's truly basic. I had thought at first that you might be in early high school (or perhaps even a genius early starter from grade school), but since you mentioned a degree, you must be an undergrad. The implication of this is that this is the easy part. If you are struggling at this stage, imagine how much worse it will be when you're taking classes that are no longer "basic".

There is nothing wrong with not being able to understand something, not everyone has the same affinity for all subject matter. However, I hope you consider what's in your best interest here. You seem to loathe the subject matter, and lack drive, background and natural aptitude for it. Wouldn't it be best for you to take a course you'll enjoy and be motivated in? That you'll excel in? Not everyone needs to graduate with a computer-related degree.

I came here to get some direct help, maybe some explanations on how to do these questions instead of spending an hour or more searching for an answer which might not even be right, but all I get is your excuses of being an "Expert".


The search should take you 30 seconds. Searching for answers online or in books is an even more basic skill everyone will need later down the road, whatever your course. I would think even high school students, in this day and age are accustomed to using Google and Wikipedia for basic information at least, even if they are discouraged by their teachers from using them for research work?
akaii
New Python User
New Python User
 
Posts: 11
Joined: Fri Aug 28, 2009 1:18 am

Re: Finding the euclidean distance between the two points

Postby xfirex on Fri Aug 28, 2009 4:05 am

This course is called "Introduction" to Software Engineering I. It's compulsory for my Bach of IT.

It's true that I have no "drive" to do this stuff. Compared to HTML, which I find extremely easy and more fun to do, python is just a whole different story.

I've considered of quitting the course but I'll have to do it at some stage, so why not just finish it now as I'm already half-way through the semester.

See I want a career in the game industry as a graphic's designer. For example, creating the terrian and characters to games. I have no interest in the programming stuff, but more of the visual stuff. The university I go to says that the best courses that will help with being in the game industry is a Bach of IT or a Bach of Multimedia.

Both of these, have this course in it as an complusory course. So, can you see my problem.

P.S. I'm sorry for the rant, It's just that I've spent several hours already today on this question and several others and I'm getting a bit pissed atm.
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am

Re: Finding the euclidean distance between the two points

Postby akaii on Fri Aug 28, 2009 4:32 am

This course is called "Introduction" to Software Engineering I. It's compulsory for my Bach of IT.

It's true that I have no "drive" to do this stuff. Compared to HTML, which I find extremely easy and more fun to do, python is just a whole different story.

I've considered of quitting the course but I'll have to do it at some stage, so why not just finish it now as I'm already half-way through the semester.

See I want a career in the game industry as a graphic's designer. For example, creating the terrian and characters to games. I have no interest in the programming stuff, but more of the visual stuff. The university I go to says that the best courses that will help with being in the game industry is a Bach of IT or a Bach of Multimedia.

Both of these, have this course in it as an complusory course. So, can you see my problem.

P.S. I'm sorry for the rant, It's just that I've spent several hours already today on this question and several others and I'm getting a bit pissed atm.


Have you checked the rest of your course? I don't know what school you go to, but an "IT" course is nearly guaranteed to have loads of programming and software engineering classes, so you're problem won't be going away. Unless you grow into liking it, you won't learn. And if you don't learn, even if you do pass the class, your wasting your time. It's an opportunity cost. Why didn't you take a Bach of Multimedia? It seems like a safer choice for you, relatively.

From personal experience, most graphic designer's I know and have worked with graduated from an arts or design course, so an IT degree seems like an odd choice to me. Mind you, a programming background WILL give you a tremendous advantage as a designer over those that don't have one in the games industry, but it isn't neccessary. Any course will work for that job. Generally, what's more important is having a portfolio of designs when you apply for the job. On your actual job, the most important thing is being familiar with design tools that are commonly used... Struggling with your programming classes might be seriously taking time away from any design work you could do in uni.
akaii
New Python User
New Python User
 
Posts: 11
Joined: Fri Aug 28, 2009 1:18 am

Re: Finding the euclidean distance between the two points

Postby waz on Fri Aug 28, 2009 8:03 am

xfirex wrote:I've tried looking up methods of calculating the euclidean distance but I can't seem to find anything. Can some1 help me with this?



Code: Select all
     y

     ^
   6 |
   5 |
   4 |
   3 |
   2 |
   1 |
     +-----------> x
       1 2 3 4 5

Does this look vaguely familiar at all?


Code: Select all
     y

     ^
   6 |         b
   5 |
   4 |
   3 |   a
   2 |
   1 |
     +-----------> x
       1 2 3 4 5

So let's pick two random points on our graph.


Code: Select all
     y

     ^
   6 |         b
   5 |       /
   4 |     /
   3 |   a
   2 |
   1 |
     +-----------> x
       1 2 3 4 5

If we want to find the "Euclidean distance" between the two points, that just means the distance between them if we put a ruler on the screen and measured the distance of the line that connects them.


Code: Select all
     y

     ^
   6 |---------b
   5 |         |
   4 |         |
   3 |---a     |
   2 |   |     |
   1 |   |     |
     +-----------> x
       1 2 3 4 5

They have coordinates a = (2, 3) and b = (5, 6), where point = (x-coordinate, y-coordinate).


The "Euclidean distance" formula is what's commonly referred to as simply the "distance formula" (you may or may not remember it from earlier math classes, years ago), and looks like this:

Code: Select all
        ______________________________
d =    /           2               2
     \/ (a_x - b_x)  +  (a_y - b_y)

That is, subtract the x-coordinates and square the result, subtract the y-coordinates and square the result, add both of those results, and finally take the square root of all that.

So for our problem above, the distance between a and b is:

Code: Select all
d = square-root( (a_x - b_x)^2 + (a_y - b_y)^2 )
  = square-root( (2 - 5)^2 + (3 - 6)^2 )
  = square-root( (-3)^2 + (-3)^2 )
  = square-root( 9 + 9 )
  = square-root( 18 )
  = 4.2426...


Now for Python you'd want to replace "square-root()" with "math.sqrt()" and "^" with "**" but everything else is essentially the same.

By the way, the first two Google results for "euclidean distance" are most enlightening. ;)
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm

Re: Finding the euclidean distance between the two points

Postby xfirex on Fri Aug 28, 2009 9:52 pm

Thank you, I understand now. However, I'm still getting an error.

NameError: global name 'a_x' is not defined

I've tried defining them but the program I use does several test cases and when I define them the answer for all the test cases are the same.
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am

Re: Finding the euclidean distance between the two points

Postby uafomega on Fri Aug 28, 2009 11:24 pm

Could you post your code for us to see?
As far as I know,(still a noob python user myself), those error usually indicate trying to call a variable from an assignment made inside a function from outside the function. If so, you will have to declare the assignment as a global assignment first since anything typed inside a function is automatically declared as a local variable. In other words, local declaration means that variables made inside a function only exist when that function is called and are immediately deleted when the function is over. A global declaration keep the assignment after the function is over for use later in the program. For instance:
Code: Select all
def function():          # creates a function
   yes = 'heya'          # assigns a variable inside the function.
   print(yes)            # prints the variable when the function is called.

   
function()               # Calls the function
heya                     # this is what the function prints as
                         # indicated in the function.
print(yes)               # from outside the function, tries to print the variable inside the function but fails.
Traceback (most recent call last): # This is the error you get correct?
  File "<pyshell#5>", line 1, in <module>
    print(yes)
NameError: name 'yes' is not defined

You can not call on assignments made inside a function unless you declare the assignment as a global variable.
For instance:
Code: Select all
def function():
   global yes
   yes = 'heya'
   print(yes)

   
function()
heya
print(yes)
heya
uafomega
Python User
Python User
 
Posts: 74
Joined: Sat Nov 29, 2008 1:40 pm

Re: Finding the euclidean distance between the two points

Postby xfirex on Sat Aug 29, 2009 1:44 am

Code: Select all
def distToPoint(self, p):
    return math.sqrt((a_x - b_x)**2 + (a_y - b_y)**2)


thats what I wrote before I defined.

Code: Select all
def distToPoint(self, p):
    a_x = 1
    b_x = 2
    a_y = 3
    b_y = 4
    return math.sqrt((a_x - b_x)**2 + (a_y - b_y)**2)


This is what I wrote to define.

The problem is that when I define I get the same answer to all the test cases and when I don't define I get an error.
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am

Re: Finding the euclidean distance between the two points

Postby bvdet on Sat Aug 29, 2009 1:25 pm

You are adding a method to a point class, right? A point object for R3 should have x, y and z attributes. One might expect a point class to also have an operator overload such as __sub__ for the subtraction operator. Then you can do this:
Code: Select all
    def dist(self, other):
        p = self-other
        return ....put the calculation here....


You don't need math.sqrt. The power operator works with a value less that one. math.sqrt(n) is equivalent to n**0.5. The cube root of n is n**(1/3.0).

Example:
Code: Select all
>>> p1 = Pt(1,2,3)
>>> p2 = Pt(3,2,1)
>>> p3 = Pt(p1.x-p2.x, p1.y-p2.y, p1.z-p2.z)
>>> p3.x
-2
>>> p3.y
0
>>> p3.z
2
>>> p1.dist(p2)
2.8284271247461903
>>>
User avatar
bvdet
Python Heavy Programmer
Python Heavy Programmer
 
Posts: 435
Joined: Tue Jun 12, 2007 6:04 pm
Location: Nashville, TN

Re: Finding the euclidean distance between the two points

Postby xfirex on Sun Aug 30, 2009 2:47 am

Thanks ppl I've solved it...I just had an error in that define part, I used the wrong letters to define them. lol

I got another question if any1 is interested?

Add a method, called addPoint that takes another point, p, as an argument, and changes this point so that it is the sum of this point's old value and the value of p.

so all I've written at this point is;

Code: Select all
def addPoint(self,p):


lol

What I'm confused with is what does it mean by by old value and the value of p?
I've been given this test case:

Test case:
foo = Point(1,2)
bar = Point(3,4)
foo.addPoint(bar)
foo.x ===> 4
foo.y ===> 6

What I can c is, it's adding 1 and 3 and 2 and 4. But I don't know the strip of code that would be used to do this. I was thinking of maybe using a sum operation with the values of x and y, but I can't be sure it would work.
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am

Re: Finding the euclidean distance between the two points

Postby waz on Sun Aug 30, 2009 11:20 am

Code: Select all
def addPoint(self,p):
    # add p.x to self.x
    # add p.y to self.y
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm

Re: Finding the euclidean distance between the two points

Postby laurafisher87 on Sun Aug 30, 2009 3:31 pm

Code: Select all
def distToPoint(self, p):
    v1 = (p.x - self.x)**2 + (p.y - self.y)**2
    v2 = math.sqrt(v1)
    return v2


hi there, i think i must be in the course you're in at uni. this is what i came up with.

this is my first post on this forum, so sorry if i've messed up that code formatting!
laurafisher87
Python Fan
Python Fan
 
Posts: 5
Joined: Sun Aug 02, 2009 12:29 am

Re: Finding the euclidean distance between the two points

Postby laurafisher87 on Sun Aug 30, 2009 3:56 pm

Code: Select all
def addPoint(self, p):
    self.x = self.x + p.x
    self.y = self.y + p.y


and this is that other one as well. it was tricky cause you had to change the value specifically rather than just return or print it... (well, tricky to us newbies) ;)
laurafisher87
Python Fan
Python Fan
 
Posts: 5
Joined: Sun Aug 02, 2009 12:29 am

Re: Finding the euclidean distance between the two points

Postby xfirex on Sun Aug 30, 2009 6:25 pm

Yea I already did it last night, I did a similar but different method...

I'm still stuck on the rectangle one.

EDIT: Thanx waz thats what I came to after an hour of thinking about it.
xfirex
Python User
Python User
 
Posts: 50
Joined: Fri Jul 31, 2009 2:54 am


Return to Beginners..

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 4 guests


Sponsored by Dreamlink Web hosting and Traduzioni Rumeno Italiano and ASSP Deluxe for cPanel.