How would one check if a square is within a circle?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How would one check if a square is within a circle?

Post by ivan »

I haven't looked it up, but ok I'll trust your solution, pgimeno. :)
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: How would one check if a square is within a circle?

Post by darkfrei »

pgimeno wrote: Thu Feb 18, 2021 5:20 pm If distance(corner, center)^2 > radius^2 then Return true -- at least one corner is out of circle

By cancelling the square root in distance() with the squaring above, you save four square roots. The radius^2 can be calculated once before the loop. I believe that's the protip that eliddell suggested.
I know, the (squared_distance(corner, center) > radius^2) is much faster :)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests