Mid Point Circle Algorithm and Implementation in Java - I@Tech

Wednesday, January 24, 2018

Mid Point Circle Algorithm and Implementation in Java



Algorithm:

Step:1    Initial values:- point(0,r) The Algorithm
x0 = 0
y0 = r
Step:2    Initial decision parameter 5/4 – r
          
Step:3   At each xi position, starting at i = 0, perform the
following test: if pi < 0, the next point is (xi + 1, yi) and                      dk+1 = dk+ 2xi+1 + 1
If pi ≥ 0, the next point is (xi+1, yi-1) and 
                    dk+1 = dk + 2xi+1 + 1 – 2yi+1
where 2xi+1 = 2xi + 2 and 2yi+1 = 2yi – 2

Step:4  Determine symmetry points in the other octants

Step:5  Move pixel positions (x,y) onto the circular path centered                on (xc, yc) and plot the coordinates:  x = x + xc,y = y + yc

Step:6  Repeat 3 – 5 until x ≥ y

Source Code:



package firstdesign; import java.io.*; import java.util.*; import java.math.*; import java.applet.*; import java.awt.*; public class midpointcircle extends Applet{ public void paint(Graphics g){ int r=150; int d=(5/4)*r; int x=0; int y=r; do{ g.setColor(Color.blue); g.drawLine(y+200,x+200,y+200,x+200 ); g.drawLine(x+200,y+200,x+200,y+200); g.drawLine(x+200,-y+200,x+200,-y+200); g.drawLine(y+200,-x+200,y+200,-x+200 ); g.drawLine(-y+200,-x+200,-y+200,-x+200 ); g.drawLine(-x+200,-y+200,-x+200,-y+200 ); g.drawLine(-x+200,y+200,-x+200,y+200 ); g.drawLine(-y+200,x+200,-y+200,x+200 ); if(d<0){ d=d+2*x+3; } else{ d=d+2*(x-y)+5; y=y-1; } x=x+1; } while (x<y); } }

No comments:

Post a Comment