Chapter Four
Chapter Four
Sometimes we are interested in some portion of the object and not in full object. So
we will decide on an imaginary box. This box will enclose desired or interested area
of the object. Such an imaginary box is called a window.
Basically, the window is an area in object space. It encloses the object. After the user
selects this, space is mapped on the whole area of the viewport. Almost all 2D and
3D graphics packages provide means of defining viewport size on the screen. It is
possible to determine many viewports on different areas of display and view the
same object in a different angle in each viewport.
The size of the window is (0, 0) coordinate which is a bottom-left corner and toward
right side until window encloses the desired area. Once the window is defined data
outside the window is clipped before representing to screen coordinates. This
process reduces the amount of data displaying signals.
The window size of the Tektronix 4.14 tube in Imperial College contains 4.96 points
horizontally and 3072 points vertically.
First, we construct the scene in world coordinate using the output primitives and
attributes.
Once the viewing frame is established, are then transform description in world
coordinates to viewing coordinates.
Then, we define viewport in normalized coordinates (range from 0 to 1) and map the
viewing coordinates description of the scene to normalized coordinates.
At the final step, all parts of the picture that (i.e., outside the viewport are dipped,
and the contents are transferred to device coordinates).
By varying the size of viewports: We can change the size and proportions of
displayed objects. We can achieve zooming effects by successively mapping different-
sized windows on a fixed-size viewport.
As the windows are made smaller, we zoom in on some part of a scene to view details
that are not shown with larger windows.
Once object description has been transmitted to the viewing reference frame, we
choose the window extends in viewing coordinates and selects the viewport limits in
normalized coordinates.
We do this thing using a transformation that maintains the same relative placement
of an object in normalized space as they had in viewing coordinates.
If a coordinate position is at the center of the viewing window:
Fig shows the window to viewport mapping. A point at position (xw, yw) in window
mapped into position (xv, yv) in the associated viewport.
In order to maintain the same relative placement of the point in the viewport as in
the window, we require:
Solving these impressions for the viewport position (xv, yv), we have
xv=xvmin+(xw-xwmin)sx
yv=yvmin+(yw-ywmin)sy ...........equation 2
Equation (1) and Equation (2) can also be derived with a set of transformation that
converts the window or world coordinate area into the viewport or screen coordinate
area. This conversation is performed with the following sequence of transformations:
From normalized coordinates, object descriptions are mapped to the various display
devices.
Any number of output devices can we open in a particular app, and three windows
to viewport transformation can be performed for each open output device.
Viewing Transformation= T * S * T1
We can display picture at device or display system according to our need and choice.
Note:
Each pixel value is used four times twice on each of the two successive scan lines.
o Ordered dither.
o Random dither.
There are widely used, especially when the grey levels (share of brightness) are
synthetically generated.
The process of panning acts as a qualifier to the zooming transformation. This step
moves the scaled up portion of the image to the center of the screen and depending
on the scale factor, fill up the entire screen.
Advantage:
Effective increase in zoom area in all four direction even if the selected image portion
(for zooming) is close to the screen boundary.
Inking:
If we sample the position of a graphical input device at regular intervals and display
a dot at each sampled position, a trial will be displayed of the movement of the
device.This technique which closely simulates the effect of drawing on paper is called
Inking.
For many years the primary use of inking has been in conjunction with online
character-recognition programs.
Scissoring:
In computer graphics, the deleting of any parts of an image which falls outside of a
window that has been sized and laid the original vision ever. It is also called the
clipping.
Clipping:
When we have to display a large portion of the picture, then not only scaling &
translation is necessary, the visible part of picture is also identified. This process is
not easy. Certain parts of the image are inside, while others are partially inside. The
lines or elements which are partially visible will be omitted.
For deciding the visible and invisible portion, a particular process called clipping is
used. Clipping determines each element into the visible and invisible portion. Visible
portion is selected. An invisible portion is discarded.
Types of Lines:
Applications of clipping:
Clipping can be applied to world co-ordinates. The contents inside the window will
be mapped to device co-ordinates. Another alternative is a complete world co-
ordinates picture is assigned to device co-ordinates, and then clipping of viewport
boundaries is done.
Types of Clipping:
1. Point Clipping
2. Line Clipping
3. Area Clipping (Polygon)
4. Curve Clipping
5. Text Clipping
6. Exterior Clipping
Point Clipping:
Point Clipping is used to determining, whether the point is inside the window or not.
For this following conditions are checked.
1. x ≤ xmax
2. x ≥ xmin
3. y ≤ ymax
4. y ≥ ymin
The (x, y) is coordinate of the point. If anyone from the above inequalities is false,
then the point will fall outside the window and will not be considered to be visible.
Program1:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<graphics.h>
4. inttlx,tly,brx,bry,px,py;
5. void point_clip()
6. {
7. intwxmin,wymin,wxmax,wymax;
8. wxmin=tlx;
9. wxmax=brx;
10. wymin=tly;
11. wymax=bry;
12. if(px>=wxmin&&px<=wxmax)
13. if(py>=wymin&&py<=wymax)
14. putpixel(px,py,RED);
15. getch();
16. closegraph();
17. }
18. void main()
19. {
20. intgd=DETECT,gm,xc,yc,r;
21. clrscr();
22. printf("Enter the top left coordinate");
23. scanf("%d%d",&tlx,&tly);
24. printf("Enter the bottom right coordinate");
25. scanf("%d%d",&brx,&bry);
26. printf("\n Enter the point");
27. scanf("%d%d",&px,&py);
28. initgraph(&gd,&gm,"c:\\tc\\bgi");
29. setbkcolor(BLUE);
30. setcolor(RED);
31. rectangle(tlx,tly,brx,bry);
32. point_clip();
33. }
Output:
Program2:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<graphics.h>
4. void main()
5. {
6. int gm,gr,xcmin,ycmin,xcmax,ycmax,x,y,c;
7. clrscr();
8. detectgraph(&gm,&gr);
9. initgraph(&gm,&gr,"c:\\tc\\BGI");
10. printf("Enter the clipmin coordinate :\n");
11. scanf("%d%d",&xcmin,&ycmin);
12. printf("Enter the clipmax coordinate :\n");
13. scanf("%d%d",&xcmax,&ycmax);
14. rectangle(xcmin,ycmax,xcmax,ycmin);
15. printf("Enter the coordinate of the point:\n");
16. scanf("%d%d",&x,&y);
17. detectgraph(&gm,&gr);
18. initgraph(&gm,&gr,"c:\\tc\\BGI");
19. putpixel(x,y,15);
20. printf("\n1.Point clipping\n2.Exit\nEnter your choice:\n");
21. scanf("%d",&c);
22. switch(c)
23. {
24. case 1:
25. detectgraph(&gm,&gr);
26. initgraph(&gm,&gr,"d:\\tc\\BGI");
27. rectangle (xcmin,ycmax,xcmax,ycmin);
28. printf("*******POINT CLIPPING******\n");
29. if ((xcmin<x) && (x<xcmax))
30. {
31. if ((ycmin<y) && (y<ycmax))
32. {
33. printf("The point is inside the clip window\n");
34. putpixel(x,y,15);
35. }
36. }
37. else
38. printf("The point is outside the clipwindow \nThe point is clipped\n");
39. break;
40. case 2: exit(0);
41. }
42. getch();
43. }
Output:
Line Clipping:
It is performed by using the line clipping algorithm. The line clipping algorithms are:
In the algorithm, first of all, it is detected whether line lies inside the screen or it is
outside the screen. All lines come under any one of the following categories:
1. Visible
2. Not Visible
3. Clipping Case
1. Visible: If a line lies within the window, i.e., both endpoints of the line lies within
the window. A line is visible and will be displayed as it is.
2. Not Visible: If a line lies outside the window it will be invisible and rejected. Such
lines will not display. If any one of the following inequalities is satisfied, then the line
is considered invisible. Let A (x1,y2) and B (x2,y2) are endpoints of line.
3. Clipping Case: If the line is neither visible case nor invisible case. It is considered
to be clipped case. First of all, the category of a line is found based on nine regions
given below. All nine regions are assigned codes. Each code is of 4 bits. If both
endpoints of the line have end bits zero, then the line is considered to be visible.
The center area is having the code, 0000, i.e., region 5 is considered a rectangle
window.
1. It calculates end-points very quickly and rejects and accepts lines quickly.
2. It can clip pictures much large than screen size.
Step4:If a line is clipped case, find an intersection with boundaries of the window
m=(y2-y1 )(x2-x1)
(a) If bit 1 is "1" line intersects with left boundary of rectangle window
y3=y1+m(x-X1)
where X = Xwmin
where Xwminis the minimum value of X co-ordinate of window
Let R be the rectangular window whose lower left-hand corner is at L (-3, 1) and
upper right-hand corner is at R (2, 6). Find the region codes for the endpoints in fig:
The region code for point (x, y) is set according to the scheme
Bit 1 = sign (y-ymax)=sign (y-6) Bit 3 = sign (x-xmax)= sign (x-2)
Bit 2 = sign (ymin-y)=sign(1-y) Bit 4 = sign (xmin-x)=sign(-3-x)
Here
So
We place the line segments in their appropriate categories by testing the region
codes found in the problem.
Category1 (visible): EF since the region code for both endpoints is 0000.
Category2 (not visible): IJ since (1001) AND (1000) =1000 (which is not 0000).
Category 3 (candidate for clipping): AB since (0001) AND (1000) = 0000, CD since
(0000) AND (1010) =0000, and GH. since (0100) AND (0010) =0000.
In clipping AB, the code for A is 0001. To push the 1 to 0, we clip against the
boundary line xmin=-3. The resulting intersection point is I1 (-3,3 ). We clip (do not
display) AI1 and I1 B. The code for I1is 1001. The clipping category for I1 B is 3 since
(0000) AND (1000) is (0000). Now B is outside the window (i.e., its code is 1000), so
we push the 1 to a 0 by clipping against the line ymax=6. The resulting intersection is
l2 (-1 ,6). Thus I2 B is clipped. The code for I2 is 0000. The remaining
segment I1 I2 is displayed since both endpoints lie in the window (i.e., their codes are
0000).
For clipping CD, we start with D since it is outside the window. Its code is 1010. We
push the first 1 to a 0 by clipping against the line ymax=6. The resulting intersection
I3 is ( ,6),and its code is 0000. Thus I3 D is clipped and the remaining
segment CI3 has both endpoints coded 0000 and so it is displayed.
For clipping GH, we can start with either G or H since both are outside the window.
The code for G is 0100, and we push the 1 to a 0 by clipping against the line
ymin=1.The resulting intersection point is I4 (2 ,1) and its code is 0010. We
clip GI4 and work on I4 H. Segment I4 H is not displaying since (0010) AND (0010)
=0010.
1. #include <iostream.h>
2. #include <conio.h>
3. #include <graphics.h>
4. #include <dos.h>
5. class data
6. {
7. int gd, gmode, x, y, xmin,ymin,ymax,xmax;
8. int a1,a2;
9. float x1, y1,x2,y2,x3,y3;
10. int xs, ys, xe, ye;
11. float maxx,maxy;
12. public:
13. void getdata ();
14. void find ();
15. void clip ();
16. void display (float, float,float,float);
17. void checkonof (int);
18. void showbit (int);
19. };
20. void data :: getdata ()
21. {
22. cout<<"Enter the minimum and maximum coordinate of window (x, y) ";
23. cin >>xmin>>ymin>>xmax>>ymax;
24. cout<<"Enter the end points of the line to be clipped";
25. cin >>xs>>ys>>xe>>ye;
26. display (xs, ys, xe,ye);
27. }
28. void data :: display (float, xs, float, ys,float xe, float ye)
29. {
30. int gd=DETECT;
31. initgraph (&gd,&gmode, "");
32. maxx=getmaxx();
33. maxy=getmaxy();
34. line (maxx/2,0,maxx/2,maxy);
35. line (0, maxy/2,maxx,maxy/2);
36. rectangle (maxx/2+xmin,maxy/2-ymax,maxx/2+xmax,maxy/2-ymin);
37. line (maxx/2+xs,maxy/2-ys,maxx/2+xe,maxy/2-ye);
38. getch();
39. }
40. void data :: find ()
41. {
42. a1=0;
43. a2=0;
44. if ((ys-ymax)>0)
45. a1+=8;
46. if ((ymin-ys)>0)
47. a1+=4;
48. if ((xs-xmax)>0)
49. a1+=2;
50. if ((xmin-xs)>0)
51. a1+=1;
52. if ((ye-ymax)>0)
53. a2+=8;
54. if ((ymin-ye)>0)
55. a2+=4;
56. if ((xe-xmax)>0)
57. a2+=2;
58. if ((xmin-xe)>0)
59. a2+=1;
60. cout<<"\nThe area code of Ist point is ";
61. showbit (a1);
62. getch ();
63. cout <<"\nThe area code of 2nd point is ";
64. showbit (a2);
65. getch ();
66. }
67. void data :: showbit (int n)
68. {
69. int i,k, and;
70. for (i=3;i>=0;i--)
71. {
72. and =1<<i;
73. k = n?
74. k ==0?cout<<"0": cout<<"1\"";
75. }
76. }
77. void data ::clip()
78. {
79. int j=a1&a2;
80. if (j==0)
81. {
82. cout<<"\nLine is perfect candidate for clipping";
83. if (a1==0)
84. {
85. else
86. {
87. checkonof(a1);
88. x2=x1;y2=y1;
89. }
90. if (a2=0)
91. {
92. x3=xe; y3=ye;
93. }
94. else
95. {
96. checkonof (a2);
97. x3=x1; y3=y1;
98. }
99. xs=x2; ys=y2;xe=x3;ye=y3;
100. cout << endl;
101. display (xs,ys,xe,ye);
102. cout<<"Line after clipping";
103. getch ()
104. }
105. else if ((a1==0) && (a2=0))
106. {
107. cout <<"\n Line is in the visible region";
108. getch ();
109. }
110. }
111. void data :: checkonof (int i)
112. {
113. int j, k,l,m;
114. 1=i&1;
115. x1=0;y1=0;
116. if (1==1)
117. {
118. x1=xmin;
119. y1=ys+ ((x1-xs)/ (xe-xs))*(ye-ys);
120. }
121. j=i&8;
122. if (j>0)
123. {
124. y1=ymax;
125. x1=xs+(y1-ys)/(ye-ys))*(xe-xs);
126. }
127. k=i & 4;
128. if (k==1)
129. {
130. y1=ymin;
131. x1=xs+((y1-ys)/(ye-ys))*(xe-xs);
132. }
133. m= i&2;
134. if (m==1)
135. {
136. x1=xmax;
137. y1=ys+ ((x1-xs)/ (xe-xs))*(ye-ys);
138. }
139. main ()
140. {
141. data s;
142. clrscr();
143. s.getdata();
144. s.find();
145. getch();
146. closegraph ();
147. return ();
148. }
Output:
Mid Point Subdivision Line Clipping Algorithm:
It is used for clipping line. The line is divided in two parts. Mid points of line is
obtained by dividing it in two short segments. Again division is done, by finding
midpoint. This process is continued until line of visible and invisible category is
obtained. Let (xi,yi) are midpoint
Step5: Check each midpoint, whether it nearest to the boundary of a window or not.
Step6: If the line is totally visible or totally rejected not found then repeat step 1 to 5.
Example: Window size is (-3, 1) to (2, 6). A line AB is given having co-ordinates of A
(-4, 2) and B (-1, 7). Does this line visible. Find the visible portion of the line using
midpoint subdivision?
Solution:
A (-4, 2) B ""(-1, 6)
Liang-Barsky Line Clipping Algorithm:
Liang and Barsky have established an algorithm that uses floating-point arithmetic
but finds the appropriate endpoints with at most four computations. This algorithm
uses the parametric equations for a line and solves four inequalities to find the range
of the parameter for which the line is in the viewport.
Let P(x1, y1), Q(x2, y2) is the line which we want to study. The parametric equation
of the line segment from gives x-values and y-values for every point in terms of a
parameter that ranges from 0 to 1. The equations are
3. If tmin< tmax? then draw a line from (x1 + dx*tmin, y1 + dy*tmin) to (x1 + dx*tmax?, y1 + dy*tmax? )
4. If the line crosses over the window, you will see (x1 + dx*tmin, y1 + dy*tmin) and (x1 + dx*tmax? , y1 + dy*tmax?) are
intersection between line and edge.
Text Clipping:
Several methods are available for clipping of text. Clipping method is dependent on
the method of generation used for characters. A simple method is completely
considered, or nothing considers method. This method is also called as all or none. If
all characters of the string are inside window, then we will keep the string, if a string
character is outside then whole string will be discarded in fig (a).
Another method is discarded those characters not completely inside the window. If a
character overlap boundary of window. Those will be discarded in fig (b).
Curve Clipping:
Exterior Clipping:
1. It is used for displaying properly the pictures which overlap each other.
2. It is used in the concept of overlapping windows.
3. It is used for designing various patterns of pictures.
4. It is used for advertising purposes.
5. It is suitable for publishing.
6. For designing and displaying of the number of maps and charts, it is also used.
Polygon Clipping:
Polygon clipping is applied to the polygons. The term polygon is used to define
objects having outline of solid. These objects should maintain property and shape of
polygon after clipping.
Polygon:
Example of Polygon:
1. Triangle
2. Rectangle
3. Hexagon
4. Pentagon
1. Concave
2. Convex
A polygon is called convex of line joining any two interior points of the polygon lies
inside the polygon. A non-convex polygon is said to be concave. A concave polygon
has one interior angle greater than 180°. So that it can be clipped into similar
polygons.
A polygon can be positive or negative oriented. If we visit vertices and vertices visit
produces counterclockwise circuit, then orientation is said to be positive.
Sutherland-Hodgeman Polygon Clipping:
1. If the first vertex is an outside the window, the second vertex is inside the
window. Then second vertex is added to the output list. The point of
intersection of window boundary and polygon side (edge) is also added to the
output line.
2. If both vertexes are inside window boundary. Then only second vertex is added
to the output list.
3. If the first vertex is inside the window and second is an outside window. The
edge which intersects with window is added to output list.
4. If both vertices are the outside window, then nothing is added to output list.
Following figures shows original polygon and clipping of polygon against four
windows.
Disadvantage of Cohen Hodgmen Algorithm:
This method requires a considerable amount of memory. The first of all polygons are
stored in original form. Then clipping against left edge done and output is stored.
Then clipping against right edge done, then top edge. Finally, the bottom edge is
clipped. Results of all these operations are stored in memory. So wastage of memory
for storing intermediate polygons.
When the clipped polygons have two or more separate sections, then it is the
concave polygon handled by this algorithm. The vertex-processing procedures for
window boundaries are modified so that concave polygon is displayed.
Let the clipping window be initially called clip polygon and the polygon to be clipped
the subject polygon. We start with an arbitrary vertex of the subject polygon and
trace around its border in the clockwise direction until an intersection with the clip
polygon is encountered:
1. If the edge enters the clip polygon, record the intersection point and continue to
trace the subject polygon.
2. If the edge leaves the clip polygon, record the intersection point and make a right
turn to follow the clip polygon in the same manner (i.e., treat the clip polygon as
subject polygon and the subject polygon as clip polygon and proceed as before).
For example, the number in fig (a) indicates the order in which the edges and
portion of edges are traced. We begin at the starting vertex and continue along the
same edge (from 1 to 2) of the subject polygon as it enters the clip polygon. As we
move along the edge that is leaving the clip polygon, we make a right turn (from 4 to
5) onto the clip polygon, which is now considered the subject polygon. Following the
same logic leads to the next right turn (from 5 to 6) onto the current clip polygon,
this is the original subject polygon. With the next step done (from 7 to 8) in the same
way, we have a sub-polygon for output in fig (b). We then resume our traversal of the
original subject polygon from the recorded intersection point where we first changed
our course. Going from 9 to 10 to 11 produces no output. After skipping the already
traversed 6 and 7, we continue with 12 and 13 and come to an end. The fig (b) is the
final result.
Pointing technique refers to look at the items already on the screen whereas the
positioning technique refers to position the item on the screen to a new position, i.e.,
the old current position. The user indicates a position on the screen with an input
device, and this position is used to insert a symbol.
There are various pointing and positioning devices which are discussed below:
1. Light Pen
2. Mouse
3. Tablet
4. Joystick
5. Trackball and spaceball
1. Light Pen: It is a pointing device. When light pen is pointed at an item on the
screen, it generates information from which the item can be identified by the
program. It does not have any associated tracking hardware instead tracking is
performed by software, making use of the output function of the display. All light
pen programs depend on a rapid response from the pen when it is pointed at the
screen fast response light pens can be build by using a highly sensitive photocell
such as a photomultiplier tube.
4. Joystick: A joystick consists of a small that is used to steer the screen cursor
around. The distance that the stick is moved in any direction from its center position
corresponds to the screen-cursor movement in that direction. Pressure sensitive
joysticks have a non-moveable stick. Pressure on the stick is measured with strain
gauges and converted to the movement of the cursor in the direction specified.
5. Trackball and spaceball: Trackball is a ball that can be rotated with the fingers
to produces screen-cursor movement potentiometers, attached to the ball, measure
the amount and direction of rotation. Trackballs are after mounted on keyboards,
whereas space-ball provides six degrees of freedom. Spaceballs is used for three-
dimensional positioning and selection operation in virtual reality system, modeling,
animation, CAD and other applications.
It becomes an integral part and de facto standard with the graphical user interface
(GUI) for drawing and is almost universally accepted by all windows based
applications.
The user specifies the line in the usual way by positioning its two endpoints. As we
move from the first endpoint to the second, the program displays a line from the first
endpoint to the cursor position, thus he can see the lie of the line before he finishes
positioning it.
The effect is of an elastic line stretched between the first endpoint and the cursor;
hence the name for these techniques.
Consider the different linear structures in fig (a) and fig (d), depending on the
position of the cross-hair cursor. The user may move the cursor to generate more
possibilities and select the one which suits him for a specific application.
Selection of Terminal Point of the Line:
The user moves the cursor to the appropriate position and selects.
Then, as the cursor is moved, the line changes taking the latest positions of the
cursors as the end-point.
As long as the button is held down, the state of the rubber band is active.
The process is explained with the state transition diagram of rubber banding in fig:
When the user is happy with the final position, the pressed button is released, and
the line is drawn between the start and the last position of the cursor.
Example: This is widely followed in MS-Window based Applications like in the case
of a paintbrush drawing package.
- Rectangles
- Arcs of circles
Advantage:
1. It is used for drawing all geometric entities such as line, polygon, circle,
rectangle, ellipse, and other curves.
2. It is easy to understand and implement.
Disadvantage:
Dragging
Dragging is used to move an object from one position to another position on the
computer screen. To drag any other object, first, we have to select the object that we
want to move on the screen by holding the mouse button down. As cursor moved on
the screen, the object is also moved with the cursor position. When the cursor
reached the desired position, the button is released.
Animation
Animation refers to the movement on the screen of the display device created by
displaying a sequence of still images. Animation is the technique of designing,
drawing, making layouts and preparation of photographic series which are
integrated into the multimedia and gaming products. Animation connects the
exploitation and management of still images to generate the illusion of movement. A
person who creates animations is called animator. He/she use various computer
technologies to capture the pictures and then to animate these in the desired
sequence.
Animation includes all the visual changes on the screen of display devices. These are:
Animation Functions
3. In the third step, the key point of the first image transforms to a corresponding
key point of the second image as shown in 3rd object of the figure.
5. Zooming: In zooming, the window is fixed an object and change its size, the object
also appear to change in size. When the window is made smaller about a fixed center,
the object comes inside the window appear more enlarged. This feature is known
as Zooming In.
When we increase the size of the window about the fixed center, the object comes
inside the window appear small. This feature is known as Zooming Out.