dist(x0,y0,x2,y2) < dist(x0,y0,x1,y1) ? cout << "Nearer than last time but it's outside yet. Try again\n" : cout << "Even not nearer than last time. Try again\n";
x1 = x2;
y1 = y2;
cin >> x2 >> y2;
}
cout << "Congratulations! Your point is inside now!";
Answers & Comments
#include <iostream>
#include <cmath>
#include <random>
using namespace std;
bool check(double x0, double y0, double R, double x, double y){
return abs(x0 - x) <= R + 1e-5 && abs(y0 - y) <= R + 1e-5;
}
double dist(double x0, double y0, double x, double y){
return sqrt(pow(x - x0, 2) + pow(y - y0, 2));
}
signed main(){
srand(time(NULL));
double x0, y0, R, x1, y1,x2,y2;
x0 = rand() / (rand() / 1.5);
y0 = rand() / (rand() / 1.5);
R = rand() / (rand() / 1.5);
cin >> x1 >> y1;
if(!check(x0,y0,R,x1,y1))
cout << "Your point is outside. Try again\n";
cin >> x2 >> y2;
while(!check(x0,y0,R,x2,y2)){
dist(x0,y0,x2,y2) < dist(x0,y0,x1,y1) ? cout << "Nearer than last time but it's outside yet. Try again\n" : cout << "Even not nearer than last time. Try again\n";
x1 = x2;
y1 = y2;
cin >> x2 >> y2;
}
cout << "Congratulations! Your point is inside now!";
}