A
A
Alexander Ivanov2022-02-22 20:02:57
C++ / C#
Alexander Ivanov, 2022-02-22 20:02:57

How to distinguish between a click on a circle and inside it?

Task: draw a circle with a void inside, hang a click on it and in the inside of the function distinguish whether we click on the void inside the circle or on it.

I stopped on a click, but I can’t figure out how to distinguish the area?
Click controller code: https://github.com/AlexandrRumiantsev/bagel_ios/bl...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stanislav Makarov, 2016-03-26
@Nipheris

For real numbers, there is no == comparison. There are comparisons (a - b) <= eps. In most cases, this is necessary, because. real numbers model continual quantities from the real world (i.e. those in which it is not necessary to count with an accuracy of each digit).
On the other hand, real arithmetic cannot be used for monetary calculations, because money is basically discrete. By definition, nobody wants to see 4.74999999...$ instead of 4.75$. For such calculations, decimal types are used (such types are not in standard C ++, there are implemented in the form of libraries).
Strictly speaking, the task is set incorrectly. Or, well, it's pointless. Such a task could be set for decimal types, which are always considered exactly, and the number of decimal digits in their record is PREDICTABLE. And for a float, you will get something like 4749999999998.
In addition to the accumulation of errors, you still do not forget that not all decimal constants, even those you specified in the code, can be represented in binary without loss of precision. For example, get acquainted with the number 0.13. You can just write it to a constant and immediately display it on the screen.
In general, welcome to the world of machine arithmetic.
https://en.wikipedia.org/wiki/Floating_point
https://en.wikipedia.org/wiki/Decimal_data_type

R
Rsa97, 2016-03-26
@Rsa97

Because when working with real numbers, one should always take into account the limitations of their representation in a computer and the accumulation of errors during operations with them. Step through your function with the debugger and you will see that with each multiplication, a small tail remains in the fractional part, which means that the condition dotNum > int(dotNum) will always be true.

L
lega, 2016-03-26
@lega

in the second case the loop becomes infinite?
Because the result is "infinity" in the fractional part. (this is how double/float works)
>>> x = 100.475
>>> x - 100
0.4749999999999943

V
Viktor, 2022-02-22
@alexsteadfast

I'm not a programmer, but there are mathematical nuances in the question that are interesting to me:
1. It is theoretically impossible to click "on a circle", since a mathematical line (including a circle), defined as a locus of points, has zero thickness. Therefore, you will have to click either inside the circle, or outside it - but not on it.
2. Algorithm for determining "inside / outside" with the so-called. Mathematics is elementary simple: by clicking, you need to start calculating the length of the segment from the current cursor coordinates to the center of the circle (its coordinates are known), and then compare it with the known radius of the circle. If the segment is longer than the radius, we are outside the circle, if shorter - inside.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question