我必须发明一种算法,当我们在控制台中输入两个坐标(x,y)时,结果将是具有该坐标的点位于图片的黑色部分(白色)还是边框。如果您能帮助我,那就太好了。
我从最小的圆圈开始,但是我不知道如何继续。
double x;
double y;
const int smallCircleRadius = 1;
const int mediumCircleRadius = 3;
const int bigCircleRadius = 6;
cin >> x >> y;
double d1 = sqrt(pow(abs(x - 0),2) + pow(abs(y - 3),2));
double d2 = sqrt(pow(abs(x - 0),2) + pow(abs(y + 3),2));
if(d1 < smallCircleRadius)
{
cout<<"Evil";
}
else if(d2 < smallCircleRadius)
{
cout<<"Good";
}
if(d1 == smallCircleRadius || d2 == smallCircleRadius)
{
cout<<"Neutral";
}
最佳答案
我想我已经在python中弄清楚了逻辑,因此您必须移植它,但是您会明白的:
smallCircleRadius = 1
mediumCircleRadius = 3
bigCircleRadius = 6
def yytest(x,y):
print(x,y,)
in_big_circle = ((x**2)+(y**2)) < bigCircleRadius**2
in_small_circle_upper_y = x**2+(y-mediumCircleRadius)**2 < smallCircleRadius**2
in_small_circle_lower_y = x**2+(y+mediumCircleRadius)**2 < smallCircleRadius**2
in_medium_circle_upper_y = x**2+(y-mediumCircleRadius)**2 < mediumCircleRadius**2
in_medium_circle_lower_y = x**2+(y+mediumCircleRadius)**2 < mediumCircleRadius**2
in_left_quadrant = x < 0
if in_big_circle:
if in_left_quadrant:
if (not in_medium_circle_lower_y or in_small_circle_lower_y) and not in_small_circle_upper_y:
return("good")
else:
return("evil")
else:
# right quadrant
if (not in_medium_circle_upper_y or in_small_circle_upper_y) and not in_small_circle_lower_y:
return("evil")
else:
return("good")
else:
return("neutral")
print(yytest(1,1)) # good
print(yytest(10,10)) # neutral
print(yytest(-0.5,3)) # evil
print(yytest(2,-2)) # evil
基本上,您必须计算关于圆中点或不圆中的点的标志,然后计算象限(左或右)并组合标志。绘图在这里很有帮助:)
关于c++ - 在阴阳上确定一点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40516222/