I need a routine

  • Thread starter Thread starter Crirus
  • Start date Start date
C

Crirus

I need to write a routine that iterate through all points inside a circle
with a known radius

points are known as pairs (x,y)
 
Hi,

Imagine we have a square with its centre at the same point that is the
centre of the circle. Given that, we should analyze only points inside that
bounding square - no point outside the square can belong to the circle.

So we can write an outer and an inner loops iterating on points within the
square. Now, we have to determine whether a point belongs to the circle
itself. Well, this is simple math - as far as I remember, Sqr(dX * dX + dY *
dY) should be less or equal to the circle radius, R (you can actually
compare dX * dX + dY * dY <= R * R to avoid taking the square root), where
dX and dY is the X and Y components of the distance of the point being
analyzed from the centre of the circle.
 
Crirus said:
I need to write a routine that iterate through all points inside a
circle with a known radius

points are known as pairs (x,y)


The function returns all the points on the circle border. That's sufficient
to get all points within the circle.

Private Function GetCirclePoints(ByVal Radius As Single) As Point()
Const Deg As Double = Math.PI / 180D

Dim y As Integer
Dim Result As Point()
Dim Radius2 As Double = Radius * Radius

ReDim Result(CInt(Radius))

For y = 0 To CInt(Radius)
Result(y) = New Point(CInt(Math.Sqrt(radius2 - y * y)), y)
Next

Return Result

End Function


For example, to draw the circle border:

Dim points As Point()
Dim pt As Point

points = GetCirclePoints(50)

For Each pt In points
e.Graphics.DrawRectangle( _
Pens.Black, pt.X + 100, pt.Y + 100, 1, 1 _
)
Next


To calculate all points between (to fill the circle), use an additional loop
from x=0 to pt.x:

Dim points As Point()
Dim pt As Point
points = GetCirclePoints(50)
For Each pt In points
Dim x As Integer
For x = 0 To pt.X
e.Graphics.DrawRectangle( _
Pens.Black, x + 100, pt.Y + 100, 1, 1 _
)
Next
Next


You only get a pie but you can execute it 4 times and multiply coordinates
by all sign combinations (+/+), (+/-), (-/+), (-/-)
 
You'll need the centre of the circle.

Find the distance from the centre of the circle to the current point. If
that distance is less (or equal to) the radius it is contained in the
circle.

Forget the square... I assume that's there to try and save some processing
filtering the data probably uses about the same amount of resources as
simply checking the distance - especially if compare the square of the
distance to the square of the radius.

Steve




Dmitriy Lapshin said:
Hi,

Imagine we have a square with its centre at the same point that is the
centre of the circle. Given that, we should analyze only points inside that
bounding square - no point outside the square can belong to the circle.

So we can write an outer and an inner loops iterating on points within the
square. Now, we have to determine whether a point belongs to the circle
itself. Well, this is simple math - as far as I remember, Sqr(dX * dX + dY *
dY) should be less or equal to the circle radius, R (you can actually
compare dX * dX + dY * dY <= R * R to avoid taking the square root), where
dX and dY is the X and Y components of the distance of the point being
analyzed from the centre of the circle.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Crirus said:
I need to write a routine that iterate through all points inside a circle
with a known radius

points are known as pairs (x,y)
 
Steven,

I would agree if the original poster wouldn't have to *iterate* on *all* the
points within the circle - not just determine whether a single point belongs
to the circle.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Steven Garlinge said:
You'll need the centre of the circle.

Find the distance from the centre of the circle to the current point. If
that distance is less (or equal to) the radius it is contained in the
circle.

Forget the square... I assume that's there to try and save some processing
filtering the data probably uses about the same amount of resources as
simply checking the distance - especially if compare the square of the
distance to the square of the radius.

Steve




Dmitriy Lapshin said:
Hi,

Imagine we have a square with its centre at the same point that is the
centre of the circle. Given that, we should analyze only points inside that
bounding square - no point outside the square can belong to the circle.

So we can write an outer and an inner loops iterating on points within the
square. Now, we have to determine whether a point belongs to the circle
itself. Well, this is simple math - as far as I remember, Sqr(dX * dX +
dY
*
dY) should be less or equal to the circle radius, R (you can actually
compare dX * dX + dY * dY <= R * R to avoid taking the square root), where
dX and dY is the X and Y components of the distance of the point being
analyzed from the centre of the circle.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Crirus said:
I need to write a routine that iterate through all points inside a circle
with a known radius

points are known as pairs (x,y)
 
You're right Dimitry
I run through all points...anyway I solved the issue..

thanks

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Dmitriy Lapshin said:
Steven,

I would agree if the original poster wouldn't have to *iterate* on *all* the
points within the circle - not just determine whether a single point belongs
to the circle.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Steven Garlinge said:
You'll need the centre of the circle.

Find the distance from the centre of the circle to the current point. If
that distance is less (or equal to) the radius it is contained in the
circle.

Forget the square... I assume that's there to try and save some processing
filtering the data probably uses about the same amount of resources as
simply checking the distance - especially if compare the square of the
distance to the square of the radius.

Steve




in message news:%[email protected]...
+
dY
*
dY) should be less or equal to the circle radius, R (you can actually
compare dX * dX + dY * dY <= R * R to avoid taking the square root), where
dX and dY is the X and Y components of the distance of the point being
analyzed from the centre of the circle.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

I need to write a routine that iterate through all points inside a circle
with a known radius

points are known as pairs (x,y)
 
Back
Top