Manual CPCS 391
Lab 5
Introduction
What we'd like to do is to use this discriminating function
to maintain our trajectory of drawn pixels as close as
possible to the desired circle. Luckily, we can start with a point on the circle
) ، ٠ x ، ٠ y + ( r ) or ٠ )
، ( r in our adjusted
coordinate system). As we move along in steps of x we note that the slope is less than zero and greater than
negative one at points in the direction we're heading that are near our known point on a circle. Thus we need only to figure out at each step whether to step down in y or
maintain y at each step
.
Objective
Drawing in a Circle Drawing ( Mid-Point
Circle Algorithm )
Overview
We will compute points between x=0 and x=y and then draw the 8 matching points
In that area, the slope of the curve is between 0 and -1
From each step/point (x, y), the next one is either (x+1, y) or (x+1, y-1
(
We decide about which one by looking at the midpoint M
M inside (f<0) => next point is E
M ouside (f>0) => next point is SE
Prelab Activities
lectu
/ au .
edu .
anu .
escience ://
http
html .
en .
midPoint2 /
Circle /
cg /
re
Book “Computer Graphics CS
454,Part 1, ch3-9, Page 105.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
public class BresenhamCircle extends Applet implements AdjustmentListener { private static final long serialVersionUID = 1L;
int d = 5; int x0 = 0; int y0 = 0; int r = 0;
Label Hlabel = new Label();
Scrollbar Hrubka = new Scrollbar(Scrollbar.HORIZONTAL, 5, 1, 1, 100);
Choice R = new Choice();
Label Rlabel = new Label(" Color px: R:");
Choice G = new Choice();
Label Glabel = new Label(" G:");
Choice B = new Choice();
Label Blabel = new Label(" B:");
Color Farba = new Color(0,0,0);
Checkbox Vymaz = new Checkbox("Clean screen");
Graphics Buffer;
Dimension size1 = getSize();
private Image imgBuffer; //image doublebuffera
public void init() {
d = Hrubka.getValue();
add(Hlabel); add(Hrubka); add(Rlabel); add(Glabel);
add(R);
add(B); add(Vymaz);
add(G); add(Blabel);
Hlabel.setText("Width px: "+Hrubka.getValue());
Vymaz.setState(true);
G.add("G"); B.add("B");
R.add("R");
for (int i = 0; i < 256; i++){
} B.add(""+i); G.add(""+i); R.add(""+i);
Hrubka.addAdjustmentListener(this);
resize(600,400);
size1 = getSize();
imgBuffer = createImage(size1.width, size1.height);
Buffer = imgBuffer.getGraphics();
y0 = getHeight()/2;
x0 = getWidth()/2;
r = getHeight() / 3;
super.init();
}
public void start() {super.start();}
super.stop(); } public void stop() {
public void destroy() { super.destroy();
public void adjustmentValueChanged(AdjustmentEvent e){
if(e.getAdjustable().equals(Hrubka)){
d = Hrubka.getValue();
Hlabel.setText("Width px: "+Hrubka.getValue());
}
repaint(); }
public boolean action (Event evt, Object arg) { int r = 0, g = 0, b = 0;
if (R.getSelectedItem() != "R") {
r = Integer.parseInt(R.getSelectedItem());
//System.out.println(R.getSelectedItem());
}
if (G.getSelectedItem() != "G") {
} g = Integer.parseInt(G.getSelectedItem());
if (B.getSelectedItem() != "B") {
} b = Integer.parseInt(B.getSelectedItem());
repaint();
Farba = new Color(r,g,b);
} return true;
public boolean mouseDrag(Event arg0, int x, int y) {
r = (int)Math.round(Math.sqrt(Math.pow(x0 - x,2)+ Math.pow(y0 - y,2)));
//System.out.println("BresenhamCircle.mouseDrag() x:"+x+" y:"+y + " r:"+r );
repaint();
return super.mouseDrag(arg0, x, y);
}
public boolean mouseDown(Event arg0, int x, int y) { d = Hrubka.getValue();
x0 = x;
y0 = y;
//System.out.println("BresenhamCircle.mouseDown() x:"+x+" y:"+y);
repaint();
return super.mouseDown(arg0, x, y);
}
public void setPixel(int x, int y, int d, Graphics g) { g.setColor(Farba);
g.fillRect(x - (d/2), y - (d/2), d, d);
}
public void update(Graphics g) { paint(g);
}
public void paint( Graphics g ) { int rr = r - (r % d);
int y = rr;
if((size1.width != getWidth()) || (size1.height != getHeight())){
size1.width = getWidth();
size1.height = getHeight();
imgBuffer = createImage(getWidth(),getHeight());
Buffer = imgBuffer.getGraphics();
}
if(Vymaz.getState()){
Buffer.setColor(Color.WHITE);
Buffer.fillRect(0,0,getWidth(),getWidth());
}
for(int x = 0; x <= rr/Math.sqrt(2)+1; x+=d){
y = (int)Math.round(Math.sqrt(Math.pow(rr,2)-Math.pow(x,2)));
y = (y/d)*d;
setPixel(x0 + x, y0 + y, d, imgBuffer.getGraphics());
setPixel(x0 - x, y0 + y, d, imgBuffer.getGraphics());
setPixel(x0 + x, y0 - y, d, imgBuffer.getGraphics());
setPixel(x0 - x, y0 - y, d, imgBuffer.getGraphics());
setPixel(x0 + y, y0 + x, d, imgBuffer.getGraphics());
setPixel(x0 - y, y0 + x, d, imgBuffer.getGraphics());
setPixel(x0 + y, y0 - x, d, imgBuffer.getGraphics());
setPixel(x0 - y, y0 - x, d, imgBuffer.getGraphics());
}
g.drawImage(imgBuffer,0,0, this); //double buffering }
}