Qbasicnews.com
May 26, 2020, 04:50:21 PM
 Pages: [1]
 Author Topic: help with circles  (Read 1468 times)
Anonymous
Guest
 « on: January 09, 2004, 08:57:39 PM »

umm say i had a circle on the screen... is there a formula thatll tell me if any given point is within that circle? thx
 Logged
SCM
Wandering Guru

Posts: 311

 « Reply #1 on: January 09, 2004, 10:40:47 PM »

This is very similar to the question about a point being inside an elipse a couple weeks ago.

If the circle is centered at (Xc, Yc) and has a radius of r, then a point (x, y) is inside or on the circle if
(x - Xc)^2 + (y - Yc)^2 <= r^2

Tips:
Only calculate r^2 once before your loops and store it in a variable such as r2

Do a speed test for (x - Xc)^2 and (x - Xc) * (x - Xc)
I think the second way is faster.
 Logged

hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
RePLaX
Member

Posts: 65

 « Reply #2 on: January 10, 2004, 06:58:17 PM »

basically we are checking to see if a point is closer to the center of the circle than the radius.  The easiest way to find the distance between 2 points is to use the pythagorean theorem(a^2 + b^2 = c^2).  we have 2 points, on is the center of the circle, the other is the point we're testing for. a = the difference in x:  b = the difference in y: and c is the overall distance

<psuedo code>
center = x1,y1
point = x2,y2
xdist = x2-x1
ydist = y2-y1
dist = sqrt(xdist^2 + ydist^2)
if dist <= radius we are inside the circle
</psuedo code>

now what he was saying is to speed it up, eliminate the sqrt
so instead, before you calculate anything you save a var with the value of the radius squared and compare that to (xdist^2 + ydist^2)  this will speed up the execution quite a bit
 Logged
Anonymous
Guest
 « Reply #3 on: January 10, 2004, 09:26:05 PM »

thanx a lot guys
 Logged
Zack
*/-\*

Posts: 3974

 « Reply #4 on: January 10, 2004, 11:47:50 PM »

And (I posted this somewhere else earlier, I think) to find if a point is *on* the border of the circle, with point (x,y) and radius r:
Code:
IF x^2 + y^2=r^2 THEN
'It's on the border
END IF
 Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
 Pages: [1]