Java Program - Mid-Point Circle Drawing Algorithm

import java.awt.*;
import javax.swing.JFrame;
import java.util.Scanner;
import java.lang.*;

public class MP_CIRCLE extends Canvas
{
  static int xc,yc,r;
  
  MP_CIRCLE(int xc,int yc,int r)
  {
   this.xc=xc;
   this.yc=yc;
   this.r=r;
  }
  
  public void paint(Graphics g)
  {
   int x,y,p;
   x=0;
   y=r;
   fill(g,x,y,xc,yc);
   p=1-r;
   while(x<y)
   {
    x=x+1;
    if(p<0)
    {
     p=p+2*x+1;
    }
    else
    {
     y=y-1;
     p=p+2*x+1-2*y;
    }
    fill(g,x,y,xc,yc);
   }
  }
  
  public void fill(Graphics g,int x,int y,int xc,int yc)
  {
   g.fillOval(xc+x,yc+y,5,5);
   g.fillOval(xc+x,yc-y,5,5);
   g.fillOval(xc-x,yc+y,5,5);
   g.fillOval(xc-x,yc-y,5,5);
   g.fillOval(xc+y,yc+x,5,5);
   g.fillOval(xc+y,yc-x,5,5);
   g.fillOval(xc-y,yc+x,5,5);
   g.fillOval(xc-y,yc-x,5,5);
  }

  public static void main(String args[])
  {
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter center (xc,yc): ");
   int xc=sc.nextInt();
   int yc=sc.nextInt();
   System.out.println("Enter radius r: ");
   int r=sc.nextInt();
   MP_CIRCLE c = new MP_CIRCLE(xc,yc,r);
   JFrame f=new JFrame();
   f.add(c);
   f.setSize(800,800);
   f.setVisible(true);
  }
}



1 comment:

  1. Your Affiliate Money Printing Machine is ready -

    And getting it set up is as simple as 1-2-3!

    Here is how it all works...

    STEP 1. Tell the system which affiliate products you want to push
    STEP 2. Add some PUSH BUTTON traffic (this ONLY takes 2 minutes)
    STEP 3. Watch the affiliate products system grow your list and sell your affiliate products all for you!

    Do you want to start making money??

    Get the full details here

    ReplyDelete