













































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Points and Lines 2 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
Typology: Assignments
1 / 53
This page cannot be seen from the preview
Don't miss anything!














































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.
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 𝑔𝑒𝑡𝑝𝑖𝑥𝑒𝑙(𝑥, 𝑦).
5
Lines are a very common primitive and will be supported by almost all graphics packages. In addition, lines form the basis of more complex primitives such as polylines (a connected sequence of straight-line segments) or polygons (2-D objects formed by straight-line edges). Lines are normally represented by the two end-points of the line, and points (x,y) along the line must satisfy the following slope-intercept equation:
y = mx + c ……………………………………………… (1)
where m is the slope or gradient of the line, and c is the coordinate at which the line intercepts the y -axis. Given two end-points (x 0 ,y 0 ) and (xend,yend) , we can calculate values for m and c as follows:
0
end
end
Furthermore, for any given x -interval δx , we can calculate the corresponding y -interval δy :
δy = m.δx …………………………………………….. (4) δx = (1/m).δy …………………………………………… (5)
These equations form the basis of the two line-drawing algorithms described below: the DDA algorithm and Bresenham’s algorithm.
The solution to this problem is to make sure that both δx and δy have values less than or equal to one. To ensure this, we must first check the size of the line gradient. The conditions are: If | m | ≤ 1: o δx = 1 o δy = m If | m | > 1: o δx = 1/m o δy = 1 Once we have computed values for δx and δy , the basic DDA algorithm is: Start with (x 0 ,y 0 ) Find successive pixel positions by adding on (δx, δy) and rounding to the nearest integer, i.e. o xk+1 = xk + δx o yk+1 = yk + δy For each position (xk,yk) computed, plot a line point at (round(xk),round(yk)) , where the round function will round to the nearest integer.
Note that the actual pixel value used will be calculated by rounding to the nearest integer, but we keep the real-valued location for calculating the next pixel position.
Let us consider an example of applying the DDA algorithm for drawing a straight-line segment. Referring to see Fig 2.4, we first compute a value for the gradient m :
3 ( 15 10 )
( 13 10 )
0
(^0)
x x
y y m end
end
Now, because |m| ≤ 1, we compute δx and δy as follows:
δx = 1 δy = 0.
Using these values of δx and δy we can now start to plot line points: Start with (x 0 ,y 0 ) = (10,10) – colour this pixel Next, (x 1 ,y 1 ) = (10+1,10+0.6) = (11,10.6) – so we colour pixel (11,11) Next, (x 2 ,y 2 ) = (11+1,10.6+0.6) = (12,11.2) – so we colour pixel (12,11) Next, (x 3 ,y 3 ) = (12+1,11.2+0.6) = (13,11.8) – so we colour pixel (13,12) Next, (x 4 ,y 4 ) = (13+1,11.8+0.6) = (14,12.4) – so we colour pixel (14,12) Next, (x 5 ,y 5 ) = (14+1,12.4+0.6) = (15,13) – so we colour pixel (15,13) We have now reached the end-point (xend,yend) , so the algorithm terminates
Bresenham’s line-drawing algorithm provides significant improvements in efficiency over the DDA algorithm. These improvements arise from the observation that for any given line, if we know the previous pixel location, we only have a choice of 2 locations for the next pixel. This concept is illustrated in Fig 2.5 given that we know (xk,yk) is a point on the line, we know the next line point must be either pixel A or pixel B. Therefore we do not need to compute the actual floating-point location of the ‘true’ line point; we need only make a decision between pixels A and B.
Figure 2.5- Bresenham's Line-Drawing Algorithm
Bresenham’s algorithm works as follows. First, we denote by dupper and dlower the distances between the centres of pixels A and B and the ‘true’ line (see Fig 2.5). Using Eq. (1) the ‘true’ y -coordinate at xk+1 can be calculated as:
Therefore we compute dlower and dupper as:
d (^) upper ( yk 1 ) y yk 1 m ( xk 1 ) c
Now, we can decide which of pixels A and B to choose based on comparing the values of dupper and dlower : If dlower > dupper , choose pixel A Otherwise choose pixel B
We make this decision by first subtracting dupper from dlower :
Always xk+1 = xk+ 1 If pk < 0, then yk+1 = yk , otherwise yk+1 = yk+ 1
Therefore we can define the incremental calculation as:
p (^) k 1 pk 2 y
p (^) k 1 pk 2 y 2 x
if pk < 0 …………………………………………… (11)
if pk ≥ 0 …………………………………………… (12)
The initial value for the decision variable, p 0 , is calculated by substituting xk = x 0 and yk = y 0 into Eq. (10), which gives the following simple expression:
p (^) 0 2 y x^ ……………………………………………………………^ (13)
So we can see that we never need to compute the value of the constant d in Eq. (10).
Fig. 2.7: - Circle with center coordinates (𝑥𝑐, 𝑦𝑐) and radius 𝑟.
A circle is defined as the set of points that are all at a given distance r from a center position say (𝑥𝑐, 𝑦𝑐). Properties of Circle
The distance relationship is expressed by the Pythagorean theorem in Cartesian coordinates as: (𝑥 − 𝑥𝑐)^2 + (𝑦 − 𝑦𝑐)^2 = 𝑟^2 We could use this equation to calculate circular boundary points by incrementing 1 in 𝑥 direction in every steps from 𝑥𝑐 – 𝑟 to 𝑥𝑐 + 𝑟 and calculate corresponding 𝑦 values at each position as:
y = 𝑦𝑐 ± 𝑟^2 −(𝑥𝑐− 𝑥)
But this is not best method for generating a circle because it requires more number of calculations which take more time to execute. And also spacing between the plotted pixel positions is not uniform as shown in figure below.
Fig. 2.8: - Positive half of circle showing non uniform spacing bet calculated pixel positions.
We can take one step further and note that there is also symmetry between octants. Circle sections in adjacent octant within one quadrant are symmetric with respect to the 45^0 line dividing the two octants. This symmetry condition is shown in figure below where point (𝑥, 𝑦) on one circle sector is mapped in other seven sector of circle.
Fig. 2.9: - symmetry of circle.
Taking advantage of this symmetry property of circle we can generate all pixel position on boundary of circle by calculating only one sector from 𝑥 = 0 to 𝑥 = 𝑦. Determining pixel position along circumference of circle using any of two equations shown above still required large computation. More efficient circle algorithm are based on incremental calculation of decision parameters, as in the Bresenham line algorithm. Bresenham’s line algorithm can be adapted to circle generation by setting decision parameter for finding closest pixel to the circumference at each sampling step. The Cartesian coordinate circle equation is nonlinear so that square root evaluations would be required to compute pixel distance from circular path. Bresenham’s circle algorithm avoids these square root calculation by comparing the square of the pixel separation distance. A method for direct distance comparison to test the midpoint between two pixels to determine if this midpoint is inside or outside the circle boundary. This method is easily applied to other conics also. Midpoint approach generates same pixel position as generated by bresenham’s circle algorithm. The error involve in locating pixel positions along any conic section using midpoint test is limited to one- half the pixel separation.