Professional Documents
Culture Documents
Geometry
The smallest element which can be shown on a graph is a point.
The point A is placed in (x, y) plane & (x, y) is the position point of A. This
means point A is at x distance x direction & it is at distance y in the y
direction from the origin (0, 0). The ordered pair (x, y) is known as the co-
ordinates of the point A.
Any two point specify in a plane will define a line. This means to
define a line we need at least two points. In the figure, there are two points
p1(x, y) & p2(x, y) that define a line L1. A random point (x1, y1) is set to be
on the line if & only if it specifies the equation of the line. The equation of a
straight line can be derived with the help of slope concept of a straight line
which measures the steepness of it. The slope is the rate at which an
ordinate of a point on the plane change with respect to a change in the
horizontal co-ordinate. This can be measured by the ratio between
differences of vertical co-ordinates of two points on the line to that of
horizontal co-ordinate of those two points. Generally, slope is denoted by
m.
1
.
Vertical distance between two points
. . m = ------------------------------------------------------
Horizontal distance between two points
Assume that, p1(x1, y1), p2(x2, y2) are the two points in a line, the slope
y2 - y1
m = ------------------
x2 - x1
The slope between any two points of a line always remains constant.
Let us consider two point p1(x1, y1) & p2(x2, y2) on a line. Let us also
assume that a point (x, y) on the same line according to equation of a
straight line slope between any twp points is constant. Then the slope is
y - y1 y2-y1
m = ----------------- & also m = -------------------
x - x1 x2-x1
2
y - y1 y2 - y1
------------------ = -----------------
x - x1 x2 - x1
y - y1
----------------- = m
x - x1
y - y1 = m(x - x1)
y - y1 = mx-mx1
y = mx-mx1+y1
y = mx + (y1-mx1)
y = mx + b
ax + by + c=0
where a, b and c are the constants, this equation can be derived as follows.
(y - y1) (y2 - y1)
--------------- = -----------------
(x - x1) (x2 -x1)
Assume that,
a = (y2 - y1)
b = (x2 - x1)
c = (y1x2- x1y2)
3
ie, ax + by + c = 0
Line segment
A line segment is a part of line which has two end points & we can
find out the length of a line segment and mid point of a line segment.
Let us imagine a line segment P1P2. The length of the line segment
can be find out by Pythagoras theorem
: l = ((x1- x2)2+(y1-y2)2)1/2
x 1 +x 2 , y 1 +y 2
--------- ----------
2 2
4
Equation of line parallel to- axis at a distance a is,
y=a
x=b
y=0
x=0
Where, m1 != m2, these two lines are not parallel to each other. Hence,
they will interact some point.
yi = m1xi + b1 &
yi = m2xi + b2
m1xi + b1 = m2xi + b2
m1xi + m2xi = b2 - b1
b2 – b1
xi = --------------
m1 – m2
yi = m1( b2 – b1 ) + b1
------------------
m1 – m2
5
m1b2 – m1b1
= -------------------------- + b1
m 1 – m2
m 1b 2 - m 2b 1
= -------------------
m1 – m2
y = m1x1+ b1 &
b2 – b1 m 1b 2 - m 2b 1
-------------- , ------------------
m1 –m2 m1 – m2
In this figure m1=m1, the slope of the both line is same, the two lines
are called parallel to each other,
6
In this figure m1 ≠ m2, the slopes are different but the intercept
values ‘b’ are same then such two lines pass through same point on y-
axis,
In this figure, the intercept value is 0 that means b=0, such line
passes though the origin & intercept at the origin.
7
Here, the line y = m1x + b1 & y = m2x + b 2 are perpendicular to each
other. This situation result into an intercepting relationship between the
two slope m1 & m2
Derived the relationship between m1 & m2 for the lines that are
perpendicular to each other
Let us consider two points P1(x1, y1) & P2(x2, y2) on two lines y1=
m1x & y2 = m2x respectively. As these two lines are perpendicular to each
other, the three points O(0,0), P1(x1, y1) & P2(x2, y2) form a right angled
triangle.
x12 - 2x1x2 + x22 + y12 - 2y1y2 + y22 = x12 + y12 + x22 + y22
8
-2 x1 x2 - 2y1y2 =0
-2 x1x2 = 2y1y2
- x1x2 = y1y2
- x2 y1
------- = ------
y2 x1
Since, y 1 = m1 x 1
y1
m1 = ----- &
x1
also y2 = m2x2
y2
m2 = -----
x2
-1
m1 = ------
m2
Therefore, if two lines y = m1x +b1 & y=m2x +b2 are perpendicular to each
other slope are negative reciprocals to each other.
The co-ordinate will be(i , j ) that gives column & row of a pixel .
9
Each pixel will be centered at the co-ordinates. We can place the intensity
values of all pixels into an array of our computers memory.
Our graphic display device can then access this array to determine
the intensity at each pixel should be displayed. This array which contains
internal representation of the image is called F rame buffer it collects &
stores pixel values for use by the display devices.
Given that two end points of a line segment are specified at position (x1,y1)
& (x2,y2). We can determine the value for the slope m & y intercept b using
the following equation,
y2 - y1
m = -------------- (2)
x2 - x1
b = y1 - mx1 (3)
Algorithm for displaying a line are based on the equation (1) and
calculations are given in equation (2) & (3).
For any given x interval ∆x along a line, we can compute y interval ∆y.
∆y = mx (4)
10
∆y
∆x = --------- (5)
m
For line with slope segment |m|<1 [gentle slope, ∆x > ∆y], ∆x can be
set proportional to a small horizontal deflection voltage & the
corresponding vertical deflection is then set proportional to ∆y. ∆y can be
calculated from equation (4)
For line whose slope have magnitude |m|>1[steep slope, ∆y > ∆x]
∆y can be set proportional to a small vertical deflection voltage with the
corresponding horizontal deflection voltage is then set proportional to ∆x.
∆x can be calculated from equation (5).
For line with m=1, (∆x = ∆y) [smooth slope], the horizontal & vertical
voltages are equal. In such case a smooth line with slope m is generated
between specified end points.
If ∆x > ∆y
Then |m|<=1
∆x=1
yk+1 – yk =(mxk+1 +b) - (mxk + b)
11
yk+1 - yk = mxk+1 – mxk
= m(xk+1 – xk)
Subscript k takes an integer value starting from 1, for the first point
& increase by 1 until the final point is reached. m can be any real number
between 0 & 1. Then calculate y value and must be rounded to the nearest
integer.
For the lines with positive slope>1, we reverse the role of x & y that
means we sample at unit y intervals (∆y=1) & calculate each succeeding x
values as xk+1 =xk + 1/m
If ∆y > ∆x
Then |m|=>1
∆y=1
yk+1– yk = (mxk+1 +b) - (mxk +b)
1 = m(xk+1)-m(xk)
= m(xk+1-xk)
xk+1 = xk + 1/m
Equation yk+1 = yk + m & xk+1 = xk+1/m are based on the assumption
that the line are to be processed from left end point to the right end point.
If this process is reversed(slope is negative), so that starting end
point is at the right then yk+1 = yk –m because ∆x = -1 & xk+1=xk-1/m
because ∆y = -1
DDA algorithm
Step 1: read the end points (x1, y1) & (x2, y2), so that they are not
equal
Step 3: if ∆x > ∆y
Step =∆x
Else
Step= ∆y
12
Step 4: ∆x =|x2-x1/steps|
∆y=|y2-y1/steps|
Step 6: i=0
while(i<=step)
{
plot(integer(x), integer(y))
x = x + ∆x
y = y+ ∆y
i = i+1
}
Step 7: stop
13
Bresenham line
An accurate & efficient raster line drawing algorithm developed by
Bresenham converts line using only incremental integer calculation that
can be adopted to display circle & other curves. The vertical axis shows
scan line positions & the horizontal axis shows pixel column.
First considering the scan conversion process for lines with positive
slope, i.e m< 1. Pixel positions along a line path are determined by
sampling at unit a interval.
Starting from the left end point (x0, y0) we step to each successive
columns (x position) & plot the pixel whose scan line y is closer to the line
path. That means next pixel should be (xk+1,yk) or (xk+1,yk+1).
14
At sampling xk+1 we label vertical pixel separation from the
mathematical line path d1 &d2 as shown in the figure.
y= m(xk+1) + b (1)
d1 = y - yk
= m(xk+1) + b - yk
d2 = yk+1 - yk
= yk+1 - (m(xk+1) +b )
= yk+1 - m(xk+1) - b
= 2∆yxk - 2∆xyk + c
The decision parameter pk for the kth step in the line algorithm can
be obtained by re-arranging the equation, so that it involves only integer
calculation. pk =∆x(d1-d2), the sign of pk is same as the sign of (d1-d2) and
the parameter c in the equation pk is constant, and has the value 2∆y +
2∆x(2b-1) , which is independent of the pixel position and will be
eliminated the following calculation
15
upper pixel at position k+1 the decision parameter can be calculated as,
if pk< 0 at k+1
position (xk+1, yk)
else
(xk+1, yk+1)
if pk<0 , yk+1 - yk = 0
else
yk+1 - yk = 1
Algorithm
Step 1 − Input the two end-points of line, storing the left end-point
in (x0,y0)(x0,y0).
Step 2 − Plot the point (x0,y0)(x0,y0).
Step 3 − Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get
the first value for the decision parameter as −
p0=2dy−dxp0=2dy−dx
Step 4 − At each XkXk along the line, starting at k = 0, perform the
following test −
If pkpk < 0, the next point to plot is (xk+1,yk)(xk+1,yk) and
pk+1=pk+2dypk+1=pk+2dy
Otherwise,
pk+1=pk+2dy−2dxpk+1=pk+2dy−2dx
16
Step 5 − Repeat step 4 (dx – 1) times.
For m > 1, find out whether you need to increment x while incrementing
y each time.
After solving, the equation for decision parameter PkPk will be very
similar, just the x and y in the equation gets interchanged.
BRESENHAM ALGORITHM
Values of the frame buffer along a line segment with integer end points
Arguments XA and YA are the co-ordinates of other endpoint
XB and YB are the co-ordinates of other endpoint
INTENSITY is the intensity setting to be used for vector.
Global FRAME the two-dimensional frame buffer array
Local DX, DY the vector to be drawn
R,C the row and column indices for the pixel
F the final row or column
G for testing for a new row or column
INC1 increment for G when row or column is unchaged
INC2 increment for G when row or column changes
POS-SLOPE a flag to indicate if the slope is positive
BEGIN
Determine the components of the vector
DX <- XB-XA;
DY <- YB-YA;
Determine the sign of the slope
POS-SLOPE <-(DX>0);
IF DY <0 THEN POS-SLOPE <- NOT POS-SLOPE ;
Decide on whether to step across columns or up rows
IF |DX| > |DY| THEN
BEGIN
This is the gentle slope case
IF DX > 0 THEN
BEGIN
C <- XA;
R <- YA;
F <- XB;
END
ELSE
BEGIN
C <- XB;
R <- YB;
F <- XA;
END;
INC1 <- 2 * |DY|;
G <- 2 * |DY| - |DX|;
17
INC2 <- 2* ( |DY| - |DX|);
IF POS-SLOPE THEN
BEGIN
Now step across line segment
WHILE C <= F DO
BEGIN
Set nearest pixel in the frame buffer
FRAME[C,R] <- INTENSITY;
Next column
C <- C + 1;
Should row change
IF G >= 0 THEN
BEGIN
R <- R + 1;
G <- G + INC2;
END
ELSE G <- G + INC1;
END;
END
ELSE
BEGIN
WHILE C <= F DO
BEGIN
Set nearest pixel in the frame buffer;
FRAME[C, R] <-INTENSITY;
Next column
C <- C + 1;
Should row change
IF G > 0 THEN
BEGIN
R <- R -1 ;
G <- INC2;
END
ELSE G <- G + INC1;
END;
END
ELSE
BEGIN
This is the sharp slope case
Here the above steps are repeated
With the roles of X and Y interchanged
END;
RETURN;
END;
18
Vecgen Algorithm
In vecgen algorithm two end points are storing (x a, ya) & (xb, yb).
Intensity setting is to be used for the vector. Then calculate the value of
Dx & Dy. This is used to draw the vector R & C, the Row & column indices
for the pixel to be changed. The stopping index will be F. The line segment
co-ordinate D can be calculated at starting point. The Variable H denotes
the differences between line segment & row index. M=Dy/Dx, the slope of
the line segment is calculated for the change in H, when a boundary is
cross.
For the gentle slope -1<m< 1, there are more columns than rows.
These are the line segment when the length of x components Dx = X b - Xa
is longer than the length of Y components Dy = Yb - Ya. In this cases we
step across the column & slope for the row.
For the sharp slope case, where Dx < Dy, we step up the row & solve
for column.
Instead of determining the fall of Y value of line segment for e ach
column, we shall only keep the record of current height above the closest
low boundary, at each step. At each step height increases by m along with
Y can check the height at each step to see if we have to move into a new
row then the change row value is used to select the pixel.
For lines with negative slope, the height is decreased & check for
crossing of a lower row boundary [R = R -1].
19
For the line with sharp slope case, similar procedure is used only the
role of X & Y are exchanged. A new height is found for a new row by
adding 1/m to the height of the old row.
For gentle slop case, we shall turn on pixel in the columns that have
center line which crosser the line segment.
We shall center columns & rows on integer co-ordinate values, so if
Xa is the left end of the line segment & Xb is the right end of the line
segment. Then columns between c = ceiling(Xa) & f = floor(Xb) will be
affected. Our starting point Y position will be corresponds to the point X =
c & may not be end point YA. The starting Y value may be determined
from the equation as follows:
D = mc + b
= mc + ya - mxa , where b = ya - mxa
= ya + m(c - xa)
r = floor(d + 0.5)
To find the height of Y above the row boundary, we take the
difference of Y & R and add m, then we have height value which should be
check the next column.
H=r–d+m
VECGEN ALGORITHM
Values of the frame buffer along a line segment with integer end points
Arguments XA and YA are the co-ordinates of other endpoint
XB and YB are the co-ordinates of other endpoint
INTENSITY is the intensity setting to be used for vector.
Global FRAME the two-dimensional frame buffer array
Local DX, DY the vector to be drawn
R,C the row and column indices for the pixel
F the stopping index
D the line segment co-ordinate at the starting point
H the difference between the line segment and the row index
M the slope of the line segment
M1 the change in H when a boundary is crossed
BEGIN
Determine the components of the vector
DX <- XB-XA;
DY <- YB-YA;
20
Decide on whether to step across columns or up rows
IF |DX| > |DY| THEN
BEGIN
This is the gentle slope case
M <- DY/DX;
Set up of starting point depends on which point is leftmost
IF DX > 0 THEN
BEGIN
C <- CEILING(XA);
D <- YA + M * (C – XA)
F <- FLOOR(XB);
END
ELSE
BEGIN
C <- CEILING(XB);
D <- YB + M * (C – XB)
F <- FLOOR(XA);
END;
R <- FLOOR(D + 0.5);
H <- R – D + M;
IF M > 0 THEN
BEGIN
The positive slope case
M1 <- M – 1;
Now step through the columns WHILE C<= F DO
BEGIN
Set the nearest pixel in the frame buffer
FRAME[C, R] <- INTENSITY;
Next column
C <- C + 1;
Show row change
IF H >= 0.5 THEN
BEGIN
R <- R + 1;
H <- H + M1;
END
ELSE H <- H + M;
END;
END
ELSE
BEGIN
Then negative slope case
21
M <- -M;
H <- -H;
M1 <- M – 1;
WHILE C <= F DO
BEGIN
Set the nearest pixel in the frame buffer
FRAME[C, R] <- INTENSITY;
Next column
C <- C + 1;
Should row change
IF H > 0.5 THEN
BEGIN
R <- R -1;
H <- H + M1;
END
ELSE H <- H + M;
END;
END;
ELSE
BEGIN
The sharp slope case
IF DY = 0.0 THEN RETURN;
Here the above steps are repeated
With the roles of x and y interchanged
END;
RETURN;
END;
END
ELSE
BEGIN
This is the sharp slope case
Here the above steps are repeated
With the roles of X and Y interchanged
END;
RETURN;
END;
Illustrate the vecgan algorithm and draw a line with the end points (20,10)
& (30,18).
C = ceiling(Xa) = 20
m = 8/10 = 0.8
F = floor(Xb) = 30
22
D =Ya + m(C - Xa ) =10 + 0.8(20-20) =10
R = floor(d + 0.5) = floor(10+0.5) =10
m = m – 1 = 0.8 – 1 = -0.2
H = r – d + m =10 – 10 +0.8 =0.8
*check with condition
If h >= 0.5
H= H+m
Else
H = H + m1
H= 0.8 + -0.2 =0.6 (20,10)
H= 0.6 + -0.2 =0.4 (21,11)
H= 0.4 + 0.8 =1.2 (22,11)
H= 1.2 + -0.2 =1 (23,12)
H= 1 + -0.2 =0.8 (24,13)
H= 0.8 + -0.2 =0.6 (25,14)
H= 0.6+ -0.2 =0.4 (26,15)
H= 0.4 + 0.8 =1.2 (27,16)
H= 1.2 + -0.2 =0.8 (28,17)
H= 0.8 + -0.2 =0.6 (29,18)
H= 0.6 + -0.2 =0.4 (30,18)
ANTIALIASING OF LINES
Many displays allow only two pixel states, on or off. For these
displays, lines may have a jagged or stair-step appearance when they step
from one row or column to the next. This is one aspect of a phenomenon
called aliasing. Aliasing produces the defects which occur when the scene
being displayed changes faster or more smoothly than every two pixel.
Displays which allow setting pixels to gray levels between black and white
provide a means to reduce this effect. The technique is called antialiasing,
and it uses the gray levels to gradually turn off the pixels in one row as it
gradually turns on the pixels in the next.
The vector generation algorithms can be modified to perform
antialiasing. Remember that for gentle sloped lines, the line position for
each column index and decided which row was closest. The line segment
would lie between two pixels, and we picked one. Suppose that instead of
picking the closest, we turned them both on. We should choose the
intensity values according to a function of the distance between the pixel
index and the line segment so that the pixel closest to the line receives
most of its intensity. The sum of the intensity values for the two pixels
should match the total intensity value for the line.
23
Aliasing is a fact of any computer image which depends on a grid of
finite number of pixels in the horizontal and vertical direction. It can not
be eliminated, but its visually irritating obviousness can be minimized.
2) By an Anti-Aliasing process:
Display two or more pixels around the computed location at varying
intensities, or by Dithering (combining pixels of available colours),
rather than a single pixel at the rounded off location.
y = mx + b
m = 0.3
x= 20
to 30 ,
b= 39
x= 20 21 22 23 24 25 26 27 28 29 30
y= 45 45.3 45.6 45.9 46.2 46.5 46.8 47.1 47.4 47.7 48
20 21 22 23 24 25 26 27 28 29 30
45
46
47
48
upper and 45 100 70 40 10
lower row 46 30 60 90 80 50 20
percentage 47 20 50 80 90 60 30
24
48 10 40 70 100
Line Styles:-
(x2,y2+ wy )
(x2,y2)
(x2, y2- wy )
Upper Line
Boundary
Lower Line
Boundary
(x1,y1+ wy )
x1,y1)
(x1,y1- wy )
25
Consider a line segment of slope m<1. Let P 1(x1, y1) & P2(x2, y2) are
the end points of a line segment with width w. Here two parallel line
segments will be drawn from a central line (x 1, y1) & (x2, y2) at a distance
w/2.
The co-ordinates of end points of upper & lower boundaries are (x 1,
y1+wy) & (x2, y2+wy) & (x1, y1-wy) & (x2, y2 -wy) respectively. The value of w y
can be calculated from the following equation:
w y = w-1 (x 2 - x 1 ) 2 + (y 2 - y 1 )2
2 | x2 - x1 |
w x = w-1 (x 2 - x 1 ) 2 + (y 2 - y 1 )2
2 | y 2 - y1 |
Line caps:-
There are three types of line caps are available in graphics application
to make the effect of thick lines more prominent.
They are
(a) Butt cap
(b) Round cap
26
(a) Butt cap:-
The butt cap is the cap with which end positions of the thick lines are
adjusted in such a way that the line segment appears with square ends.
The ends of such thick lines are perpendicular to the original line segment.
When two thick segments are connected with each other, they create
problem at joining point. When two thick line segments are joined an angle
is created at the joining point.
The problem that occurred while joining two thick lines can be
removed from the poly lines by adding some additional portion to the joint
by using some specific type of joining process. These joints are miter joint,
round joint and bevel joint as shown in the figure.
27
In Round joint, the exterior boundaries are connected with a circular
arc with a diameter of total thickness of two thick lines, which are joined.
2. Dot matrix/Bitmap.
Stroke method:-
This method creates character by a series of line segment like
strokes of a pen. We could build our own stroke method character
generator by using the line drawing (VECGEN) Algorithm. We could
decide what line segment are needed for each character & set ups the calls
to the line drawing (VECGEN) algorithm for each character we wish to. In
actual graphics display the commands for drawing the character line
segment may be in either hardware or software.
The stroke method provides itself to change of scale(dimension). The
character may be made twice as large by simply doubling the length of
each segment.
This array is like a small frame buffer, just big enough to hold
character. The dots are the pixels for this small array. The memory
containing the character dot matrix array is often a hardware device called
a character generator chip.
The size of a dot is fixed, so the dot matrix method does not allow
itself to change its size.
28
1 2
29