電腦圖形學CG 中點畫圓法 畫圓

來源:互聯網
上載者:User
畫圓

姓 名:    

學 號:        

班 級:      

日 期:    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
staticvoid
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);

      }

實驗結果:()

  

程式原始碼:

package cs0904.no200942094;

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

public
class
DrawCircle 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);

   }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.