Haki Terror
Haki Terror

Reputation: 363

Check if point belongs to square

I want to check if a point P(x1,y1) belongs , is inside , a square with center C(x,y) and horizontal diagonal r.

enter image description here

Upvotes: 1

Views: 786

Answers (1)

user555045
user555045

Reputation: 64913

You don't need the Euclidian distance between points here.

Just as for a circle (at the origin) you know that x2+y2 is some constant (r2), here you know that |x|+|y| is some constant (r again), which is even simpler. Actually you can interpolate between these shapes by using exponents between 1 and 2.

So to check whether a point (x,y) is inside the diamond (which without loss of generality can be assumed to be centered on the origin), just test

fabsf(x)+fabsf(y) <= r

Upvotes: 3

Related Questions