Tell if a point is in an area of a square

87 Views Asked by At

It's probably a basic question, but I would like to determine whether a point $(x, y)$ belongs to a square area defined by two points on a circle, that were created under some degree. Here I will draw an example:

sample image

Known data is as follows:

$\alpha$ - degree in which the line is drawn

$(x, y)$ - left top corner position of a square

$a$ - length of a square

I'm calculating the other data as follows (I can be wrong here):

$$r = a * {\sqrt 2} / 2$$

$$\text{circle Center Position X} = x + a/2$$

$$\text{circle Center Position Y}= y + a/2$$

$$\alpha = \alpha - 90$$

These two points on a circle calculated like this:

$p1x = \text{circle Center Position X} + radius * \cos(\alpha)$

$p1y = \text{circle Center Position Y} + radius * \sin(\alpha)$,

$p2x = \text{circle Center Position X} - radius * \cos(\alpha)$

$p2y = \text{circle Center Position Y}- radius * \sin(\alpha)$

Important to note is that Y axis values grow as we go down. X axis values grow as we go to the right as usual.

1

There are 1 best solutions below

2
On BEST ANSWER

First make sure the point is in the square (e.g. by checking directly with $x$ and $y$ coordinates). Next convert your $(x,y)$ value to polar coordinates (with angle between $0$ and $360$) and check its angle. If it lies between $\theta$ and $−(180−\theta)$ it will lie on one half of the line, otherwise it will lie on the other half. To get the angle, you can use the $\operatorname{atan2}$ function implemented in most programming languages.