CG1
CG1
CG1
(VIth sem )
Viral B. Polishwala
2.1
Geometry
By now we know that the smallest element is a point and that for a raster device, it is pixel. Pixel is the smallest addressable element of raster device. It is derived from the Picture Element. The number of pixel of a display device determines the resolution of the device, and therefore higher number of pixels is preferred for graphics applications. Each of these pixels is associated with the coordinate of Cartesian plane. These coordinates identify pixel or point. To draw any image or object, some of pixels are activated by setting some intensity. In other words, the pixels turned on. For example, to draw a straight line segment the adjacent pixels between two points are turned on.
Y
Point AS in the figure Point A has been positioned in the XY-plane and (x1, y1) is the position of point A. It implies that the A is located at x1 distance in X direction and at y1 distance in Y direction X (0,0) Fig:2.1 - A Point in XY-Plane from the origin (0,0). Where the (x1,y1) is known as the coordinates of point A. Line
A(x1,y1)
Any two points specified in the Plane will define a line, which means that for Y defining the line we must have to specify two points. l In the figure 2.2, two points P1(x1,y1) and P2(x2,y2)define a P1(x1,y1) line l1. To define the line we need an equation. A random point P(xi,yi) is said to be on the line l1 if and only if it satisfies the equation of the line. The equation of a straight line can be derived P2(x2,y2) with the help of slope concept of a straight line. The slope is the X rate at which an ordinate of a point on a plane changes with (0,0) Fig:2.2 - A Straight Line respect to a change in the horizontal coordinate. The notion slope is denoted by m. Thus,
1
So the slope of any line can be one of four types: the straight line 1) with positive slope, 2) with negative slope, 3) with zero slope and 4) with undefined slope.
(3,2) (-1,-2) 1) Slope m =1 2) (-3,2) (-3,2) (3,2) 2,3)
(2,-3) Slope m = -1
Viral B. Polishwala
As per previous discussion to derive a line equation the concept of slope is being used. The slope of any line between two points will remain unchanged (constant). According to this Let assume that P1(x1,y1) and P2(x2,y2) constitute a line as well as a point P(x,y) is lying on the same line and the slope remain same at any point of the slope. So the slope can be written as follows: 2 = 2 2 = 2 We can assume that b = 1
+ .(1) => 1=
1 = 1
( 1)
=>
2 1 2 1
=
=
1+ 1
=>
2 1 2 1
+( 1
1)
Viral B. Polishwala
Step: 1 If the slope is less than or equal to 1, the unit x intervals dx=1 and compute each successive y values. dx=1 m = dy / dx m = ( y2 - y1 ) / 1 m = ( yk + 1 yk ) /1 yk+1 = yk + m ------ (2) Subscript k takes integer values starting from 1,for the first point and increment by 1 until the final end point is reached. o m -> any real numbers between 0 and 1 o Calculate y values must be rounded to the nearest integer Step: 2 If the slope is greater than 1, the roles of x any y at the unit y intervals dy=1 and compute each successive y values. dy=1 m= dy / dx m= 1/ ( x2-x1 ) m = 1 / ( xk+1 xk ) xk+1 = xk + ( 1 / m ) ------( 3 ) Equation 2 and Equation 3 that the lines are to be processed from left end point to the right end point. Step: 3 If the processing is reversed, the starting point at the right dx=-1 m= dy / dx m = ( y2 y1 ) / -1 yk+1 = yk - m -------- ( 4 )
o
Intervals dy=1 and compute each successive y values. Step: 4 Here, dy=-1 m= dy / dx m = -1 / ( x2 x1 ) m = -1 / ( xk+1 xk ) xk+1 = xk + ( 1 / m ) -------- ( 5 ) Equation 2 and Equation 5 used to calculate pixel position along a line with ve slope. This algorithm can also be written in following form: Step-1: Input two line end points A(xa,ya) and B(xb,yb) and start with the left endpoint A(xa,ya) and declare x, y, step, k, xinc, yinc, x, y. Step-2: Calculate x = xb - xa ; y = yb - ya [find the difference of end points] Step-3: If abs(x) > abs(y) then step = abs(x) else step = abs(y) Step-4: We have xinc = x / steps and yinc = y / steps [Find total intermediate points] Step-5: setPixel(round(xa), round(xa), 1) [Plot the first point]
Viral B. Polishwala
Step-6: x = x + xinc; y = y + yinc ; setPixel(round(x), round(y),1) [Plot all points] Step-7: Repeat step-6 for step times
Advantage:
1) It is a faster method for calculating pixel position then simple line algorithm. 2) It eliminates continues multiplication in equation Y=mx+b so that appropriate increments are applied on x or y.
Disadvantage:
1) Round off error in successive additions can cause the pixel positions to drift away from the true line path for long line segments. 2) The rounding operations and floating point arithmetic are still time consuming.
Let us assume that we are drawing a line y = mx+b that passes through a point (x0,y0). Here 0 < m < 1. Let us also assume that the last pixel turned on is (xk,yk) and the decision to be made is for the next step that is for the vertical distance xk+1 as in fig:2.5.
Y
yk +1 y yk
d2
d1
Now let us assume a vertical row of pixels which passes through horizontal distance xk+1. There are three vertical points, (xk+1, yk), (xk+1, y) and (xk+1, yk+1), fall on this assumed line. in addition the assumed distance between (xk+1, yk) and (xk+1, y) is d1 and distance between (xk+1, y) and (xk+1, yk+1) is d2 in above figure. The equation of this vertical line is, y= m(xk+1) + b d1 = y yk = m(xk+1) + b yk d2 = yk +1 - y
Therefore,
Viral B. Polishwala
= yk +1 - (m(xk+1) + b) = yk +1 - m(xk+1) - b The difference among these two points can be obtained as d1 - d2, d1 - d2 = [m(xk+1) + b yk] [yk +1 - m(xk+1) b] = m(xk)+ m + b yk yk 1 + m(xk) + m + b = 2m( xk) +2m + 2b - 2yk - 1 = 2m(xk + 1) + 2b - 2yk - 1 So, d1 - d2 = 2m(xk + 1) + 2b - 2yk 1 dx (d1 - d2) = 2 dy (xk + 1) + 2b dx - 2yk dx 1 dx =2 (xk + 1) + 2b - 2yk 1 Pk = 2 dy xk - 2yk dx + c = 2 dy xk + 2 dy + 2b dx - 2yk dx dx [after substituting m = ]
Where c = 2 dy + 2b dx dx and it is independent of the positions of pixel, and P k is a decision parameter for the kth step, ..[2]
The sign of decision parameter is same as the sign of d1 - d2, as for our case 0 < m <1, that is dx > 0. If Pk is positive, that is dx(d1 - d2) > 0, and d1 > d2. Therefore, the upper pixel at position yk + 1 is closer to the line than the pixel yk, hence the pixel at position yk + 1 will be activated. In case of negative Pk, that is dx(d1 - d2) < 0, (d2 > d1) and therefore the pixel at position yk will be activated. Now for the (k + 1)st step, Pk+1 - Pk = [2 dy xk+1 - 2yk+1 dx + c] [2 dy xk - 2yk dx + c] = 2 dy xk+1 - 2yk+1 dx + c 2 dy xk + 2yk dx c = 2 dy (xk+1 - xk) - 2dx (yk+1 - 2yk) = 2 dy - 2dx (yk+1 - 2yk) [since xk+1 - xk = 1] Pk+1 - Pk = 2 dy - 2dx (yk+1 - 2yk) Pk+1 = 2 dy xk+1 - 2yk+1 dx + c
Where the value of (yk+1 - yk) will be 0 or 1 respectively for positive and negative value of decision parameter Pk. From equation [2], the initial value of the decision parameter is given as: Pk = 2 (dy dx) ..(3)
Here, it is noticeable that for making a decision regarding the pixel to be activated, no floating point operation is needed. Other integer coordinate values of the next point in line can be obtained by the following algorithm and so the line is created.
Bresenham's Line-Drawing Algorithm for |m| < 1
Step 1: Input the two line endpoints and store the left endpoint in (x0,y0)
Viral B. Polishwala
Step 2: Load (x0,y0) into the frame buffer; that is, plot the first point. Step 3: Calculate constants dx, dy, 2dy, and 2dy 2dx, and obtain the starting value for the decision parameter as P0 = 2dy - dx Step 4: At each xk along the line, starting at k = 0, perform the following test: If Pk < 0, the next point to plot is (xk +1, yk) and Pk +1 = Pk + 2dy Otherwise, the next point to plot is (xk +1, yk +1) Pk +1 = Pk + 2dy - 2dx Step 5: Repeat step 4 dx no. of times.
Let p1(x1,y1) and p2(x2,y2) be the end points of a line segment with width , which is to be drawn. Here two parallel line segments will be drawn from a central line(x1,y1) and (x2,y2) at a distance( /2).
Viral B. Polishwala
As shown in fig:, the coordinates of end points of the upper and lower line boundaries are [(x1,y1 + y), (x2,y2 + y)] and [(x1,y1 - y), (x2,y2 - y)] respectively. Where the y is as below:
y
In the above equation, w-1 is taken instead of w. the reason being, on both sides the line boundaries are drawn which are of 1 pixel in length. Therefore, from both the sides half of the pixel width will be deducted to get the exact width w. for a line segment with steep slope, that is slope greater than 1, the role of x and y in equation () will be interchanged. Thus, for steep slope, the value of w y will be
y
In the above equation, 1 is taken instead of . The reason is we are considering the boundaries which are drawn on both the sides are of 1 pixel in length. So from both side 1 deducted to get its original width . When the slope is steep that is greater than 1, the role of x and y in the above equation will get change and the equation would be written as follows:
x
(4)
. (5)
(c)
Butt Cap
(b)
Round Cap
(a)
In the butt cap the 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. In the round cap, the upper and lower line boundaries are joined by a circular arch with a semi-circle of the diameter of the width of the thick line segment. In the projecting square cap, the portion of line segment is just extended to give a line segment an effect of square.
Viral B. Polishwala
The discreteness that occurs while joining two thick lines can be removed from the poly-lines by adding some additional portion to the joint as some specific types of joining process. There are three joints: Miter Joint, Round Joint, Bevel Fig.2.10 Thick line Joints Joint. The miter joint is one in which the exterior boundaries of two thick line segments are extended up to their intersection point the point where these two line segments meet each other, if they are extended. (See figure 2.11(a)) In the round joint, the exterior boundaries are connected with a circular arch with a diameter of total thickness of two thick lines, which are joined. (See figure 2.11(b)) The bevel joint is one where butt cap is used to draw line segment and the little triangle that is created at the joint is filled with color of these two line segments. (See figure Fig.2.11 Constructs of Thick line Joints 2.11(c))