Distance between 2 points

Hi, im having alot of trouble and i dont know why. Im trying to make a program in visual basic 2010 that i must write a function "double distance(Point p, Point q)"
that computes the distance between two points, then write a test program that asks the user to select two points then display the distance using my function. I am stuck on gettin the distance from the user inputted points. This is what i have so far.

#include "ccc_msw.h"
#include <iostream>


using namespace std;

//Create the function for the distance formula between points p and q
double distance(Point p, Point q)
{
double distance = sqrt((x2-x1)^2 + (y2-y1)^2);
return 0;
}

int ccc_win_main()
{
Point p = cwin.get_mouse("Click on the screen to define point one");
cwin << p;

Point q = cwin.get_mouse("Click on the screen to define point two");
cwin << q;

double calculateDistance = distance(p , q);
return 0;
}

I need to know how to get the x1,x2,y1, and y2 in my distance function
You need to use p and q. It's probably p.x and q.x. But it might be something else depending on what Point's members are.

Also, you should return the distance. It doesn't make sense to calculate the distance only to discard it and return 0. Naming a var 'distance' when your function is also named 'distance' is also probably not a great idea.

Lastly, note that ^ is not an exponent operator in C++, it's a XOR operator. So that code won't do what you want. To square stuff, you should multiply it by itself.

1
2
3
double x = p.x - q.x;
// then to square it:
x = x*x;  // squared 
Last edited on
You need the members of Point, probably x and y. ^ is the bitwise XOR operator in C++, you can either write x*x or use pow(x,2) from the cmath header.

Just for the sake of curiosity, what exactly is cwin?
Topic archived. No new replies allowed.