I am writing something which could:
1) take a point
2) find distance to origin
3) compare to one before and submit closest one
4) if within 0.01 of origin it terminates
It does compile but it does not work? it outputs odd numvers e.g for (10,9) it would submit something like ''the closest point to the origin so far: 8.00831e-307 8.00991e-307'' what is wrong?
#include <iostream>
#include <cmath>
struct point{
double x;
double y;
};
double pdistance (point p1, point p2);
int main(){
point pa, pb, o;
double d1, d2;
o.x = 0;
o.y = 0;
d2=0;
// intialising d2 to to make the loop correct
do{
std::cout<< " enter the co-ordiantes of a point" << std::endl;
std::cin >> pa.x >> pa.y ;
d1 = pdistance(pa, o);
if(d1<d2){
std::cout << " the closest point to the origin so far: " << pa.x << " " << pa.y << std::endl;
}
else{
std::cout << " the closest point to the origin so far: " << pb.x << " " << pb.y << std::endl;
}
d2=d1;
//storing the value of d1 into d2 here so it can be compared once another point is put in
}while(d1||d2 > 0.01);
}
double pdistance(point p1, point p2){
return std::sqrt(
std::pow((p1.x-p2.x), 2)+
std::pow((p1.y-p2.y), 2)
);
}