Professional Documents
Culture Documents
Week08 - Drawing Circles
Week08 - Drawing Circles
Graphics
CPCS-391
Drawing Circles
DR. MOHAMED Y. DAHAB
MOHAMED.DAHAB@GMAIL.COM
1
Bresenham’s Line Algorithm
An accurate and efficient raster line-generating algorithm
we- consider only the scan-conversion process for lines with positive slope less than 1 (i.e. 𝑚 ≤ 1).
2
Bresenham’s Line Algorithm
(Cont’)
This equation is very important and it represents the core of bresnham’s algorithm
𝒑𝒌+𝟏 = 𝒑𝒌 + 𝟐 ∆𝒚
𝒑𝒌+𝟏 = 𝒑𝒌 + 𝟐 ∆𝒚 − 𝟐 ∆𝒙
3
private void BresenhamsDrawingLine(int X1, int Y1, int X2, int Y2, GL gl) {
int DX= X2-X1; int DY=Y2-Y1; int DY2=2*DY; int DX2=2*DY-2*DX; int p=2*DY-DX; float m = (float)DY /DX; int y=Y1; int x= X1;
gl.glBegin(GL.GL_POINTS);
gl.glVertex2i(X1, Y1);
gl.glEnd();
Bresenham’s
if (m<=1){
gl.glBegin(GL.GL_POINTS);
for(int xi=X1+1;xi<=X2;xi++){
if (p<0){
Line
gl.glVertex2i(xi, y);
AlgorithmUsing
JOGL
p=p+DY2;
}else{
gl.glVertex2i(xi, ++y);
p=p+DX2;
gl.glEnd();
}else{
4
Midpoint Line Algorithm
5
Midpoint Line Algorithm
Derivation
Line Properties
𝑦=𝑚𝑥+𝑏
∆𝑦
𝑦= 𝑥+𝑏
∆𝑥
∆𝑥 . 𝑦 = ∆𝑦 . 𝑥 + ∆𝑥 . 𝑏
∆𝑦 . 𝑥 − ∆𝑥 . 𝑦 + ∆𝑥 . 𝑏 = 0
𝐹 𝑥, 𝑦 = ∆𝑦 . 𝑥 − ∆𝑥 . 𝑦 + ∆𝑥 . 𝑏 = 0
6
Midpoint Line Algorithm
Derivation(Cont’)
Line Properties
𝐹 𝑥, 𝑦 = ∆𝑦 . 𝑥 − ∆𝑥 . 𝑦 + ∆𝑥 . 𝑏 = 0
7
Midpoint Line Algorithm
Derivation(Cont’)
𝐹 𝑥, 𝑦 = ∆𝑦 . 𝑥 − ∆𝑥 . 𝑦 + ∆𝑥 . 𝑏 = 0
Decision 𝑑 = 𝐹 Midpoint
8
Midpoint Line Algorithm
Derivation(Cont’)
If 𝑑 < 0 If 𝑑 ≥ 0
9
Midpoint Line Algorithm derivation
(Cont’)
𝐹 𝑥, 𝑦 = ∆𝑦 . 𝑥 − ∆𝑥 . 𝑦 + ∆𝑥 . 𝑏 = 0
Decision 𝑑 = 𝐹 Midpoint
𝑑 = 𝐹 Midpoint = 𝐹(𝑥𝑝+1 , 𝑦𝑝 + 0.5)
If 𝒅 ≥ 𝟎 then choose NE
𝑑𝑛𝑒𝑤 = 𝐹(𝑥𝑝 + 2 , 𝑦𝑝 + 1.5)
If 𝒅 < 𝟎
𝑑𝑛𝑒𝑤 = ∆𝑦 . (𝑥𝑝 + 2) − ∆𝑥 . (𝑦𝑝 + 0.5) + ∆𝑥 . 𝑏
𝑑𝑛𝑒𝑤 = 𝑑 + ∆𝑦
𝑑𝑛𝑒𝑤 = 𝑑 + ∆𝐸
11
Midpoint Line Algorithm
Derivation(Cont’)
Finding the initial value of decision 𝑑0
𝑑0 =∆𝑦 . 𝑥0 + ∆𝑦 − ∆𝑥 . 𝑦0 − 0.5∆𝑥 + ∆𝑥 . 𝑏
𝑑0 =𝐹 𝑥0 , 𝑦0 + ∆𝑦 − 0.5∆𝑥
𝑑0 =∆𝑦 − 0.5∆𝑥
12
Midpoint Line Algorithm
Derivation(Cont’)
Finding the initial value of decision 𝑑0
𝑑0 =∆𝑦 − 0.5∆𝑥
We are interested with the sign of 𝑑 not the value. So to avoid the fraction multiply by 2
𝑑0 = 2. ∆𝑦 − ∆𝑥
∆𝑁𝐸 = 2. (∆𝑦 − ∆𝑥)
∆𝐸 = 2∆𝑦
13
Midpoint Line Algorithm
function [x,y]=mpla(x0,y0,x1,y1)
x0=round(x0);x1=round(x1); y0=round(y0);
dx=x1-x0; dy=y1-y0; dne=2*dy-2*dx; de=2*dy; d=2*dy-dx;
y(1)=y0; x=x0:x1;
k=1;
for i=x0+1:x1
if d<0
y(k+1)=y(k);
d=d+de;
else
y(k+1)=y(k)+1;
d=d+dne;
end
k=k+1;
end 14
Midpoint Line Algorithm
Use Midpoint Line Algorithm to draw the following line: from (1, 1) to (10, 5)
x0 y0 x1 y1 ΔE ΔNE p
1 1 10 5 8 -10 -1
2 1 7
3 2 -3
4 2 5
5 3 -5
6 3 3
7 4 -7
8 4 1
9 5 -9
10 5
15
Tracing Bresenham’s Algorithm
Use Bresenham’s algorithm to draw the following line: from (1, 1) to (10, 5)
x0 y0 x1 y1 2Δy 2Δy-2Δx p
1 1 10 5 8 -10 -1
2 1 7
3 2 -3
4 2 5
5 3 -5
6 3 3
7 4 -7
8 4 1
9 5 -9
10 5
16
Quick Revision
Draw a line from (2, 1) to (12, 7) using midpoint line algorithm. Show all intermediate computations
and draw pixels on the figures
17
Quick Revision (Cont’)
Name the following algorithm:
increment 𝑥 by 1 from the starting point (𝑥0 , 𝑦0 ) to the ending point (𝑥1 ,𝑦1 ) calculate y at
each point, 𝑦1 = 𝑚 . 𝑥1 + 𝑏 turn on pixel (𝑥1 ,round(𝑦1 ))
18
Properties of Circles
A circle is defined as the set of points that are all at a given distance 𝑟 from
a center position (𝑥𝑐 ,𝑦𝑐 )
But this is not the best method for generating a circle. One problem with
this approach is that it involves considerable computation at each step
19
Properties of Circles
(Cont’)
Moreover, the spacing between plotted pixel positions is not uniform, as
demonstrated in the Figure
For a more continuous boundary on a raster display, we can set the step
1
size at
𝑟
20
Properties of Circles
(Cont’)
Computation can be reduced by considering the symmetry of
circles
We can take this one step further and note that there is also
symmetry between octants. Circle sections in adjacent octants
within one quadrant are symmetric with respect to the 45º line
dividing the two octants
21
Drawing Circle Algorithm
Determining pixel positions along a circle circumference still requires a good deal of computation
time.
The Cartesian equation involves multiplications and square root calculations, while the parametric
equations contain multiplications and trigonometric calculations
22
Midpoint Circle Algorithm
Implicit of equation of circle is : 𝑥 2 + 𝑦 2 − 𝑟 2 = 0
Taking advantage of the circle symmetry, in this way we can generate all pixel positions around a
circle by calculating only the points within the sector from 𝑥 = 0 to 𝑥 = 𝑦
Point at position (𝑥, 𝑦) on a one-eighth circle sector is mapped into the seven circle points in the
other octants of the 𝑥𝑦 plane
23
Midpoint Circle Algorithm (Cont’)
Define decision variable 𝑑 as:
2 2
𝑑 = 𝐹 𝑥𝑝 + 1 , 𝑦𝑝 − 0.5 = 𝑥𝑝 + 1 + 𝑦𝑝 − 0.5 − 𝑟2
𝑖𝑓 𝑑 < 0
𝑚 inside the circle, choose 𝐸
𝑦 remains unchanged
𝑖𝑓 𝑑 > 0
𝑚 outside the circle, choose 𝑆𝐸
Decrement 𝑦
𝑖𝑓 𝑑 = 0
choose 𝑆𝐸 or 𝐸
24
Midpoint Circle Algorithm (Cont’)
𝑖𝑓 𝑑 < 0
2 2
𝑑 = 𝐹 𝑥𝑝 + 1 , 𝑦𝑝 − 0.5 = 𝑥𝑝 + 1 + 𝑦𝑝 − 0.5 − 𝑟2
2 2
𝑑𝑛𝑒𝑤 = 𝐹 𝑥𝑝 + 2 , 𝑦𝑝 − 0.5 = 𝑥𝑝 + 2 + 𝑦𝑝 − 0.5 − 𝑟2
𝑑𝑛𝑒𝑤 − 𝑑 = 2𝑥𝑝 + 3
𝑑𝑛𝑒𝑤 − 𝑑 = ∆𝐸
𝑑𝑛𝑒𝑤 = 𝑑 + ∆𝐸
𝑑𝑛𝑒𝑤 = 𝑑 + ∆𝐸
25
Midpoint Circle Algorithm (Cont’)
𝑖𝑓 𝑑 > 0
2 2
𝑑 = 𝐹 𝑥𝑝 + 1 , 𝑦𝑝 − 0.5 = 𝑥𝑝 + 1 + 𝑦𝑝 − 0.5 − 𝑟2
2 2
𝑑𝑛𝑒𝑤 = 𝐹 𝑥𝑝 + 2 , 𝑦𝑝 − 1.5 = 𝑥𝑝 + 2 + 𝑦𝑝 − 1.5 − 𝑟2
𝑑𝑛𝑒𝑤 − 𝑑 = ∆𝑆𝐸
𝑑𝑛𝑒𝑤 = 𝑑 + ∆𝑆𝐸
𝑑𝑛𝑒𝑤 = 𝑑 + ∆𝑆𝐸
26
Midpoint Circle Algorithm (Cont’)
Initial condition 𝑑0
𝑑0 = 𝐹 1, 𝑟 − 0.5 − 𝐹 0, 𝑟 = 1.25 − 𝑟 = 1 − 𝑟
𝑑0 = 1 − 𝑟
27
Midpoint Circle Algorithm using
matlab
function [xo,yo]=MidpointCircleAlgorithm (r)
if h<0
y(k+1)=y(k);
h=h+2*x(k)+3;
else
y(k+1)=y(k)-1;
h=h+2*x(k)-2*y(k)+5;
end
k=k+1;
end
xo=[x -x -y -y -x x y y];
yo=[y y x -x -y -y -x x];
plot(xo,yo,'ro')
28
Tracing Midpoint Circle Algorithm
MidpointCircleAlgorithm (0,0,5)
𝑑0 = 1 − 𝑟
R x y 𝑑h
𝑖𝑓 𝑑 < 0 5 0 5 -4
1 5 -1
𝑑𝑛𝑒𝑤 = 𝑑 + ∆𝐸 = 𝑑 + 2𝑥𝑝 + 3 2 5 4
3 4
𝑖𝑓 𝑑 > 0
What are the mapped seven circle points in the other octants
29
Tracing Midpoint Circle Algorithm
(cont’)
For the point (0,5) there are only three circle points:
(0,-5)
(5, 0)
(-5, 0)
30
Midpoint Ellipse
Algorithm
Loosely stated, an ellipse is an elongated circle
𝑥2 𝑦2
𝐹 𝑥, 𝑦 = + −1=0
𝑎2 𝑏2
32
Quick Revision (Cont’)
33