检查是否有一个点位于圆圈之外
Check if theres a point that lies outside of circles
所以我正在尝试制作一个控制台应用程序来检查它是否在 n
圆圈之外有任何 N(x,y)
点。
所以当有一个圆圈时程序运行完美。但是当我进入多个圈子时,它不能正常工作。
这是我的代码:
#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;
}
}
测试:
带一圈:
超过1圈:
删除循环的不太相关的部分基本上是这样的:
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";
您应该与 r*r
进行比较,因为距离是 sqrt(dx^2 + dy^2)
。然而,这不是主要问题。
你的标记 outside
逻辑颠倒了。您首先说该点在圆圈内,一旦找到一个不覆盖该点的圆圈,您就说该点在圆圈外并从循环中断开。只有一个“外部”圆圈使您的代码断定该点不在任何圆圈内。错了。
相反,您应该首先假设该点 不是 在任何圆圈 (outside=true;
) 内,并且只有当您发现该点在其中一个圆圈内时你可以打破循环(并设置 outside=false;
)。
换句话说:目前您的代码检查点是否在任何圆圈之外,但您似乎更想检查每个点是否在任何圆圈内。
你可以切换这个
outside=false;
//...other instructions...
if(pow(dx,2) + pow(dy,2) > r) {
outside = true;
break;
}
和
outside=true;
//...other instruction...
outside=outiside&&sqrt(pow(dx, 2) + pow(dy, 2)) > r);
所以我正在尝试制作一个控制台应用程序来检查它是否在 n
圆圈之外有任何 N(x,y)
点。
所以当有一个圆圈时程序运行完美。但是当我进入多个圈子时,它不能正常工作。
这是我的代码:
#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;
}
}
测试:
带一圈:
超过1圈:
删除循环的不太相关的部分基本上是这样的:
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";
您应该与 r*r
进行比较,因为距离是 sqrt(dx^2 + dy^2)
。然而,这不是主要问题。
你的标记 outside
逻辑颠倒了。您首先说该点在圆圈内,一旦找到一个不覆盖该点的圆圈,您就说该点在圆圈外并从循环中断开。只有一个“外部”圆圈使您的代码断定该点不在任何圆圈内。错了。
相反,您应该首先假设该点 不是 在任何圆圈 (outside=true;
) 内,并且只有当您发现该点在其中一个圆圈内时你可以打破循环(并设置 outside=false;
)。
换句话说:目前您的代码检查点是否在任何圆圈之外,但您似乎更想检查每个点是否在任何圆圈内。
你可以切换这个
outside=false;
//...other instructions...
if(pow(dx,2) + pow(dy,2) > r) {
outside = true;
break;
}
和
outside=true;
//...other instruction...
outside=outiside&&sqrt(pow(dx, 2) + pow(dy, 2)) > r);