SlideShare a Scribd company logo
Larry F. Hodges
(modified by Amos Johnson)
1
Design of Line, Circle & Ellipse
Algorithms
Larry F. Hodges
(modified by Amos Johnson)
2
y2-y1
SLOPE =
RISE
RUN
=
x2-x1
Basic Math Review
Slope-Intercept Formula For A Line
Given a third point on the line:
P = (x,y)
Slope = (y - y1)/(x - x1)
= (y2 - y1)/(x2 - x1)
Solving For y
y = [(y2-y1)/(x2-x1)]x
+ [-(y2-y1)/(x2-x1)]x1 + y1
therefore
y = Mx + B
where
M = [(y2-y1)/(x2-x1)]
B = [-(y2-y1)/(x2-x1)]y1 + y1
Cartesian Coordinate System
2
4
3
5
6
1 P1 = (x1,y1)
P2 = (x2,y2)
P = (x,y)
1 3 4 5 6 7
Larry F. Hodges
(modified by Amos Johnson)
3
Other Helpful Formulas
Length of line segment between P1 and P2:
L = sqrt[ (x2-x1)2 + (y2-y1)2 ]
Midpoint of a line segment between P1 and P3:
P2 = ( (x1+x3)/2 , (y1+y3)/2 )
Two lines are perpendicular iff
1) M1 = -1/M2
or
2) Cosine of the angle between them is 0.
Larry F. Hodges
(modified by Amos Johnson)
4
Parametric Form Of The Equation Of
A 2D Line Segment
Given points P1 = (x1, y1) and P2 = (x2, y2)
x = x1 + t(x2-x1)
y = y1 + t(y2-y1)
t is called the parameter. When
t = 0 we get (x1,y1)
t = 1 we get (x2,y2)
As 0 < t < 1 we get all the other points on the line segment between (x1,y1)
and (x2,y2).
Larry F. Hodges
(modified by Amos Johnson)
5
Basic Line and Circle Algorithms
1. Must compute integer coordinates of pixels which lie on or near a line or
circle.
2. Pixel level algorithms are invoked hundreds or thousands of times when an
image is created or modified.
3. Lines must create visually satisfactory images.
• Lines should appear straight
• Lines should terminate accurately
• Lines should have constant density
• Line density should be independent of line length and angle.
4. Line algorithm should always be defined.
Larry F. Hodges
(modified by Amos Johnson)
6
Simple DDA Line Algorithm
{Based on the parametric equation of a line}
Procedure DDA(X1,Y1,X2,Y2 :Integer);
Var Length, I :Integer;
X,Y,Xinc,Yinc :Real;
Begin
Length := ABS(X2 - X1);
If ABS(Y2 - Y1) > Length Then
Length := ABS(Y2-Y1);
Xinc := (X2 - X1)/Length;
Yinc := (Y2 - Y1)/Length;
X := X1;
Y := Y1;
DDA (digital differential analyzer) creates good lines but it is too time
consuming due to the round function and long operations on real values.
For I := 0 To Length Do
Begin
Plot(Round(X), Round(Y));
X := X + Xinc;
Y := Y + Yinc
End {For}
End; {DDA}
Larry F. Hodges
(modified by Amos Johnson)
7
DDA Example
Compute which pixels should be turned on to represent the line from (6,9) to
(11,12).
Length := Max of (ABS(11-6), ABS(12-9)) = 5
Xinc := 1
Yinc := 0.6
Values computed are:
(6,9), (7,9.6),
(8,10.2), (9,10.8),
(10,11.4), (11,12)
6 7 8 9 10 11 12 13
9
10
11
12
13
Larry F. Hodges
(modified by Amos Johnson)
8
Simple Circle Algorithms
Since the equation for a circle on radius r centered at (0,0) is
x2 + y2 = r2,
an obvious choice is to plot
y = ±sqrt(r2 - x2)
for -r <= x <= r.
This works, but is inefficient because of the
multiplications and square root
operations. It also creates large gaps in the
circle for values of x close to R (and clumping for x near 0).
A better approach, which is still inefficient but avoids the gaps is to plot
x = r cosø
y = r sinø
as ø takes on values between 0 and 360 degrees.
Larry F. Hodges
(modified by Amos Johnson)
9
Fast Lines Using The Midpoint Method
Assumptions: Assume we wish to draw a line between points (0,0) and (a,b) with slope
M between 0 and 1 (i.e. line lies in first octant).
The general formula for a line is y = Mx + B where M is the slope of the line
and B is the y-intercept. From our assumptions M = b/a and B = 0.
Therefore y = (b/a)x + 0 is f(x,y) = bx – ay = 0 (an equation for the line).
If (x1,y1) lie on the line with M = b/a and B = 0, then
f(x1,y1) = 0.
+x
-x
-y
+y
(a,b)
(0,0)
Larry F. Hodges
(modified by Amos Johnson)
10
Fast Lines (cont.)
For lines in the first octant, the next
pixel is to the right or to the right
and up.
Assume:
Distance between pixels centers = 1
Having turned on pixel P at (xi, yi), the next pixel is T at (xi+1, yi+1) or S at
(xi+1, yi). Choose the pixel closer to the line f(x, y) = bx - ay = 0.
The midpoint between pixels S and T is (xi + 1,yi + 1/2). Let e be the
difference between the midpoint and where the line actually crosses
between S and T. If e is positive the line crosses above the midpoint and is
closer to T. If e is negative, the line crosses below the midpoint and is
closer to S. To pick the correct point we only need to know the sign of e.
(xi +1, yi + 1/2 + e)
e
(xi +1,yi + 1/2)
P = (xi,yi ) S = (xi + 1, yi )
T = (xi + 1, yi + 1)
Larry F. Hodges
(modified by Amos Johnson)
11
Fast Lines - The Decision Variable
f(xi+1,yi+ 1/2 + e) = b(xi+1) - a(yi+ 1/2 + e) = b(xi + 1) - a(yi + 1/2) -ae
= f(xi + 1, yi + 1/2) - ae = 0
Let di = f(xi + 1, yi + 1/2) = ae; di is known as the decision variable.
Since a >= 0, di has the same sign as e.
Algorithm:
If di >= 0 Then
Choose T = (xi + 1, yi + 1) as next point
di+1 = f(xi+1 + 1, yi+1 + 1/2) = f(xi +1+1,yi +1+1/2)
= b(xi +1+1) - a(yi +1+1/2) = f(xi + 1, yi + 1/2) + b - a
= di + b - a
Else
Choose S = (xi + 1, yi) as next point
di+1 = f(xi+1 + 1, yi+1 + 1/2) = f(xi +1+1,yi +1/2)
= b(xi +1+1) - a(yi +1/2) = f(xi + 1, yi + 1/2) + b
= di + b
Larry F. Hodges
(modified by Amos Johnson)
12
x := 0;
y := 0;
d := b - a/2;
For i := 0 to a do
Plot(x,y);
If d >= 0 Then
x := x + 1;
y := y + 1;
d := d + b – a
Else
x := x + 1;
d := d + b
End
End
Fast Line Algorithm
Note: The only non-integer value is a/2. If we then multiply by 2 to get d' = 2d, we can do all
integer arithmetic using only the operations +, -, and left-shift. The algorithm still works since we
only care about the sign, not the value of d.
The initial value for the decision variable, d0,
may be calculated directly from the formula at
point (0,0).
d0 = f(0 + 1, 0 + 1/2) = b(1) - a(1/2) = b - a/2
Therefore, the algorithm for a line from (0,0) to
(a,b) in the first octant is:
Larry F. Hodges
(modified by Amos Johnson)
13
Bresenham’s Line Algorithm
We can also generalize the
algorithm to work for lines
beginning at points other than
(0,0) by giving x and y the proper
initial values.
This results in Bresenham's Line
Algorithm.
Begin {Bresenham for lines with slope between 0 and 1}
a := ABS(xend - xstart);
b := ABS(yend - ystart);
d := 2*b - a;
Incr1 := 2*(b-a);
Incr2 := 2*b;
If xstart > xend Then
x := xend;
y := yend
Else
x := xstart;
y := ystart
End
For I := 0 to a Do
Plot(x,y);
x := x + 1;
If d >= 0 Then
y := y + 1;
d := d + incr1
Else
d := d + incr2
End
End {For Loop}
End {Bresenham}
Note: This algorithm only works for lines with
Slopes between 0 and 1
Larry F. Hodges
(modified by Amos Johnson)
14
Circle Drawing Algorithm
We only need to calculate the values on the border of the circle in the first
octant. The other values may be determined by symmetry. Assume a
circle of radius r with center at (0,0).
Procedure Circle_Points(x,y :Integer);
Begin
Plot(x,y);
Plot(y,x);
Plot(y,-x);
Plot(x,-y);
Plot(-x,-y);
Plot(-y,-x);
Plot(-y,x);
Plot(-x,y)
End;
(a,b)
(b,a)
(a,-b)
(b,-a)
(-a,-b)
(-a,b)
(-b,-a)
(-b,a)
Larry F. Hodges
(modified by Amos Johnson)
15
Fast Circles
Consider only the first octant of a circle of radius r centered on the origin.
We begin by plotting point (r,0) and end when x < y.
The decision at each step is whether to choose the pixel directly above the
current pixel or the pixel which is above and to the left.
Assume Pi = (xi, yi) is the current pixel.
Ti = (xi, yi +1) is the pixel directly above
Si = (xi -1, yi +1) is the pixel above and to the left.
x=y
x + y - r = 0
2
2 2
Larry F. Hodges
(modified by Amos Johnson)
16
Fast Circles - The Decision Variable
f(x,y) = x2 + y2 - r2 = 0
f(xi - 1/2 + e, yi + 1)
= (xi - 1/2 + e)2 + (yi + 1)2 - r2
= (xi- 1/2)2 + (yi+1)2 - r2 + 2(xi-1/2)e + e2
= f(xi - 1/2, yi + 1) + 2(xi - 1/2)e + e2 = 0
Let di = f(xi - 1/2, yi+1) = -2(xi - 1/2)e - e2 Thus,
If e < 0 then di > 0 so choose point S = (xi - 1, yi + 1).
di+1 = f(xi - 1 - 1/2, yi + 1 + 1) = ((xi - 1/2) - 1)2 + ((yi + 1) + 1)2 - r2
= di - 2(xi -1) + 2(yi + 1) + 1
= di + 2(yi+1- xi+1) + 1
If e >= 0 then di <= 0 so choose point T = (xi, yi + 1).
di+1 = f(xi - 1/2, yi + 1 + 1)
= di + 2yi+1 + 1
P = (xi ,yi )
T = (xi ,yi +1)
S = (xi -1,yi +1)
e
(xi -1/2, yi + 1)
Larry F. Hodges
(modified by Amos Johnson)
17
Fast Circles - Decision Variable (cont.)
The initial value of di is
d0 = f(r - 1/2, 0 + 1) = (r - 1/2)2 + 12 - r2
= 5/4 - r {1-r can be used if r is an integer}
When point S = (xi - 1, yi + 1) is chosen then
di+1 = di + -2xi+1 + 2yi+1 + 1
When point T = ( xi , yi + 1) is chosen then
di+1 = di + 2yi+1 + 1
Larry F. Hodges
(modified by Amos Johnson)
18
Fast Circle Algorithm
Begin {Circle}
x := r;
y := 0;
d := 1 - r;
Repeat
Circle_Points(x,y);
y := y + 1;
If d <= 0 Then
d := d + 2*y + 1
Else
x := x - 1;
d := d + 2*(y-x) + 1
End
Until x < y
End; {Circle}
Procedure Circle_Points(x,y :Integer);
Begin
Plot(x,y);
Plot(y,x);
Plot(y,-x);
Plot(x,-y);
Plot(-x,-y);
Plot(-y,-x);
Plot(-y,x);
Plot(-x,y)
End;
Larry F. Hodges
(modified by Amos Johnson)
19
Fast Ellipses
The circle algorithm can be generalized to work for an ellipse but only four way
symmetry can be used.
F(x,y) = b2x2 + a2y2 -a2b2 = 0
(a,0)
(-a,0)
(0,b)
(0,-b)
(x, y)
(x, -y)
(-x, y)
(-x, -y)
Larry F. Hodges
(modified by Amos Johnson)
20
Fast Ellipses
The circle algorithm can be generalized to work for an ellipse but only four way
symmetry can be used.
F(x,y) = b2x2 + a2y2 -a2b2 = 0
All the points in one quadrant must be computed. Since Bresenham's algorithm is
restricted to only one octant, the computation must occur in two stages.
The changeover occurs when the point on the ellipse is reached where the tangent line
has a slope of ±1. In the first quadrant, this is where the line y = x intersects the
ellipses.
(a,0)
(-a,0)
(0,b)
(0,-b)
(x, y)
(x, -y)
(-x, y)
(-x, -y)
y = x
Larry F. Hodges
(modified by Amos Johnson)
21
Line and Circle References
Bresenham, J.E., "Ambiguities In Incremental Line Rastering," IEEE
Computer Graphics And Applications, Vol. 7, No. 5, May 1987.
Eckland, Eric, "Improved Techniques For Optimising Iterative Decision-
Variable Algorithms, Drawing Anti-Aliased Lines Quickly And Creating
Easy To Use Color Charts," CSC 462 Project Report, Department of
Computer Science, North Carolina State University (Spring 1987).
Foley, J.D. and A. Van Dam, Fundamentals of Interactive Computer
Graphics, Addison-Wesley 1982.
Newman, W.M and R.F. Sproull, Principles Of Interactive Computer
Graphics, McGraw-Hill, 1979.
Van Aken J. and Mark Novak, "Curve Drawing Algorithms For Raster
Display," ACM Transactions On Graphics, Vol. 4, No. 3, April 1985.

More Related Content

Similar to 2D_line_circle.ppt

Edited Per4 Analytic Geometry
Edited Per4 Analytic GeometryEdited Per4 Analytic Geometry
Edited Per4 Analytic Geometry
ingroy
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
Muhammad Fiaz
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
Kumar
 
AEM Integrating factor to orthogonal trajactories
AEM Integrating factor to orthogonal trajactoriesAEM Integrating factor to orthogonal trajactories
AEM Integrating factor to orthogonal trajactories
Sukhvinder Singh
 
Mathematics 3.pdf civil engineering concrete
Mathematics 3.pdf civil engineering concreteMathematics 3.pdf civil engineering concrete
Mathematics 3.pdf civil engineering concrete
mocr84810
 
Some Common Fixed Point Theorems for Multivalued Mappings in Two Metric Spaces
Some Common Fixed Point Theorems for Multivalued Mappings in Two Metric SpacesSome Common Fixed Point Theorems for Multivalued Mappings in Two Metric Spaces
Some Common Fixed Point Theorems for Multivalued Mappings in Two Metric Spaces
IJMER
 
Ellipses drawing algo.
Ellipses drawing algo.Ellipses drawing algo.
Ellipses drawing algo.Mohd Arif
 
ISI MSQE Entrance Question Paper (2013)
ISI MSQE Entrance Question Paper (2013)ISI MSQE Entrance Question Paper (2013)
ISI MSQE Entrance Question Paper (2013)
CrackDSE
 
Fourier 3
Fourier 3Fourier 3
Fourier 3
nugon
 
Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.Mohd Arif
 
Interpolation functions
Interpolation functionsInterpolation functions
Interpolation functions
Tarun Gehlot
 
5icdsa2007 v4
5icdsa2007 v45icdsa2007 v4
5icdsa2007 v4fminhos
 
Continuity Of Functions
Continuity Of FunctionsContinuity Of Functions
Continuity Of Functions
Yash Thakkar
 
Bahan ajar kalkulus integral
Bahan ajar kalkulus integralBahan ajar kalkulus integral
Bahan ajar kalkulus integral
grand_livina_good
 
Last+minute+revision(+Final)+(1) (1).pptx
Last+minute+revision(+Final)+(1) (1).pptxLast+minute+revision(+Final)+(1) (1).pptx
Last+minute+revision(+Final)+(1) (1).pptx
AryanMishra860130
 
DIFFERENTIAL EQUATION
DIFFERENTIAL EQUATIONDIFFERENTIAL EQUATION
DIFFERENTIAL EQUATION
shahzadebaujiti
 
Lecture_Slides_Mathematics_06_Optimization.pdf
Lecture_Slides_Mathematics_06_Optimization.pdfLecture_Slides_Mathematics_06_Optimization.pdf
Lecture_Slides_Mathematics_06_Optimization.pdf
SantiagoGarridoBulln
 
Tcu12 crc multi
Tcu12 crc multiTcu12 crc multi
Tcu12 crc multi
Sahiris Seg
 

Similar to 2D_line_circle.ppt (20)

Edited Per4 Analytic Geometry
Edited Per4 Analytic GeometryEdited Per4 Analytic Geometry
Edited Per4 Analytic Geometry
 
Bresenham circle
Bresenham circleBresenham circle
Bresenham circle
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
AEM Integrating factor to orthogonal trajactories
AEM Integrating factor to orthogonal trajactoriesAEM Integrating factor to orthogonal trajactories
AEM Integrating factor to orthogonal trajactories
 
Mathematics 3.pdf civil engineering concrete
Mathematics 3.pdf civil engineering concreteMathematics 3.pdf civil engineering concrete
Mathematics 3.pdf civil engineering concrete
 
Some Common Fixed Point Theorems for Multivalued Mappings in Two Metric Spaces
Some Common Fixed Point Theorems for Multivalued Mappings in Two Metric SpacesSome Common Fixed Point Theorems for Multivalued Mappings in Two Metric Spaces
Some Common Fixed Point Theorems for Multivalued Mappings in Two Metric Spaces
 
Line integrals
Line integralsLine integrals
Line integrals
 
Ellipses drawing algo.
Ellipses drawing algo.Ellipses drawing algo.
Ellipses drawing algo.
 
ISI MSQE Entrance Question Paper (2013)
ISI MSQE Entrance Question Paper (2013)ISI MSQE Entrance Question Paper (2013)
ISI MSQE Entrance Question Paper (2013)
 
Fourier 3
Fourier 3Fourier 3
Fourier 3
 
Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.
 
Interpolation functions
Interpolation functionsInterpolation functions
Interpolation functions
 
5icdsa2007 v4
5icdsa2007 v45icdsa2007 v4
5icdsa2007 v4
 
Continuity Of Functions
Continuity Of FunctionsContinuity Of Functions
Continuity Of Functions
 
Bahan ajar kalkulus integral
Bahan ajar kalkulus integralBahan ajar kalkulus integral
Bahan ajar kalkulus integral
 
Last+minute+revision(+Final)+(1) (1).pptx
Last+minute+revision(+Final)+(1) (1).pptxLast+minute+revision(+Final)+(1) (1).pptx
Last+minute+revision(+Final)+(1) (1).pptx
 
DIFFERENTIAL EQUATION
DIFFERENTIAL EQUATIONDIFFERENTIAL EQUATION
DIFFERENTIAL EQUATION
 
Lecture_Slides_Mathematics_06_Optimization.pdf
Lecture_Slides_Mathematics_06_Optimization.pdfLecture_Slides_Mathematics_06_Optimization.pdf
Lecture_Slides_Mathematics_06_Optimization.pdf
 
Tcu12 crc multi
Tcu12 crc multiTcu12 crc multi
Tcu12 crc multi
 

More from PuneetMathur39

Unit 5_ Materials Phase diagram_Part 2.pptx
Unit 5_ Materials Phase diagram_Part 2.pptxUnit 5_ Materials Phase diagram_Part 2.pptx
Unit 5_ Materials Phase diagram_Part 2.pptx
PuneetMathur39
 
3972991.ppt
3972991.ppt3972991.ppt
3972991.ppt
PuneetMathur39
 
Ch-5.0_Robot Technology - CIM.pptx
Ch-5.0_Robot Technology - CIM.pptxCh-5.0_Robot Technology - CIM.pptx
Ch-5.0_Robot Technology - CIM.pptx
PuneetMathur39
 
Geometric Modeling.pptx
Geometric Modeling.pptxGeometric Modeling.pptx
Geometric Modeling.pptx
PuneetMathur39
 
14 Emotional Development.ppt
14 Emotional Development.ppt14 Emotional Development.ppt
14 Emotional Development.ppt
PuneetMathur39
 
Academic audit presentation.pptx
Academic audit presentation.pptxAcademic audit presentation.pptx
Academic audit presentation.pptx
PuneetMathur39
 
DSR_Unit-5_Sensors.pptx
DSR_Unit-5_Sensors.pptxDSR_Unit-5_Sensors.pptx
DSR_Unit-5_Sensors.pptx
PuneetMathur39
 
Ch-1_CIM-intro.pptx
Ch-1_CIM-intro.pptxCh-1_CIM-intro.pptx
Ch-1_CIM-intro.pptx
PuneetMathur39
 
Project Presentation.pptx
Project Presentation.pptxProject Presentation.pptx
Project Presentation.pptx
PuneetMathur39
 
power transmission system.pptx
power transmission system.pptxpower transmission system.pptx
power transmission system.pptx
PuneetMathur39
 

More from PuneetMathur39 (10)

Unit 5_ Materials Phase diagram_Part 2.pptx
Unit 5_ Materials Phase diagram_Part 2.pptxUnit 5_ Materials Phase diagram_Part 2.pptx
Unit 5_ Materials Phase diagram_Part 2.pptx
 
3972991.ppt
3972991.ppt3972991.ppt
3972991.ppt
 
Ch-5.0_Robot Technology - CIM.pptx
Ch-5.0_Robot Technology - CIM.pptxCh-5.0_Robot Technology - CIM.pptx
Ch-5.0_Robot Technology - CIM.pptx
 
Geometric Modeling.pptx
Geometric Modeling.pptxGeometric Modeling.pptx
Geometric Modeling.pptx
 
14 Emotional Development.ppt
14 Emotional Development.ppt14 Emotional Development.ppt
14 Emotional Development.ppt
 
Academic audit presentation.pptx
Academic audit presentation.pptxAcademic audit presentation.pptx
Academic audit presentation.pptx
 
DSR_Unit-5_Sensors.pptx
DSR_Unit-5_Sensors.pptxDSR_Unit-5_Sensors.pptx
DSR_Unit-5_Sensors.pptx
 
Ch-1_CIM-intro.pptx
Ch-1_CIM-intro.pptxCh-1_CIM-intro.pptx
Ch-1_CIM-intro.pptx
 
Project Presentation.pptx
Project Presentation.pptxProject Presentation.pptx
Project Presentation.pptx
 
power transmission system.pptx
power transmission system.pptxpower transmission system.pptx
power transmission system.pptx
 

Recently uploaded

Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 

Recently uploaded (20)

Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 

2D_line_circle.ppt

  • 1. Larry F. Hodges (modified by Amos Johnson) 1 Design of Line, Circle & Ellipse Algorithms
  • 2. Larry F. Hodges (modified by Amos Johnson) 2 y2-y1 SLOPE = RISE RUN = x2-x1 Basic Math Review Slope-Intercept Formula For A Line Given a third point on the line: P = (x,y) Slope = (y - y1)/(x - x1) = (y2 - y1)/(x2 - x1) Solving For y y = [(y2-y1)/(x2-x1)]x + [-(y2-y1)/(x2-x1)]x1 + y1 therefore y = Mx + B where M = [(y2-y1)/(x2-x1)] B = [-(y2-y1)/(x2-x1)]y1 + y1 Cartesian Coordinate System 2 4 3 5 6 1 P1 = (x1,y1) P2 = (x2,y2) P = (x,y) 1 3 4 5 6 7
  • 3. Larry F. Hodges (modified by Amos Johnson) 3 Other Helpful Formulas Length of line segment between P1 and P2: L = sqrt[ (x2-x1)2 + (y2-y1)2 ] Midpoint of a line segment between P1 and P3: P2 = ( (x1+x3)/2 , (y1+y3)/2 ) Two lines are perpendicular iff 1) M1 = -1/M2 or 2) Cosine of the angle between them is 0.
  • 4. Larry F. Hodges (modified by Amos Johnson) 4 Parametric Form Of The Equation Of A 2D Line Segment Given points P1 = (x1, y1) and P2 = (x2, y2) x = x1 + t(x2-x1) y = y1 + t(y2-y1) t is called the parameter. When t = 0 we get (x1,y1) t = 1 we get (x2,y2) As 0 < t < 1 we get all the other points on the line segment between (x1,y1) and (x2,y2).
  • 5. Larry F. Hodges (modified by Amos Johnson) 5 Basic Line and Circle Algorithms 1. Must compute integer coordinates of pixels which lie on or near a line or circle. 2. Pixel level algorithms are invoked hundreds or thousands of times when an image is created or modified. 3. Lines must create visually satisfactory images. • Lines should appear straight • Lines should terminate accurately • Lines should have constant density • Line density should be independent of line length and angle. 4. Line algorithm should always be defined.
  • 6. Larry F. Hodges (modified by Amos Johnson) 6 Simple DDA Line Algorithm {Based on the parametric equation of a line} Procedure DDA(X1,Y1,X2,Y2 :Integer); Var Length, I :Integer; X,Y,Xinc,Yinc :Real; Begin Length := ABS(X2 - X1); If ABS(Y2 - Y1) > Length Then Length := ABS(Y2-Y1); Xinc := (X2 - X1)/Length; Yinc := (Y2 - Y1)/Length; X := X1; Y := Y1; DDA (digital differential analyzer) creates good lines but it is too time consuming due to the round function and long operations on real values. For I := 0 To Length Do Begin Plot(Round(X), Round(Y)); X := X + Xinc; Y := Y + Yinc End {For} End; {DDA}
  • 7. Larry F. Hodges (modified by Amos Johnson) 7 DDA Example Compute which pixels should be turned on to represent the line from (6,9) to (11,12). Length := Max of (ABS(11-6), ABS(12-9)) = 5 Xinc := 1 Yinc := 0.6 Values computed are: (6,9), (7,9.6), (8,10.2), (9,10.8), (10,11.4), (11,12) 6 7 8 9 10 11 12 13 9 10 11 12 13
  • 8. Larry F. Hodges (modified by Amos Johnson) 8 Simple Circle Algorithms Since the equation for a circle on radius r centered at (0,0) is x2 + y2 = r2, an obvious choice is to plot y = ±sqrt(r2 - x2) for -r <= x <= r. This works, but is inefficient because of the multiplications and square root operations. It also creates large gaps in the circle for values of x close to R (and clumping for x near 0). A better approach, which is still inefficient but avoids the gaps is to plot x = r cosø y = r sinø as ø takes on values between 0 and 360 degrees.
  • 9. Larry F. Hodges (modified by Amos Johnson) 9 Fast Lines Using The Midpoint Method Assumptions: Assume we wish to draw a line between points (0,0) and (a,b) with slope M between 0 and 1 (i.e. line lies in first octant). The general formula for a line is y = Mx + B where M is the slope of the line and B is the y-intercept. From our assumptions M = b/a and B = 0. Therefore y = (b/a)x + 0 is f(x,y) = bx – ay = 0 (an equation for the line). If (x1,y1) lie on the line with M = b/a and B = 0, then f(x1,y1) = 0. +x -x -y +y (a,b) (0,0)
  • 10. Larry F. Hodges (modified by Amos Johnson) 10 Fast Lines (cont.) For lines in the first octant, the next pixel is to the right or to the right and up. Assume: Distance between pixels centers = 1 Having turned on pixel P at (xi, yi), the next pixel is T at (xi+1, yi+1) or S at (xi+1, yi). Choose the pixel closer to the line f(x, y) = bx - ay = 0. The midpoint between pixels S and T is (xi + 1,yi + 1/2). Let e be the difference between the midpoint and where the line actually crosses between S and T. If e is positive the line crosses above the midpoint and is closer to T. If e is negative, the line crosses below the midpoint and is closer to S. To pick the correct point we only need to know the sign of e. (xi +1, yi + 1/2 + e) e (xi +1,yi + 1/2) P = (xi,yi ) S = (xi + 1, yi ) T = (xi + 1, yi + 1)
  • 11. Larry F. Hodges (modified by Amos Johnson) 11 Fast Lines - The Decision Variable f(xi+1,yi+ 1/2 + e) = b(xi+1) - a(yi+ 1/2 + e) = b(xi + 1) - a(yi + 1/2) -ae = f(xi + 1, yi + 1/2) - ae = 0 Let di = f(xi + 1, yi + 1/2) = ae; di is known as the decision variable. Since a >= 0, di has the same sign as e. Algorithm: If di >= 0 Then Choose T = (xi + 1, yi + 1) as next point di+1 = f(xi+1 + 1, yi+1 + 1/2) = f(xi +1+1,yi +1+1/2) = b(xi +1+1) - a(yi +1+1/2) = f(xi + 1, yi + 1/2) + b - a = di + b - a Else Choose S = (xi + 1, yi) as next point di+1 = f(xi+1 + 1, yi+1 + 1/2) = f(xi +1+1,yi +1/2) = b(xi +1+1) - a(yi +1/2) = f(xi + 1, yi + 1/2) + b = di + b
  • 12. Larry F. Hodges (modified by Amos Johnson) 12 x := 0; y := 0; d := b - a/2; For i := 0 to a do Plot(x,y); If d >= 0 Then x := x + 1; y := y + 1; d := d + b – a Else x := x + 1; d := d + b End End Fast Line Algorithm Note: The only non-integer value is a/2. If we then multiply by 2 to get d' = 2d, we can do all integer arithmetic using only the operations +, -, and left-shift. The algorithm still works since we only care about the sign, not the value of d. The initial value for the decision variable, d0, may be calculated directly from the formula at point (0,0). d0 = f(0 + 1, 0 + 1/2) = b(1) - a(1/2) = b - a/2 Therefore, the algorithm for a line from (0,0) to (a,b) in the first octant is:
  • 13. Larry F. Hodges (modified by Amos Johnson) 13 Bresenham’s Line Algorithm We can also generalize the algorithm to work for lines beginning at points other than (0,0) by giving x and y the proper initial values. This results in Bresenham's Line Algorithm. Begin {Bresenham for lines with slope between 0 and 1} a := ABS(xend - xstart); b := ABS(yend - ystart); d := 2*b - a; Incr1 := 2*(b-a); Incr2 := 2*b; If xstart > xend Then x := xend; y := yend Else x := xstart; y := ystart End For I := 0 to a Do Plot(x,y); x := x + 1; If d >= 0 Then y := y + 1; d := d + incr1 Else d := d + incr2 End End {For Loop} End {Bresenham} Note: This algorithm only works for lines with Slopes between 0 and 1
  • 14. Larry F. Hodges (modified by Amos Johnson) 14 Circle Drawing Algorithm We only need to calculate the values on the border of the circle in the first octant. The other values may be determined by symmetry. Assume a circle of radius r with center at (0,0). Procedure Circle_Points(x,y :Integer); Begin Plot(x,y); Plot(y,x); Plot(y,-x); Plot(x,-y); Plot(-x,-y); Plot(-y,-x); Plot(-y,x); Plot(-x,y) End; (a,b) (b,a) (a,-b) (b,-a) (-a,-b) (-a,b) (-b,-a) (-b,a)
  • 15. Larry F. Hodges (modified by Amos Johnson) 15 Fast Circles Consider only the first octant of a circle of radius r centered on the origin. We begin by plotting point (r,0) and end when x < y. The decision at each step is whether to choose the pixel directly above the current pixel or the pixel which is above and to the left. Assume Pi = (xi, yi) is the current pixel. Ti = (xi, yi +1) is the pixel directly above Si = (xi -1, yi +1) is the pixel above and to the left. x=y x + y - r = 0 2 2 2
  • 16. Larry F. Hodges (modified by Amos Johnson) 16 Fast Circles - The Decision Variable f(x,y) = x2 + y2 - r2 = 0 f(xi - 1/2 + e, yi + 1) = (xi - 1/2 + e)2 + (yi + 1)2 - r2 = (xi- 1/2)2 + (yi+1)2 - r2 + 2(xi-1/2)e + e2 = f(xi - 1/2, yi + 1) + 2(xi - 1/2)e + e2 = 0 Let di = f(xi - 1/2, yi+1) = -2(xi - 1/2)e - e2 Thus, If e < 0 then di > 0 so choose point S = (xi - 1, yi + 1). di+1 = f(xi - 1 - 1/2, yi + 1 + 1) = ((xi - 1/2) - 1)2 + ((yi + 1) + 1)2 - r2 = di - 2(xi -1) + 2(yi + 1) + 1 = di + 2(yi+1- xi+1) + 1 If e >= 0 then di <= 0 so choose point T = (xi, yi + 1). di+1 = f(xi - 1/2, yi + 1 + 1) = di + 2yi+1 + 1 P = (xi ,yi ) T = (xi ,yi +1) S = (xi -1,yi +1) e (xi -1/2, yi + 1)
  • 17. Larry F. Hodges (modified by Amos Johnson) 17 Fast Circles - Decision Variable (cont.) The initial value of di is d0 = f(r - 1/2, 0 + 1) = (r - 1/2)2 + 12 - r2 = 5/4 - r {1-r can be used if r is an integer} When point S = (xi - 1, yi + 1) is chosen then di+1 = di + -2xi+1 + 2yi+1 + 1 When point T = ( xi , yi + 1) is chosen then di+1 = di + 2yi+1 + 1
  • 18. Larry F. Hodges (modified by Amos Johnson) 18 Fast Circle Algorithm Begin {Circle} x := r; y := 0; d := 1 - r; Repeat Circle_Points(x,y); y := y + 1; If d <= 0 Then d := d + 2*y + 1 Else x := x - 1; d := d + 2*(y-x) + 1 End Until x < y End; {Circle} Procedure Circle_Points(x,y :Integer); Begin Plot(x,y); Plot(y,x); Plot(y,-x); Plot(x,-y); Plot(-x,-y); Plot(-y,-x); Plot(-y,x); Plot(-x,y) End;
  • 19. Larry F. Hodges (modified by Amos Johnson) 19 Fast Ellipses The circle algorithm can be generalized to work for an ellipse but only four way symmetry can be used. F(x,y) = b2x2 + a2y2 -a2b2 = 0 (a,0) (-a,0) (0,b) (0,-b) (x, y) (x, -y) (-x, y) (-x, -y)
  • 20. Larry F. Hodges (modified by Amos Johnson) 20 Fast Ellipses The circle algorithm can be generalized to work for an ellipse but only four way symmetry can be used. F(x,y) = b2x2 + a2y2 -a2b2 = 0 All the points in one quadrant must be computed. Since Bresenham's algorithm is restricted to only one octant, the computation must occur in two stages. The changeover occurs when the point on the ellipse is reached where the tangent line has a slope of ±1. In the first quadrant, this is where the line y = x intersects the ellipses. (a,0) (-a,0) (0,b) (0,-b) (x, y) (x, -y) (-x, y) (-x, -y) y = x
  • 21. Larry F. Hodges (modified by Amos Johnson) 21 Line and Circle References Bresenham, J.E., "Ambiguities In Incremental Line Rastering," IEEE Computer Graphics And Applications, Vol. 7, No. 5, May 1987. Eckland, Eric, "Improved Techniques For Optimising Iterative Decision- Variable Algorithms, Drawing Anti-Aliased Lines Quickly And Creating Easy To Use Color Charts," CSC 462 Project Report, Department of Computer Science, North Carolina State University (Spring 1987). Foley, J.D. and A. Van Dam, Fundamentals of Interactive Computer Graphics, Addison-Wesley 1982. Newman, W.M and R.F. Sproull, Principles Of Interactive Computer Graphics, McGraw-Hill, 1979. Van Aken J. and Mark Novak, "Curve Drawing Algorithms For Raster Display," ACM Transactions On Graphics, Vol. 4, No. 3, April 1985.