[英]How to calculate if a point is within a circle
I am given a constructor:我得到了一个构造函数:
Circle::Circle(const Point& c, float r) {
x_ = c.getX();
y_ = c.getY();
r_ = r;
}
All values have been initialised as shown.如图所示,所有值都已初始化。 In the parameter I have
Point&
- This just allows me to get the x and y coords using a function from a different class.在参数中我有
Point&
- 这仅允许我使用来自不同 class 的 function 获取 x 和 y 坐标。 Also, I have "r" which will take in the radius value.另外,我有“r”,它将采用半径值。
Using this I want to check whether that: If o is a circle, it returns true if and only if p is on the inside or the boundary of the circle使用它我想检查是否:如果 o 是一个圆,当且仅当 p 在圆的内部或边界上时,它才返回 true
For example,例如,
I am given: Point p(1,2);
给我:
Point p(1,2);
& Circle c(p,3);
&
Circle c(p,3);
I want to check if Point(3.9,2)
is on the inside or on the boundary of Circle c(p,3);
我想检查
Point(3.9,2)
是在Circle c(p,3);
For extra reference, I will provide the Point Constructor
:作为额外参考,我将提供
Point Constructor
:
Point::Point(float x, float y) {
x_ = x;
y_ = y;
}
Initialised constructor to allow me to create getter and setters for use in other classes.初始化构造函数以允许我创建用于其他类的 getter 和 setter。
In my function I have tried this:在我的 function 我试过这个:
bool Circle::contains(const Point& p) const {
bool results;
if( ( (getX()-p.getX() ) * ( getX()-p.getX() ) ) + ( ( (getY()-p.getY() ) * ( getY()-p.getY() ) ) <= (getR()*getR())))
{
results = true;
}
else {results = false;}
return results;
}
This did not work.这没有用。
Test Case I am given:给我的测试用例:
Point p(1,2);
Circle c(p,3);
if (!c.contains(p)) errorOut_("c does not contain p?",1);
if (!c.contains(Point(3.9,2))) errorOut_("c does not contain (3.9,2)?",1);
if (!c.contains(Point(3.1,4.1))) errorOut_("c does not contain (3.1,4.1)?",1);
if (!c.contains(Point(-1.1,4.1))) errorOut_("c does not contain (-1.1,4.1)?",1);
if (!c.contains(Point(-1.1,-0.1))) errorOut_("c does not contain (-1.1,-0.1)?",1);
if (!c.contains(Point(3.1,-0.1))) errorOut_("c does not contain (3.1,-0.1)?",1);
if (c.contains(Point(3.2,4.2))) errorOut_("c contains (3.2,4.2)?",1);
if (c.contains(Point(-1.2,4.2))) errorOut_("c contains (-1.2,4.2)?",1);
if (c.contains(Point(-1.2,-0.2))) errorOut_("c contains (-1.2,-0.2)?",1);
if (c.contains(Point(3.2,-0.2))) errorOut_("c contains (3.2,-0.2)?",1);
The result:结果:
fail1: c contains (-1.2,4.2)?
fail1: c contains (-1.2,-0.2)?
fail1: c contains (3.2,-0.2)?
To check whether point lies inside circle, you need to implement this formula (strict <
if you don't need points at circumerence):要检查点是否在圆内,您需要实现此公式(如果您不需要圆周上的点,则严格
<
):
(px-cx)*(px-cx) + (py-cy)*(py-cy) <= r*r
Squared distance from point to center should be less than squared radius (to avoid sqrt calculation)点到中心的平方距离应小于平方半径(避免 sqrt 计算)
For your example对于你的例子
(3.9-1)^2+(2-2)^2 = 8.41 < 3*3=9 - inside
Corrected your function:更正了您的 function:
bool Circle::contains(const Point& p) const {
return ( ( (getX()-p.getX() ) * ( getX()-p.getX() ) ) +
( ( (getY()-p.getY() ) * ( getY()-p.getY() ) ) <= (getR()*getR())))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.