Hi! I have created a class named point, where I could get x and y coordinates of a point. Now I want to calculate the distance between two points. I've done this like that: pt1.distance(pt2), but now i would like to do the same like this, in order to implentate a lot of points: something like: distance(pt1, pt2); so, i don't want to access to the method distance by starting from a point. Is it possible? Thanks!
Hi , Suppose you have the class like
class CMilage
{
private:
int distance1 , distance2 ;
public:
CMilage() ;
int Distance( int dist1 , int dist2);
};
CMilage::CMilage()
{
distance1 = 0 ; distance2 = 0 ;
}
//This is your distance function
int CMilage::Distance(int dist1 , int dist2)
{
return ( dist1 + dist2 ) ;
}
int main()
{
int x = 10 , int y = 20 ;
CMilage miles;
cout <<"\n Total distance = "<< miles.Distance( x , y) ;
return 0 ;
}
#include "point.h"
#include "cmath"
point::point() { x = 0; y = 0; p = 0; ang = 0; }
point::point(float a, float b) {
x = a; y = b;
p = sqrt(pow(a,2)+pow(b,2));
ang = atan2(b, a);
}
float point::getX() { return x; }
float point::getY() { return y; }
void point::setCart(float a, float b) {
x = a; y = b;
p = sqrt(pow(x,2)+pow(y,2));
ang = atan2(y, x);
}
float point::dist( point b ) {
return sqrt(pow(b.getX() - x, 2) + pow(b.getY() - y, 2));
}
float point::getPolarP() { return p; }
float point::getPolarAng() { return ang; };
void point::setPolar(float a, float b) {
p = a; ang = b;
x = p*cos(ang);
y = p*sin(ang);
}