Sie sind auf Seite 1von 10

COMPUTER GRAPHICS CAP616T

PART-A Question-1:- Write an algorithm to show the 3D view of a hut.? Solution:#include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h>

void drawhouse(float hx[],float hy[]) { int i; int cx=getmaxx()/2; int cy=getmaxy()/2; for(i=0;i<4;i++) line(cx+hx[i],cy-hy[i],cx+hx[i+1],cy-hy[i+1]); line(cx+hx[4],cy-hy[4],cx+hx[0],cy-hy[0]); } void initscreen(float hx[],float hy[]) { line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy()); drawhouse(hx,hy); }

void main() { int gd=DETECT,gm,ch,i,px,py,lx1,lx2,ly1,ly2; float sx,sy,mx,my,x,y,m,c,thx[5],thy[5]; float hx[]={40.0,10.0,10.0,70.0,70.0}; float hy[]={80.0,50.0,10.0,10.0,50.0}; initgraph(&gd,&gm,"c:\\tc");

initscreen(hx,hy); do { printf("\nEnter the choice from below"); printf("\n1.Scale wrt 0,0"); printf("\n2.Scale wrt arb pt"); printf("\n3.reflect abt pt"); printf("\n4.exit"); scanf("%d",&ch); switch(ch) { case 1: cleardevice(); initscreen(hx,hy); for(i=0;i<=4;i++) { thx[i]=hx[i]; thy[i]=hy[i]; } printf("\nEnter the scale value of x and y axis"); scanf("%f%f",&sx,&sy); for(i=0;i<=4;i++) { thx[i]=thx[i]*sx; thy[i]=thy[i]*sy; } drawhouse(thx,thy); break;

case 2: cleardevice(); initscreen(hx,hy); for(i=0;i<=4;i++) { thx[i]=hx[i]; thy[i]=hy[i]; } printf("\nEnter the ref point"); scanf("%d%d",&px,&py);

printf("\nEnter the scale value of x and y axis"); scanf("%f%f",&sx,&sy); for(i=0;i<=4;i++) { thx[i]-=px; thy[i]-=py; } for(i=0;i<=4;i++) { thx[i]=thx[i]*sx; thy[i]=thy[i]*sy; } for(i=0;i<=4;i++) { thx[i]+=px; thy[i]+=py; } drawhouse(thx,thy); break; case 3: cleardevice(); initscreen(hx,hy); printf("\nEnter the line with m amd c value"); scanf("%f%f",&m,&c); if(m>0||m<0) { lx1=0; ly1=c; lx2=(getmaxy()-c)/m; ly2=getmaxy(); } if(m==0) { lx1=0; ly1=c; lx2=getmaxx(); ly2=c; }

if(m>=9999) { lx1=lx2=c; ly1=0; ly2=getmaxy(); } for(i=0;i<=4;i++) { thx[i]=hx[i]; thy[i]=hy[i]; } for(i=0;i<=4;i++) { x=thx[i]; y=thy[i]; if(m==0) { thx[i]=x; thy[i]=c+(c-y); } if(m>0||m<0) { thx[i]=2*((m*m*y+x)-c*m*m)/((1+m*m)*m)-x; thy[i]=2*(c+m*m*y+x)/(1+m*m)-y; } if(m>=9999) { thy[i]=y; thx[i]=c+(c-x); } } drawhouse(thx,thy); line(getmaxx()/2+lx1,getmaxy()/2-ly1,getmaxx()/2+lx2,getmaxy()/2-ly2); break; case 4: exit(1); } }while(ch!=4); }

Question-2:-Write notes on :Joystick,Image Scanners,Voice systems and RasterScan Display Processor? Solution:Joystick:-A Joystick has two potentiometers, just as a pair of thumbwheels. They have been attached to a
single lever. Moving the lever forward or back changes the setting on one potentiometer. Moving it left or right changes the setting on the other potentiometer. Thus with a joystick both x and y coordinate positions can be simultaneously altered by the motion of a single lever.

Image Scanners:-Drawings,

graphs, color and black & white photos or text can be stored for computer processing with an image scanner by passing an optical scanning mechanism over the information to be stored. The gradations of gray scale or color are then recorded and stored in an array. Once we have the internal representation of a picture, we can apply transformations to rotate, scale or crop the picture to a particular screen area. We can also apply various image processing methods to modify the array representation of the picture. For scanned text input, various editing operations can be performed on the stored documents. Some scanners are able to scan either graphical representations or text and they come in a variety of sizes and capabilities.

Voice systems:-Speech recognizers

are used in some graphics workstations as input devices to accept voice command. The voice system input can be used to initiate graphics operations or to enter data. These systems operate by matching an input against a predefined dictionary of words and phrases. A dictionary is set up for a particular operator by having the operator speak the command words to be used into the system. Each word is spoken several times, and the system analyzes the word and establishes a frequency pattern for that word in the dictionary alone with the corresponding function to be performed. When a voice command is given, the system searches the dictionary for a frequency pattern match. Voice input is typically spoken into a microphone mounted on a headset. If a different operator is to use the system, the dictionary must be reestablished with that operators voice patterns.

Raster-Scan Display Processor:- The purpose of the display processor is to free the CPU from the
graphics chores. In addition to the system memory, a separate display processor memory area can also be provided. A major task of the display processor is digitizing a picture definition given in an application program into a set of pixel-intensity values for storage in the frame buffer. This digitization process is called scan conversion. Characters can be defined with rectangular grids. The array size for character grids can vary from about 5 by 7 to 9 by 12 or more for higher quality displays. Display processors are typically designed to interface with interactive input devices such as mouse. In an effort to reduce memory requirements in raster systems, methods have been devised for organizing the frame buffer as a linked list and encoding the intensity information. One way to do this is to store each scan line as a set of integer pairs. One number of each pair indicates an intensity value, and the second number specifies the number of adjacent pixels on the scan line that are to have that intensity. This technique called run-length encoding. A similar approach can be taken when pixel intensities change linearly. Another approach is to encode the raster as a set of rectangular areas (cell encoding).

Question-3:-What is difference between Raster-Scan Display and Random-Scan Display? Solution:Raster Scan System Random Scan System

The electron beam is swept across the screen, one row at The electron beam is directed only to the parts of screen a time, from top to bottom. where a picture is to be drawn. Its resolution is poor because raster system in contrast Its resolution is good because this system produces smooth produces zig-zag lines that are plotted as discrete point lines drawings because CRT beam directly follows the line sets. path. Picture definition is stored as a set of intensity values for Picture definition is stored as a set of line drawing all screen points, called pixels in a refresh buffer area. instructions in a display file. The capability of this system to store intensity values for These systems are designed for line-drawing and cant pixel makes it well suited for the realistic display of scenes display realistic shaded scenes. contain shadow and color pattern. Screen points/pixels are used to draw an image. Mathematical functions are used to draw an image.

PART - B Question-4:-Write an algorithm for circle drawing using Bresenhams algorithm? Solution:-A circle is defined as the set of points that are all at a given distance r from a center position (x, y).
The coordinate values are calculated for a mouse placed in origin and later each calculated position is moved to the desired screen position by adding x to x coordinate value and y to y coordinate value. The points are calculated for an octet of the circle and the rest of the points are obtained by property of symmetry.

Algorithm
Input values of radius r and circle centre ( x0, y0) and obtain the first pt on the circumference of a circle centred on the origin as ( x0, y0)= (0, l) Calculate the initial decision parameter as d= 3-2r For each pixel position (xi, yi),perform: If di < 0, the next point is (xi + 1 , yi) and di + 1 = di + 4xi +6 Otherwise the next point along the circle is (xi+1, yi -1)and di + 1 = di + 4 (xi - yi) + 10

Determine symmetry points on the other 7 octants Move each calculated pixel position (x, y) onto the circular path centred on (xa , y a) and plot the co-ordinate values. x = x + xa y = y + ya

Repeat step 2 to 5 until x>=y

Question-5:-Write an algorithm to draw different moods of a smiley? Solution:#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\tc\bgi"); setcolor(GREEN); setbkcolor(0); ellipse(298,244,160,380,60,80); arc(300,219,400,140,80); ellipse(355,190,270,438,10,28); arc(359,188,169,265,30); ellipse(288,190,180,360,40,20); ellipse(239,193,96,370,8,25); arc(282,255,89,130,40); arc(278,259,80,120,40); arc(314,255,405,92,40); arc(319,259,420,100,40); line(310,215,310,220); line(284,215,284,219); setfillstyle(SOLID_FILL,WHITE); ellipse(320,230,0,360,10,5); ellipse(275,230,0,360,10,5); fillellipse(320,230,10,5); fillellipse(275,230,10,5); setfillstyle(SOLID_FILL,BLACK); ellipse(320,230,0,360,4,4); ellipse(275,230,0,360,4,4); fillellipse(320,230,5,5);

fillellipse(275,230,5,5);

ellipse(280,220,270,0,10,40); ellipse(315,220,180,270,10,40); ellipse(285,260,100,285,8,7); ellipse(310,260,255,70,8,7); circle(320,230,2); circle(275,230,2); arc(297,257,228,689,15); ellipse(298,290,0,360,30,7); line(270,290,326,290); ellipse(234,240,0,330,4,20); ellipse(362,240,220,170,4,20); getch(); closegraph(); restorecrtmode(); }

Question-6:-Which algorithm is more efficient between DDA line drawing algorithm and Bresenham's algorithm? Solution:-Bresenhams algorithm is more efficient and much accurate than DDA algorithm.
Digital Differential Analyzer Line Drawing Algorithm
DDA algorithm uses floating points i.e. Real Arithmetic.

Bresenhams Line Drawing Algorithm

Bresenhams algorithm uses fixed points i.e. Integer Arithmetic. Bresenhams algorithm uses only subtraction and addition in its operations. Bresenhams algorithm is faster than DDA algorithm in line drawing because it performs only addition and subtraction in its calculation and uses only integer arithmetic so it runs significantly faster. Bresenhams algorithm can draw circles and curves with much more accuracy than DDA algorithm. Bresenhams algorithm does not round off but takes the incremental value in its operation. Bresenhams algorithm is less expensive than DDA algorithm as it uses only addition and subtraction.

DDA algorithm uses multiplication and division in its operations.

DDA algorithm is rather slowly than Bresenhams algorithm in line drawing because it uses real arithmetic (floatingpoint operations). DDA algorithm can draw circles and curves but that are not as accurate as Bresenhams algorithm. DDA algorithm round off the coordinates to integer that is nearest to the line. DDA algorithm uses an enormous number of floating-point multiplications so it is expensive.

Das könnte Ihnen auch gefallen