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);
}
}