I have to write a program, which asks for points (coordinates), a circle's centre (coordinates) and the circle's radius, and it counts the distance between a point and the centre, then the program
decides whether a point is inside or beyond the circle. (if the distance is smaller than the radius, it's inside.)
As you can see in the code below, i managed this task.
BUT the problem is, that the program also has to print out the index of the point, which is the closest to the circle. I figured it out, that |distance-radius| will work well, but i have serious problems with the coding.
As you can see, i tried using struct, and i think it would be good if i could put the distances into it as well, but i can't, since the program asks for the u and the v coordinates only in line 62. So it's not working.
The first part of the program works well, but the second is not. And even though i used float, in the end, it only prints out whole numbers. (like, "the distance is: 1" even though the distance was 1.73)
Any ideas for that? I really need help. What should i change in the first part to make the second part work properly?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
struct point {
float x;
float y;
};
int main()
{
int n;
float u;
float v;
float d;
float r;
string s;
//Asking for the number of points
bool wrong;
do{
cout<<"How many points:"; cin>>n;
wrong=cin.fail();
if (wrong || n<=0){
cin.clear();
cout<<"N has to be a number and bigger than 0\n";
getline(cin,s);
}
}while(wrong || n<=0);
//Asking for the coordinates of the points
point p[n];
for (int i=0; i<n; i++){
do{
cout<<i+1<<"th point's x coordinate:"; cin>>p[i].x;
wrong=cin.fail();
if (wrong){
cin.clear();
cout<<"x has to be a number\n";
getline(cin,s);
}
} while (wrong);
do{
cout<<i+1<<"th point's y coordinate:"; cin>>p[i].y;
wrong=cin.fail();
if (wrong){
cin.clear();
cout<<"y has to be a number\n";
getline(cin,s);
}
} while (wrong);
}
//Asking for the circle's coordinates
do{
cout<<"the X coordinate of the circle's centre:"; cin>>u;
wrong=cin.fail();
if (wrong){
cin.clear();
cout<<"x has to be a number\n";
getline(cin,s);
}
} while (wrong);
do{
cout<<"The Y coordinate of the circle's centre"; cin>>v;
wrong=cin.fail();
if (wrong){
cin.clear();
cout<<"y has to be a number\n";
getline(cin,s);
}
} while (wrong);
//Asking for the radius
do{
cout<<"The circle's radius:"; cin>>r;
wrong=cin.fail();
if (wrong || r<=0){
cin.clear();
cout<<"Radius has to be a number and bigger than 0\n";
getline(cin,s);
}
} while (wrong || r<=0);
//The distance between a point and the circle's centre
for (int i=0; i<n; i++){
d=sqrt((u-(p[i].x))*(u-(p[i].x))+(v-(p[i].y))*(v-(p[i].y)));
cout<<"The distance between the "<<i+1<<"th point and the centre is: "<<d<<" so"<<endl<<endl;
if(d<=r){cout<<"the"<<i+1<<"th point is inside the circle"<<endl<<endl;}
else
if(d>r){cout<<"the "<<i+1<<"th point is beyond the circle"<<endl<<endl;}
}
//The closest point to the circle
for (int i=1; i<n; i++){
int maxindex=0;
float distance=fabs(((u-(p[i].x))*(u-(p[i].x))+(v-(p[i].y))*(v-(p[i].y)))-r);
float maxvalue=fabs(((u-(p[0].x))*(u-(p[0].x))+(v-(p[0].y))*(v-(p[0].y)))-r);
if(distance<maxvalue){
maxindex=i;
cout<<"The "<<maxindex<<"th point is the closest, and the distance is:"<<distance<<endl;
}
}
return 0;
}
|