# How to write MidPoint Circle Algorithm in Java Applet

MidPoint Circle Algorithm was one of the old and basic methods to print a Circle. For details, you may read Wikipedia. But, anyway I will try to explain what I know and try to make this as concise as possible. So let’s start learning how to write MidPoint Circle Drawing Algorithm in Java Applet

In Midpoint Circle Algorithm, for a given radius ‘r’ and screen centre position (xc , yc) , we calculate around a circle path centred at origin (0,0). Then each position calculated is moved to its proper position by adding x to xc and y to yc.

## Equation for Midpoint Circle Algorithm

To apply midpoint circle Algorithm, we define a circle equation:

fcir(x,y) = x2 + y2 – r2

The point lies on the boundary of the circle satisfies the above equation.

fcircle(x,y) =

```< 0           if(x,y) lies inside the circle boundary

>0            if(x,y) lies ourside the circle boundary

=0             if(x,y) lies on the circle boundary
```

Now, find a decision parameter for the circle by incrementing the ‘x’ by unit and finding ‘yk’ and ‘yk-1’.

The above shows the midpoint between the two selecting pixels at the same position  Xk + 1. We will now plot the pixel which is closer to the circle from the two points:

• Xk + 1, yk
• Xk + 1, yk – 1

The decision parameter is calculated as:-

Pk = fcircle( xk + 1, yk – ½)

= (xk + 1)2 + (yk – ½)2 – r2

Also , successive decision parameters are obtained using incremental calculations.

Now , incrementing by unit in previous ‘x’ variable,

xk + 1 + 1= xk + 2

Decision parameter for xk + 2

Pk+1 = fcircle( xk+1 + 1, yk+1 – ½)

= [(xk + 1) +1]2 + (yk+1 – ½)2 – r2

Our algorithm then plots first point at (r, 0) in the circle equation (x, y)= (0,r)

P = fcircle( 1, r-)

= 1 + (r-1/2)2  – r2

P0 = 5/4 -r

```while (x <= y) {
raster.setPixel((int) (x + r), (int) (y + r), array);
System.out.println(x + " , " + y + " : p0 = " + p0);
raster.setPixel((int) (y + r), (int) (x + r), array);
raster.setPixel((int) (-x + r), (int) (y + r), array);
raster.setPixel((int) (-y + r), (int) (x + r), array);
raster.setPixel((int) (-x + r), (int) (-y + r), array);
raster.setPixel((int) (-y + r), (int) (-x + r), array);
raster.setPixel((int) (x + r), (int) (-y + r), array);
raster.setPixel((int) (y + r), (int) (-x + r), array);
if (p0 <= 0) {
x++;
p0 += 1 + (2 * x);
} else {
x++;
y--;
p0 += 1 + (2 * x) - (2 * y);
}
}
```

In the code above, I ran the algorithm until the initial point x does not move from (0,y) to (x, 0) and thereby printing our one quadrant and then, but since the circle has symmetry thus, we print all other quadrants by replicating our points for the other quadrants.

The full code is as follows:

```package computergraphics;

import java.io.*;

import java.awt.*;

import java.applet.*;

import java.util.*;

import java.awt.image.*;

public class MidPointCircleDrawing extends Applet {

BufferedImage image = new BufferedImage(1900, 1000, BufferedImage.TYPE_INT_ARGB);

WritableRaster raster = image.getRaster();

int array[] = {255, 0, 0, 255};

public void init() {

setSize(400, 250);

repaint();

}

```

Moreover, I extended the class to the Applet class of Java in order to use Writable Raster for printing the points. We have to use the buffered image object in order to provide a canvas for plotting the points.

### Java program to implement MidPoint Circle Algorithm:

```    public void paint(Graphics g) {

double r = 100, x, y;

double p0 = (1) - r;

x = 0;

y = r;

while (x <= y) {

raster.setPixel((int) (x + r), (int) (y + r), array);

System.out.println(x + " , " + y + " : p0 = " + p0);

raster.setPixel((int) (y + r), (int) (x + r), array);

raster.setPixel((int) (-x + r), (int) (y + r), array);

raster.setPixel((int) (-y + r), (int) (x + r), array);

raster.setPixel((int) (-x + r), (int) (-y + r), array);

raster.setPixel((int) (-y + r), (int) (-x + r), array);

raster.setPixel((int) (x + r), (int) (-y + r), array);

raster.setPixel((int) (y + r), (int) (-x + r), array);

if (p0 <= 0) {

x++;

p0 += 1 + (2 * x);

} else {

x++;

y--;

p0 += 1 + (2 * x) - (2 * y);

}

}

g.drawImage(image, 0, 0, null);

}

}```

Output:

I pressed Shift+F6 and output appeared in Applet window as a circle thus our algorithm worked perfectly.

Note: To run the code easily, change the package name with yours and press Shift+F6 to execute it on NetBeans.

For any queries feel free to comment.