i have to write a small program with coordinates, but i stuck.

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;
}
the point, which is the closest to the circle
what is the closes point to the circle? if two points are inside which one is the closest?
The problem is in you formula in line 112. |distance-radius| doesn't work You are doing |distance2 - r|.

Try to use functions or methods and the code will be cleaner.
1
2
3
4
5
6
7
8
9
10
struct point{
  float x,y; //or float coord[2] so you can expand more easily
  point operator-(const point &b) const; //component to component (actually is a vector)
  float norm(); //norm of the vector (aka distance to origin)
};
int main(){
  //to know the distance from every point to the centre of the circle
  (p[i]-centre).norm();
  return 0;
}
Last edited on
I mean, it has to choose the point which is closest to the "circumference", not the centre.
The distance between two points (the centre and a point) is d=√(u-x)^2+(v-y)^2 So that's why i thought that |d-r| will give a positive number, and the program has to choose the smallest number, and also print out the index of it.
So the actual problem is, that i don't know how to print out the index of the number.
Once in a lesson i had a very similar task, but in that, we just put the distance equation in struct, than did this (we only had x and y coordinates, and no u and v, because the centre was (0;0)):

1
2
3
4
5
6
7
8
9
10
int maxindex=0;
float maxvalue=p[0].distance();

for (int i=1; i<n; i++){
float d=p[i].distance();
if(d>maxvalue){
maxindex=i;
maxvalue=d;
}
}


So i have to do something similar, but i can't, since i can't put the equations in struct, because the program gets the value of u and v only in line 62.
Again, you're forgetting sqrt in line 112.
Compare this two:
110
111
112
113
114
115
116
117
118
119
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;
            }
        }
1
2
3
4
5
6
7
8
9
10
int maxindex=0;
float maxvalue=p[0].distance();

for (int i=1; i<n; i++){
  float d=p[i].distance();
  if(d>maxvalue){
    maxindex=i; 
    maxvalue=d;
  }
}
Do you see the scope problem?

You could also fill an array of floats with all the distances to the circumference and then apply "index of the maximum element" function
I guess i can see it now, i hope it's ok (it works fine now when i run it, but i only started learing c++ in school a month ago, and i'm really not sure about my code)

so i put the two distances into struct like this:

1
2
3
4
struct point {
    float x, y;
    float distance, d;
    };


and i changed my code to this:

1
2
3
4
5
6
7
8
9
10
11
12
//The closest point to the circle

        for (int i=0; i<n; i++){
            int maxindex=0;
            p[i].distance=fabs(sqrt((u-(p[i].x))*(u-(p[i].x))+(v-(p[i].y))*(v-(p[i].y)))-r);
            float maxvalue=p[0].distance;

            if(p[i].distance<=maxvalue){
            maxindex=i;
            cout<<"The "<<maxindex<<"th point is the closest to the circle, and the distance is:"<<p[i].distance<<endl;
            }
        }


EDIT: Sorry, it does not work fine. The problem is, that when there are like 4 points, and all the distances are smaller than the first one, it prints out all of them. What should i do?
Last edited on
maxindex is set to 0 every time in the loop. Same for maxvalue.
You will not get the maximum element till you finish the loop. So print outside the loop. (check http://www.cplusplus.com/forum/beginner/31635/#msg171308 )
Last edited on
Thanks, it works now!
Another question: i realized, that the first part is, that the program has to decide WHERE are more points, in the circle, or beyond the circle.
So i changed my code to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
          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)));}

                int db=0;
                if(d<=r){db++;}

                int db2=0;
                if (d>r){db2++;}

                bool inthecircle=(db>db2);

                if (inthecircle){
                cout<<"There are more points in the circle."<<endl;
                }
                if (!inthecircle){
                cout<<"There are more points beyond the circle."<<endl;
                }


The problem is, that the decision depends on the last two coordinates. If the last point is in the circle, it says "More points are in the circle", even if it's not true.
However, my code has to be similar to this:

1
2
3
4
5
6
7
8
    int i=0;
    while(i<=n-3 && !whatever)
        i++;
    bool therewere=i<=n-3;
    if(therewere)
        cout<<"There were something."<<endl;
    else
        cout<<"There weren't something."<<endl;
Topic archived. No new replies allowed.