计算机图形学CG 中点画圆法 画圆

来源:互联网 发布:电脑桌面美化软件大全 编辑:程序博客网 时间:2024/04/27 15:17

画圆

姓 名:    

学 号:        

班 级:      

日 期:    2012-4    

实验:     画圆

实验内容:

       我使用的是中点画圆法(改进(2)算法)

       核心算法:

//8个对称的点

   public static void drawEightPoint(int x,int y,Graphics g)

   {

      g.fillOval(offsetX+x,offsetY+y,pointSize,pointSize);

      g.fillOval(offsetX+x,offsetY-2*r+(offsetY-y),pointSize,pointSize);

      g.fillOval(offsetX-3*r+(offsetX-x),offsetY+y,pointSize,pointSize);

      g.fillOval(offsetX-3*r+(offsetX-x),offsetY-2*r+(offsetY-y),pointSize,pointSize);

     

      g.fillOval(offsetX+y,offsetY+x,pointSize,pointSize);

      g.fillOval(offsetX+y,offsetY-2*r+(offsetY-x),pointSize,pointSize);

      g.fillOval(offsetX-3*r+(offsetX-y),offsetY+x,pointSize,pointSize);

      g.fillOval(offsetX-3*r+(offsetX-y),offsetY-2*r+(offsetY-x),pointSize,pointSize);

   }

//中点画圆核心算法 

public staticvoiddrawCircle(Graphics g) {

      int x, y, d;

      x= 0;

      y= r;

      int deltax = 3;

      int deltay = 2 -r -r;

      d= 1 - r;

      drawEightPoint(x,y,g);

      while (x <= y) {

         if (d < 0) {

            d+= deltax;

            deltax+= 2;

         }else{

            d+= deltax + deltay;

            deltax+= 2;

            deltay+= 2;

            y--;

         }

         x++;

         drawEightPoint(x,y,g);

      }

实验结果:(截图)

  

程序源代码:

package cs0904.no200942094;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

public classDrawCircle extendsJPanel {

   private static int frameWidth = 600;

   private static int frameHeight = 600;

   private static int offsetX = 300,offsetY = 200;

   // 起点和重点的坐标

   // 半径

   private static int r = 100;

   // 定义点的大小

   private static int pointSize = 5;

   // 20格算一个小格

   private static int turnBigger(int temp) {

      return temp * 20;

   }

   public static void main(String[] args) {

      JFrameframe = newJFrame("DrawLine");

      frame.getContentPane().add(new DrawCircle());

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.setSize(frameWidth,frameHeight);

      frame.setLocation(200,10);

      frame.setVisible(true);

   }

   //8个对称的点

   public static void drawEightPoint(int x,int y,Graphics g)

   {

      g.fillOval(offsetX+x,offsetY+y,pointSize,pointSize);

      g.fillOval(offsetX+x,offsetY-2*r+(offsetY-y),pointSize,pointSize);

      g.fillOval(offsetX-3*r+(offsetX-x),offsetY+y,pointSize,pointSize);

      g.fillOval(offsetX-3*r+(offsetX-x),offsetY-2*r+(offsetY-y),pointSize,pointSize);

     

      g.fillOval(offsetX+y,offsetY+x,pointSize,pointSize);

      g.fillOval(offsetX+y,offsetY-2*r+(offsetY-x),pointSize,pointSize);

      g.fillOval(offsetX-3*r+(offsetX-y),offsetY+x,pointSize,pointSize);

      g.fillOval(offsetX-3*r+(offsetX-y),offsetY-2*r+(offsetY-x),pointSize,pointSize);

   }

   public static void drawCircle(Graphics g){

      int x, y, d;

      x= 0;

      y= r;

      int deltax = 3;

      int deltay = 2 -r -r;

      d= 1 - r;

      drawEightPoint(x,y,g);

      while (x <= y) {

         if (d < 0) {

            d+= deltax;

            deltax+= 2;

         }else{

            d+= deltax + deltay;

            deltax+= 2;

            deltay+= 2;

            y--;

         }

         x++;

         drawEightPoint(x,y,g);

      }

      g.drawString("", x+20, y+20);

   }

 

   protected void paintComponent(Graphicsg) {

      super.paintComponent(g);

      for (int i = 0; i <frameHeight; i +=turnBigger(1)){

         g.drawLine(0,i, frameWidth,i);

      }

      for (int i = 0; i <frameWidth; i +=turnBigger(1)){

         g.drawLine(i,0, i, frameHeight);

      }

      drawCircle(g);

   }

}