简体   繁体   中英

Check if theres a point that lies outside of circles

So i'm trying to make an console application that checks if it has any N(x,y) points outside of n circles.

So the program work perfect when there is one circle. But when i enter more than one circles, its not working properly.

Here is my code:

#include <stdio.h>
#include <iostream>
#include <cmath>

using namespace std;
struct tokr {
    float x, y, r; int br;
};
struct ttoc {
    float x, y;
};
tokr circ[30];
ttoc points[20];
int brokr, brtoc;
void readOkr(tokr* ok) {
    cout << "x: "; cin >> ok->x;
    cout << "y: "; cin >> ok->y;
    cout << "r="; cin >> ok->r;
}
void readToc(ttoc* t) {
    cout << "x :"; cin >> t->x;
    cout << "y :"; cin >> t->y;
}
int main()
{
  int n, brToc;
  float dx,dy,r;
  bool outside;

  cout << "Number of circles: ";
  cin >> n;

  for(int i = 0; i <n; i++) {
    readOkr(&circ[i]);
  }

  cout << "Number of points: ";
  cin >> brToc;

  for(int i = 0; i <brToc; i++) {
    readToc(&points[i]);
  }

  for(int i = 0; i<brToc; i++) {
    outside = false;
    for(int j = 0; j<n; j++) {
        dx = abs(points[i].x - circ[j].x);
        dy = abs(points[i].y - circ[j].y);
        r = abs(pow(circ[j].r,2));
        if(pow(dx,2) + pow(dy,2) > r) {
           outside = true;
           break;
        }
    }
    if(outside) cout << "Point: " << i+1 << " is outside \n";
  }


  return 0;
}
}

Tests:

With one circle:

一圈

With more than 1 circle:

超过 1

The not so relevant parts removed your loops are basically this:

for(int i = 0; i<brToc; i++) {
outside = false;
for(int j = 0; j<n; j++) {
    dx = ...;
    dy = ...;
    r = ...;
    if(pow(dx,2) + pow(dy,2) > r) {
       outside = true;
       break;
    }
}
if(outside) cout << "Point: " << i+1 << " is outside \n";

You should compare to r*r , because distance is sqrt(dx^2 + dy^2) . However, thats not the main problem.

Your flag outside has the logic flipped. You start by saying that the point is inside a circle and as soon as you find one circle that does not cover the point you say the point is outside and break from the loop. Only one "outside" circle makes your code conclude that the point is not inside any circle. Thats wrong.

Instead you should start by assuming that the point is not inside any circle ( outside=true; ) and only when you find that the point is inside one of the circles you can break the loop (and set outside=false; ).

In other words: Currently your code checks if the points are outside any of the circles, but it appears you rather want to check if each of the points is inside any of the circles.

You can just switch this

 outside=false;
    //...other instructions...
    if(pow(dx,2) + pow(dy,2) > r) {
           outside = true;
           break;
        }

with

    outside=true;
//...other instruction...
outside=outiside&&sqrt(pow(dx, 2) + pow(dy, 2)) > r);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM