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

画圆

姓 名:

学 号:

班 级:

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

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值