Bresenham's Circle Algorithm and Implementation in Java - I@Tech

Wednesday, January 24, 2018

Bresenham's Circle Algorithm and Implementation in Java



Algorithm:

Bresenham Circle ( Xc, Yc, R):
Description: Here Xc and Yc denote the x – coordinate and y –coordinate of the center of the circle. R is the radius.

Step:1    Set X = 0 and Y = R
Step:2    Set D = 3 – 2R
Step:3    Repeat While (X < Y)
Step:4    Call Draw Circle(Xc, Yc, X, Y)
Step:5    Set X = X + 1
Step:6    If (D < 0) Then
Step:7    D = D + 4X + 6
              Else
              Set Y = Y – 1
             D = D + 4(X – Y) + 10
             [End of If]
             Call Draw Circle(Xc, Yc, X, Y)
              X++
             [End of While]
Step:8 Exit

Source Code:


package firstdesign; import java.applet.*; import java.awt.*; public class Bcirclealgorithm extends Applet{ public void paint(Graphics g){ int Ox = (int)(getSize().getWidth() / 2); int Oy = (int)(getSize().getHeight() / 2); int R = (Ox > Oy) ? Oy-10 : Ox-10; g.setColor(Color.black); g.drawArc(Ox-R, Oy-R, 2*R, 2*R, 0, 360); g.setColor(Color.red); method1(g, Ox, Oy, R+2); g.setColor(Color.green); method1(g, Ox, Oy, R+4); g.setColor(Color.blue); method1(g, Ox, Oy, R+6); g.setColor(Color.cyan); method1(g, Ox, Oy, R+8); } void drawPoint(Graphics g, int x, int y){ g.drawLine(x,y,x,y); } void method1(Graphics g, int Ox, int Oy, int R){ int x = R; int y = 0; int err = 0; int dedx = (R << 1) - 1; int dedy = 1; drawPoint(g, Ox + R, Oy); drawPoint(g, Ox, Oy + R); drawPoint(g, Ox - R, Oy); drawPoint(g, Ox, Oy - R); while(x > y){ y++; err += dedy; dedy += 2; if (err >= dedx){ x--; err -= dedx; dedx -= 2; } drawPoint(g, Ox + x, Oy + y); drawPoint(g, Ox + x, Oy - y); drawPoint(g, Ox - x, Oy + y); drawPoint(g, Ox - x, Oy - y); drawPoint(g, Ox + y, Oy + x); drawPoint(g, Ox + y, Oy - x); drawPoint(g, Ox - y, Oy + x); drawPoint(g, Ox - y, Oy - x); } } }

No comments:

Post a Comment