B
B
BonBon Slick2019-12-22 21:58:04
Mathematics
BonBon Slick, 2019-12-22 21:58:04

How to determine whether a point belongs to a circle?

There is a circle, the position of the center is conditionally x \u003d 150, y \u003d 100 and radius \u003d 50. And also a point, let it be in coordinates x \u003d 100, y \u003d 100.
How to determine whether a point is inside the circle?

bool isInside = (circleInner->getPositionX() >= touch->getLocationInView().x - 55 &&
    circleInner->getPositionX() <= touch->getLocationInView().x + 55) &&
    (circleInner->getPositionY() <= touch->getLocationInView().x + 55 &&
    circleInner->getPositionY() >= touch->getLocationInView().x - 55)

We check this approach in the X range and at the same time in the Y range from the center of the circle +- radius.
There is another option
bool isInside = (circleInner->getPositionX() - touch->getLocationInView().x <= 55) &&
    (circleInner->getPositionY() - touch->getLocationInView().y <= 55)

We check the distance between the current point and the center of the circle, if the distance is less than the radius, then the point is inside the circle.
I would like to know the formula if there is. How correct are the approaches above? Perhaps you know better?
Here's where I got it from
https://discuss.cocos2d-x.org/t/how-do-i-get-the-p...
https://stackoverflow.com/questions/42793556/how-d...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2019-12-22
@BonBonSlick

Circle formula: (x-x0)^2+(y-y0)^2<=r^2
Substitute a dot in the formula, you get a bool.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question