计算机图形学-圆弧生成算法 Bresenham 椭圆生成

本文介绍三种不同的圆弧绘制算法:Bresenham算法、Pnarc算法和Dissert算法,并通过Java Swing实现这些算法来绘制不同参数的圆弧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import javax.swing.JFrame;

public class MyDrawArc {
 public static void main(String args[]) {
  // pnarc(40);
  // bresenham_arc(40);
  dissert_arc(30, 40);
 }

 private static void dissert_arc(int a, int b) {
  JFrame frame = new JFrame();
  TwoDimen env = new TwoDimen();
  frame.getContentPane().add(env);
  frame.setBounds(100, 100, 600, 600);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  frame.setVisible(true);
  int x, y;
  double d;
  x = 0;
  y = b;
  d = b * b + a * a * (- b + 0.25);
  while (b * b * (x + 1) < a * a * (y - 0.5)) {
   env.drawPoint(x, y);
   env.drawPoint(x, -y);
   env.drawPoint(-x, y);
   env.drawPoint(-x, -y);
   if (d < 0) {
    d = d + b * b * (2 * x + 3);
   } else {
    d = d + b * b * (2 * x + 3) + a * a * (-2 * y + 2);
    y--;
   }
   x++;
  }
  d = (b * x + 0.5 * b) * (b * x + 0.5 * b) + (y * a - a) * (y * a - a)
    - a * a * b * b;
  while (y > 0) {
   env.drawPoint(x, y);
   env.drawPoint(x, -y);
   env.drawPoint(-x, y);
   env.drawPoint(-x, -y);
   if (d < 0) {
    d = d + b * b * (2 * x + 2) + a * a * (-2 * y + 3);
    x++;
   } else {
    d = d + a * a * (-2 * y + 3);
   }
   y--;
  }
 }

 private static void bresenham_arc(int radius) {
  JFrame frame = new JFrame();
  TwoDimen env = new TwoDimen();
  frame.getContentPane().add(env);
  frame.setBounds(100, 100, 600, 600);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  frame.setVisible(true);
  int x, y, d;
  x = 0;
  y = radius;
  d = 3 - 2 * radius;
  while (x < y) {
   env.drawPoint(x, y);
   env.drawPoint(-x, y);
   env.drawPoint(x, -y);
   env.drawPoint(-x, -y);
   env.drawPoint(y, x);
   env.drawPoint(-y, x);
   env.drawPoint(y, -x);
   env.drawPoint(-y, -x);
   if (d < 0) {
    d = d + 4 * x + 6;
   } else {
    d = d + 4 * (x - y) + 10;
    y = y - 1;
   }
   x = x + 1;
  }
  if (x == y)
   env.drawPoint(x, y);
 }

 private static void pnarc(int radius) {
  JFrame frame = new JFrame();
  TwoDimen env = new TwoDimen();
  frame.getContentPane().add(env);
  frame.setBounds(100, 100, 600, 600);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  frame.setVisible(true);
  int x, y, f;
  x = 0;
  y = radius;
  f = 0;
  while (y > 0) {
   env.drawPoint(x, y);
   env.drawPoint(x, -y);
   env.drawPoint(-x, y);
   env.drawPoint(-x, -y);
   if (f > 0) {
    f = f - 2 * y + 1;
    y = y - 1;
   } else {
    f = f + 2 * x + 1;
    x = x + 1;
   }
  }
  if (y == 0)
   env.drawPoint(x, y);
 }
}

class TwoDimen extends JPanel {
 private boolean[][] data;
 public TwoDimen() {
  data = new boolean[100][100];
  initialData(data);
  this.setBounds(100, 100, 500, 500);
  this.setVisible(true);
 }

 public void initialData(boolean[][] data) {
  for (int i = 0; i < data.length; i++) {
   for (int j = 0; j < data[i].length; j++) {
    data[i][j] = false;
   }
  }
  repaint();
 }
 public void drawPoint(int i,int j){
  data[i+49][50-j]=true;
  repaint();
 }
 @Override
 public void paint(Graphics g) {
  // TODO Auto-generated method stub
  super.paint(g);
  g.drawLine(0, 250, 500, 250);
  g.drawLine(250, 0, 250, 500);
  for (int i = 0; i < data.length; i++) {
   for (int j = 0; j < data[i].length; j++) {
    if (data[i][j]) {
     g.fillRect(5 * i, 5 * j, 5, 5);
    }
   }
  }
  // g.fillRect(100, 100, 5, 5);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值