Let’s see how that would work with an annulus with outer radius 2 and inner radius

1.

Let’s place this annulus on a coordinate system (sort of like a map).

And now imagine a grid laid over the top of that (like the grid lines on a map).

x

y

CSP1150/4150 Programming Principles page 3

Now (the centre points of) some of these grid cells are on the annulus, and some

are not. Let’s put a mark on those cells that are on the annulus.

In this case 94 cells are marked, out of 224 cells (16 across by 14 down). As a

fraction (94/224) this is about 0.42 i.e. about 42% of the cells are marked.

We can work out the area of the rectangle formed by all the grid cells (it’s about

24.9). So the area of the annulus is about 0.42 times 24.9, which is about 10.45.

We could have used any rectangle that covers the whole annulus for this

calculation, and we could have made the grid cells smaller (the smaller they are,

the more accurate the calculation should be).

Using the mathematical formula for the area, we should actually get about 9.43.

What went wrong? Well, my drawing is not very accurate! But we could do it better:

we could write a program to do the calculation for us. In pseudocode, here is what

the program should do:

BEGIN Area of Annulus

Set gridSize = 20 (we could use 20 by 20 grid cells)

Get r1 from user (outer radius)

Get r2 from user (inner radius)

Set counter = 0 (will count how many grid cells are on the annulus)

For each grid cell

if the centre of the cell is on the annulus

Set counter = counter + 1

Set area = (rectangle area) * counter/(gridSize * gridSize)

Output “Area : ” area

END Area of Annulus

Before we could turn this into a Java program, we have to fill in some of the details.

1. How big should the rectangle of grid cells be?

2. How do we figure out where the centre of each grid cell is?

3. Once we know that, how do we figure out whether it is on the annulus or

not?

Let’s tackle these one at a time:

1. How big should the rectangle of grid cells be?

Any rectangle that completely covers the annulus will do. Let’s say the rectangle

goes from minx to maxx in the x direction, and from miny to maxy in the y direction.

We might as well use this rectangle:

so minx = -r1, maxx = r1, miny = -r1, maxy = r1.

2. How do we figure out where the centre of each grid cell is?

This is tricky to work out so I’ll just give you the answer:

If you start counting the columns of grid cells from the left, starting at zero, then the

x-coordinate of a cell is min x + (column +0.5) × ((max x − min x) / gridSize) .

And is you start counting the rows of grid cells from the bottom, starting at zero,

then the y-coordinate of a cell is min y + (row +0.5) × ((max y − min y) / gridSize) .

x

y

and finally

3. Once we know that, how do we figure out whether it is on the annulus or

not?

Again, tricky. To be on the annulus, a point has to be inside a circle of the outer

radius, but not inside a circle of the inner radius. Using a formula worked out by

Pythagoras, this means that if the point has x-coordinate x and y-coordinate y, then

for it to be on the annulus:

x × x + y × y < r1× r1 (the point is inside the outer circle), and

x × x + y × y > r2 × r2 (the point is outside the inner circle).

Putting all this together in pseudocode:

BEGIN Area of Annulus

Set gridSize = 20 (we could use 20 by 20 grid cells)

Get r1 from user (outer radius)

Get r2 from user (inner radius)

Set minx = -r1

Set maxx = r1

Set miny = -r1

Set maxy = r1

Set counter = 0 (will count how many grid cells are on the annulus)

For column = 0 to gridSize-1

Set x = -minx + (column + 0.5)*((maxx–minx)/gridSize)

For row = 0 to gridSize-1

Set y = -miny + (row + 0.5)*((maxy–miny)/gridSize)

Set test = x*x + y*y

if test < r1*r1 and test > r2*r2

Set counter = counter + 1

Set area = (maxx-minx)*(maxy-miny)*counter/(gridSize * gridSize)

Output “Area : ” area

END Area of Annulus

Notice that when this is written in Java, it will use nested for loops.