简体   繁体   English

检查是否有一个点位于圆圈之外

[英]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.所以我正在尝试制作一个控制台应用程序来检查它是否在n圆圈之外有任何N(x,y)点。

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 个圆圈:

超过 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) .您应该与r*r进行比较,因为距离是sqrt(dx^2 + dy^2) However, thats not the main problem.然而,这不是主要问题。

Your flag outside has the logic flipped.outside的旗帜已经翻转了逻辑。 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; ).相反,您应该首先假设该点不在任何圆圈内( outside=true; ),并且只有当您发现该点在其中一个圆圈内时,您才能打破循环(并设置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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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