CG Notes Module2
CG Notes Module2
Output Primitives: Line Drawing Algorithms – Loading the Frame Buffer – Line Function – Circle
– Generating Algorithms - Attributes of Output Primitives: Line Attributes – Curve Attributes –
Color and Gray scale levels– Area fill Attributes – Character Attributes – Bundled Attributes –
Inquiry Functions.
Output Primitives
Point plotting is done by converting a single coordinate position furnished by an application program into
appropriate operations for the output device in use.
Line drawing is done by calculating intermediate positions along the line path between two specified
endpoint positions.
The output device is then directed to fill in those positions between the end points with some color.
For some device such as a pen plotter or random scan display, a straight line can be drawn smoothly from one
end point to other.
Digital devices display a straight line segment by plotting discrete points between the two endpoints.
Discrete coordinate positions along the line path are calculated from the equation of the line.
For a raster video display, the line intensity is loaded in frame buffer at the corresponding pixel positions.
Reading from the frame buffer, the video controller then plots the screen pixels.
Screen locations are referenced with integer values, so plotted positions may only approximate actual line
positions between two specified endpoints.
For example line position of (12.36, 23.87) would be converted to pixel position (12, 24).
This rounding of coordinate values to integers causes lines to be displayed with a stair step appearance
Fig. 2.1: - Stair step effect produced when line is generated as a series of pixel positions.
The stair step shape is noticeable in low resolution system, and we can improve their appearance somewhat
by displaying them on high resolution system.
More effective techniques for smoothing raster lines are based on adjusting pixel intensities along the line
paths.
For raster graphics device-level algorithms discuss here, object positions are specified directly in integer
device coordinates.
Pixel position will referenced according to scan-line number and column number which is illustrated by
following figure.
6
2
0 1 2 3 4 5 6
Fig. 2.2: - Pixel positions referenced by scan-line number and column number.
To load the specified color into the frame buffer at a particular position, we will assume we have
available low-level procedure of the form 𝑠𝑒𝑡𝑝𝑖𝑥𝑒𝑙(𝑥, 𝑦)
Similarly for retrieve the current frame buffer intensity we assume to have procedure 𝑔𝑒𝑡𝑝𝑖𝑥(𝑥, 𝑦).
The Line drawing algorithm is a graphical algorithm which is used to represent the line segment on discrete
graphical media, i.e., printer and pixel-based media.
An algorithm should be precise: Each step of the algorithm must be adequately defined.
Finiteness: An algorithm must contain finiteness. It means the algorithm stops after the execution of all
steps.
Easy to understand: An algorithm must help learners to understand the solution in a more natural way.
Correctness: An algorithm must be in the correct manner.
Effectiveness: The steps of an algorithm must be valid and efficient.
Uniqueness: All steps of an algorithm should be clearly and uniquely defined, and the result should be
based on the given input.
Input: A good algorithm must accept at least one or more input.
Output: An algorithm must generate at least one output.
We can define a straight line with the help of the following equation.
y= mx + a
Where,
(x, y) = axis of the line.
m = Slope of the line.
a = Interception point
Let us assume we have two points of the line (p1, q1) and (p2, q2).
Now, we will put values of the two points in straight line equation, and we get
y = mx + a
q2 = mp2 …(1)
q1 = mp1 + a …(2)
The Digital Differential Analyzer helps us to interpolate the variables on an interval from one point to another point.
We can use the digital Differential Analyzer algorithm to perform rasterization on polygons, lines, and triangles.
Digital Differential Analyzer algorithm is also known as an incremental method of scan conversion. In this
algorithm, we can perform the calculation in a step by step manner. We use the previous step result in the next step.
Now, we consider one point (xk, yk) and (xk+1, yk+1) as the next point.
Then the slope m = (yk+1 – yk)/ (xk+1 – xk)
Now, we have to find the slope between the starting point and ending point. There can be following three cases to
discuss:
Case 1: If m < 1
Then x coordinate tends to the Unit interval.
xk+1 = xk + 1
yk+1 = yk + m
Case 2: If m > 1
Then y coordinate tends to the Unit interval.
yk+1 = yk + 1
xk+1 = xk + 1/m
Case 3: If m = 1
Then x and y coordinate tend to the Unit interval.
xk+1 = xk + 1
yk+1 = yk + 1
We can calculate all intermediate points with the help of above three discussed cases.
Step 1: Start.
Step 2: We consider Starting point as (x1, y1), and endingpoint (x2, y2).
Step 3: Now, we have to calculate ▲x and ▲y.
▲x = x2-x1
▲y = y2-y1
m = ▲y/▲x
Step 4: Now, we calculate three cases.
If m < 1
Then x change in Unit Interval
y moves with deviation
(xk+1, yk+1) = (xk+1, yk+1)
If m > 1
Then x moves with deviation
y change in Unit Interval
(xk+1, yk+1) = (xk+1/m, yk+1/m)
If m = 1
Then x moves in Unit Interval
y moves in Unit Interval
(xk+1, yk+1) = (xk+1, yk+1)
Step 5: We will repeat step 4 until we find the ending point of the line.
Step 6: Stop.
Example: A line has a starting point (1,7) and ending point (11,17). Apply the Digital Differential Analyzer algorithm
to plot a line.
Step 4: We will repeat step 3 until we get the endpoints of the line.
Step 5: Stop.
This algorithm was introduced by “Jack Elton Bresenham” in 1962. This algorithm helps us to perform scan
conversion of a line. It is a powerful, useful, and accurate method. We use incremental integer calculations to draw a
line. The integer calculations include addition, subtraction, and multiplication.
In Bresenham’s Line Drawing algorithm, we have to calculate the slope (m) between the starting point and the ending
point.
As shown in the above figure let, we have initial coordinates of a line = (xk, yk). The next coordinates of a line = (xk+1,
yk+1). The intersection point between yk and yk+1 = y. Let we assume that the distance between y and yk = d1. The
distance between y and yk+1 = d2. Now, we have to decide which point is nearest to the intersection point.
Step 1: Start.
Step 2: Now, we consider Starting point as (x1, y1) and ending point (x2, y2).
Step 3: Now, we have to calculate ▲x and ▲y.
▲x = x2-x1
▲y = y2-y1
m = ▲y/▲x
Step 4: Now, we will calculate the decision parameter pk with following formula.
pk = 2▲y-▲x
Step 5: The initial coordinates of the line are (xk, yk), and the next coordinates are (xk+1, yk+1). Now, we are going to
calculate two cases for decision parameter pk
Case 1: If
pk < 0
Then
pk+1 =pk +2▲y
xk+1 = xk +1
yk+1 = yk
Case 2: If
pk >= 0
Then
pk+1 =pk +2▲y-2▲x
xk+1 =xk +1
yk+1 =yk +1
Step 6: We will repeat step 5 until we found the ending point of the line and the total number of iterations =▲x-1.
Step 7: Stop.
Example: A line has a starting point (9,18) and ending point (14,22). Apply the Bresenham’s Line Drawing algorithm
to plot a line.
Thus
pk+1 = pk +2▲y-2▲x =3+ (2 x 4) – (2 x 5) = 1
xk+1 =xk +1 = 9 + 1 = 10
yk+1 =yk +1 = 18 +1 = 19
Step 4: Now move to next step. We will calculate the coordinates until we reach the end point of the line.
▲x -1 = 5 – 1 = 4
9 18
3 1 10 19
1 -1 11 20
-1 7 12 20
7 5 13 21
5 3 14 22
Step 5: Stop.
The Bresenham’s Line drawing algorithm only helps to draw the basic line.
The resulted draw line is not smooth.
Bresenham’s Algorithm
We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest pixel
position to complete the arc.
From the following illustration, you can see that we have put the pixel at X,YX,Y location and now need
to decide where to put the next pixel − at N X+1,YX+1,Y or at S X+1,Y−1X+1,Y−1.
Find the next point of the first octant depending on the value of decision parameter Pk.
Step-04:
If the given centre point (X0, Y0) is not (0, 0), then do the following and plot the point-
Xplot = Xc + X0
Yplot = Yc + Y0
To find the points for other seven octants, follow the eight symmetry property of circle.
Line Attributes:
Basic attributes of a straight line are its type, its width, and its colour. In some graphics packages, line
can also be displayed using selected pen or brush options.
1. Line Type: The line type attribute includes solid lines, dashed lines, and dotted lines. We
modify the line drawing algorithm to generate such lines by setting the length and space. A
dashed line could be displayed by generating spaces that is equal to length of solid part. A
dotted line can be displayed by generating very short dashes with spacing equal to or greater
than the dash size. Similar methods are used to produce other line-type variations.
Raster line-algorithms displays line type attribute by plotting pixels. For various dashed,
dotted patterns, the line-drawing algorithms outputs part of pixels followed by spaces. Plotting
dashes with a fixed number of pixels results in unequal-length dashes for different line angles.
For example, the length of dash diagonally is more than horizontal dash for same number of
pixels. For precision drawings, dash length should remain approximately same for any line
angle. For this, we can adjust the pixel number according to line slope.
2. Line Width: A line with more width can be displayed as parallel lines on a video monitor.
In raster lines, a standard width line is generated with single pixels at each point. Width lines
are displayed by plotting additional pixels along next parallel line paths. For lines with slope
less than 1, we can display thick lines by plotting a vertical length of pixels at each x position
along the line. Similarly, for lines with slope greater than 1, we can plot thick lines with
horizontal widths for each point.
The problem with implementing width options using horizontal and vertical pixel widths is
that the width of line is depended on the slope. A 45-degree line will be displayed thinner as
compared to vertical or horizontal line plotted with same number of pixel widths.
Another problem is that it produces lines whose ends are either horizontal or vertical. We
can adjust the shape of the line ends by adding Line Caps.
One kind of line cap is the Butt Cap. It is obtained by adjusting the end positions of lines so
that the thick line is displayed with square ends that are perpendicular to the line. Another line
cap is the Round Cap obtained by adding a filled semicircle to each butt cap. The semicircle
has diameter equal to thickness of line. The third type of line cap is the Projecting Square Cap.
Here, the butt cap is extended to half of line width.
Thick Lines drawn with (a) Butt Cap (b) Round Cap and (c) Projecting Square Cap
Generating thick connected line segments require other considerations. The methods that we
have considered for displaying thick lines will not produce smoothly connected line segments.
It leaves gaps at the boundaries between lines of different slope. There are three possible
methods for smoothly joining two line segments. A Miter Join is obtained by extending the
outer boundaries of each of the two lines until they meet. A Round Join is produced by covering
the connection between the two segments with a circular boundary whose diameter is equal to
the line width. A Bevel Join is generated by displaying the line with butt caps and filling in
the triangular gap where the segments meet.
Line without Joins Bevel Join
3. Pen and Brush Options: In some graphic packages, lines can be displayed with pen or
brush selection. Options in this category include shape, size and pattern. These shapes are
stored in a Pixel Mask that identifies the pixel positions that are to be set along the line path.
Lines generated with pen or brush shaped can be displayed in various widths by changing the
size of the mask. Lines can also be displayed with selected patterns.
4. Line Colour: A system displays a line in the current colour by setting the colour value in
the frame buffer at pixel locations. The number of colour choices depends on the number of
bits available per pixel in the frame buffer. A line drawn in the background colour is invisible.
COLOR AND GRAYSCALE LEVELS
Various color and intensity-level options can be made available to a user, depending on the capabilities
and design objectives of a particular system. General purpose raster-scan systems, for example, usually
provide a wide range of colors, while random-scan monitors typically offer only a few color choices, if
any. Color options are numerically coded with values ranging from 0 through the positive integers. For
CRT monitors, these color codes are then converted to intensity level settings for the electron beams. With
color plotters, the codes could control ink-jet deposits or pen selections.
In a color raster system, the number of color choices available depends on the amount of storage provided
per pixel in the frame buffer. Also, color-information can be stored in the frame buffer in two ways: We
can store color codes directly in the frame buffer, or we can put the color codes in a separate table and use
pixel values as an index into this table. With the direct storage scheme, whenever a particular color code
is specified in an application program, the corresponding binary value is placed in the frame buffer for
each-component pixel in the output primitives to be displayed in that color. A minimum number of colors
can be provided in the scheme with 3 bits of storage per pixel, as shown in Table. Each of the three bit
positions is used to control the intensity level (either on or off) of the corresponding electron gun in an
RGB monitor. The leftmost bit controls the red gun, the middle bit controls the green gun, and the
rightmost bit controls the blue gun. Adding more bits per pixel to the frame buffer increases the number
of color choices. With 6 bits per pixel, 2 bits can be used for each gun. This allows four different intensity
settings for each of the three color guns, and a total of 64 color values are available for each screen pixel.
With a resolution of 1024 by 1024, a full-color (24bit per pixel) RGB system needs 3 megabytes of storage
for the frame buffer. Color tables are an alternate means for providing extended color capabilities to a user
without requiring large frame buffers. Lower-cost personal computer systems, in particular, often use color
tables to reduce frame-buffer storage requirements.
COLOR TABLES
Figure 2.1 illustrates a possible scheme for storing color values in a color lookup table (or video lookup
table), where frame-buffer values are now used as indices into the color table. In this example, each pixel
can reference any one of the 256 table positions, and each entry in the table uses 24 bits to specify an RGB
color. For the color code 2081, a combination green-blue color is displayed for pixel location (x, y).
Systems employing this particular lookup table would allow a user to select any 256 colors for
simultaneous display from a palette of nearly 17 million colors. Compared to a full color system, this
scheme reduces the number of simultaneous colors that can be displayed,
but it also reduces the frame buffer storage requirements to 1 megabyte. Some graphics systems provide
9 bits per pixel in the frame buffer, permitting a user to select 512 colors that could be used in each display.
(Table 2.1, The eight color codes for a three-bit per pixel frame buffer)
(Fig: 2:1, Color Lookup Table)
There are several advantages in storing color codes in a lookup table. Use of a color table can provide a
"reasonable" number of simultaneous colors without requiring large frame buffers. For most applications,
256 or 512 different colors are sufficient for a single picture.
GRAYSCALE
With monitors that have no color capability, color functions can be used in an application program to set
the shades of gray, or grayscale, for displayed primitives. Numeric values over the range from 0 to 1 can
be used to specify grayscale levels, which are then converted to appropriate binary codes for storage in the
raster. This allows the intensity settings to be easily adapted to systems with differing grayscale
capabilities.
Table lists the specifications for intensity codes for a four-level grayscale system. In this example, any
intensity input value near 0.33 would be stored as the binary value 01 in the frame buffer, and pixels with
this value would be displayed as dark gray. If additional bits per pixel are available in the frame buffer,
the value of 0.33 would be mapped to the nearest level. With 3 bits per pixel, we can accommodate 8 gray
levels; while 8 bits per pixel would give us 256 shades of gray. An alternative scheme for storing the
intensity information is to convert each intensity code directly to the voltage value that produces this
grayscale level on the output device in use.
INTENSITY CODES FOR A FOUR-LEVEL GRAYSCALE SYSTEM
Area-Fill Attributes:
Options for filling a region include a choice between a solid colour and a patterned fill. These options
can be applied to polygon regions or regions with curved boundaries.
Areas can be displayed with various fill styles: hollow, solid, pattern and hatch.
Hollow areas are displayed using only the boundary outline, with interior colour the same as the
background colour.
A Solid fill is displayed in a single colour including the borders. Fill style can also be with a
specific pattern or design. The Hatch fill is used to fill area with hatching pattern.
Other fill options include specifications for the edge type, edge width and edge colour. These
attributes are same as the line attributes. That is, we can display polygon edges as dotted,
dashed, thick or of different colours, etc.
Soft Fill
Character Attributes:
The appearance of displayed characters is controlled by attributes such as font, size, colour and
orientation. Attributes can be set both for entire character strings and for individual
characters, known as Marker symbols.
1. Text Attributes: There are many text options available, such as font, colour, size, spacing,
and orientation.
Text Style: The characters in a selected font can also be displayed in various underlining
styles (solid, dotted, dashed, double), in bold, in italics, shadow style, etc. Font options
can be made available as predefined sets of grid patterns or as character sets designed with
lines and curves.
Text Colour: Colour settings for displayed text are stored in the system attribute list and
transferred to the frame buffer by character loading functions. When a character string is
displayed, the current colour is used to set pixel values in the frame buffer corresponding
to the character shapes and position.
Text Size: We can adjust text size by changing the overall dimensions, i.e., width and
height, of characters or by changing only the width. Character size is specified in Points,
where 1 point is 0.013837 inch or approximately 1/72 inch. Point measurements specify the
size of the Character Body. Different fonts with the same point specifications can have
different character sizes depending upon the design of the font.
Topline Kern
q
Capline
H Character
B
seline
Bottomline
f Descenders
Character Body
The distance between Topline and Bottomline is same for all characters in a particular size
and font, but the width may be different. A smaller body width is assigned to narrow
characters such as i, j, l, e t c . compared to broad characters such as W or M. The Character
Height is the distance between the Baseline and Capline of characters. Kerned characters,
such as f and j, extend beyond the character-width limits. And letters with descenders, such
as g, j, p, q, extend below the baseline.
The size can be changed in such a way so that the width and spacing of characters is adjusted
to maintain the same text proportions. For example, doubling the height also doubles the
character width and the spacing between characters. Also, only the width of the character s
can be changes without affecting its height. Similarly, spacing between characters can be
increased without changing height or width of individual characters.
The effect of different character-height setting, character-width setting, and character
spacing on a text is shown below.
Text Orientation: The text can be displayed at various angles, known as orientation. A
procedure for orienting text rotates characters so that the sides of character bodies, from
baseline to topline at aligned at some angle. Character strings can be arranged vertically or
horizontally.
Orientation
Text Path: In some applications the character strings are arranged vertically or
horizontally. This is known as Text Path. Text path can be right, left, up or down.
COMPUTER GRAPHICS
g
n
i
r
t
s
g
Text Alignment: Another attribute for character strings is alignment. This attribute
specifies how text is to be positioned with respect to the start coordinates. Vertical
alignment can be top, cap, half, base and bottom. Similarly, horizontal alignment can be
left, centre and right.
top
cap
TEXT ALIGNMENT half
bottom
right
Text Attributes
In text we are having so many style and design like italic fonts, bold fonts etc.
For setting the font style in PHIGS package we have one function which is:
setTextFont (tf)
Where tf is used to specify text font
It will set specified font as a current character.
For setting color of character in PHIGS we have function:
setTextColorIndex (tc)
Where text color parameter tc specifies an allowable color code.
For setting the size of the text we use function.
setCharacterheight (ch)
For scaling the character we use function.
COMPUTER GRAPHICS
setCharacterExpansionFacter (cw)
Where character width parameter cw is set to a positive real number that scale the character body
width.
Spacing between character is controlled by function
setCharacterSpacing (cs)
Where character spacing parameter cs can be assigned any real value.
The orientation for a displayed character string is set according to the direction of the character up
vector:
setCharacterUpVector (upvect)
Parameter upvect in this function is assigned two values that specify the 𝑥 and 𝑦 vector components
Text is then displayed so that the orientation of characters from baseline to cap line is in the direction of
the up vector.
For setting the path of the character we use function:
setTextPath (tp)
Where the text path parameter tp can be assigned the value: right, left, up, or down.
It will set the direction in which we are writing.
For setting the alignment of the text we use function.
setTextAlignment (h, v)
Where parameter h and v control horizontal and vertical alignment respectively.
For specifying precision for text display is given with function.
setTextPrecision (tpr)
Where text precision parameter tpr is assigned one of the values: string, char, or stroke.
The highest-quality text is produced when the parameter is set to the value stroke.
Marker Attributes
A marker symbol display single character in different color and in different sizes.
For marker attributes implementation by procedure that load the chosen character into the raster at
defined position with the specified color and size.
We select marker type using function.
setMarkerType (mt)
Where marker type parameter mt is set to an integer code.
Typical codes for marker type are the integers 1 through 5, specifying, respectively, a dot (.), a
vertical cross (+), an asterisk (*), a circle (o), and a diagonal cross (x). Displayed marker types are
centered on the marker coordinates.
We set the marker size with function.
SetMarkerSizeScaleFactor (ms)
Where parameter marker size ms assigned a positive number according to need for scaling.
For setting marker color we use function.
setPolymarkerColorIndex (mc)
Where parameter mc specify the color of the marker symbol
Bundled Attributes
A particular set of attribute values tor a primitive on each output device is then chosen by specifying the
appropriate table index. Attributes specified in this manner called bundled attributes
COMPUTER GRAPHICS