The document describes algorithms for Bresenham's line drawing, circle drawing, and ellipse drawing. It provides pseudocode for each algorithm, with steps including initializing graphics mode, getting input coordinates, calculating values, plotting pixels in a loop, and displaying the output shape. It also includes a C program that implements functions for drawing lines, circles, and ellipses using these algorithms, and allows the user to select and provide input for which shape to draw. The program executes the selected drawing method and allows the user to continue drawing more shapes.
Download as DOCX, PDF, TXT or read online on Scribd
100%(1)100% found this document useful (1 vote)
348 views
CG Lab Manual
The document describes algorithms for Bresenham's line drawing, circle drawing, and ellipse drawing. It provides pseudocode for each algorithm, with steps including initializing graphics mode, getting input coordinates, calculating values, plotting pixels in a loop, and displaying the output shape. It also includes a C program that implements functions for drawing lines, circles, and ellipses using these algorithms, and allows the user to select and provide input for which shape to draw. The program executes the selected drawing method and allows the user to continue drawing more shapes.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56
EX.
NO: 1(a) BRESENHAMS LINE DRAWING ALGORITHM
DATE:
Aim: To implement Bresenhams line drawing Algorithm for drawing lines.
ALGORITHM:
STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: get line coordinates x1,y1 and x2,y2. STEP 4: calculate dx from x1,x2 and dy from y1,y2 coordinates. STEP 5: calculate e (error) value. STEP 6: initialize i=1. STEP 7: putpixel(x,y,15). STEP 8: check e>=0 is true then calculate y=y+1 and e=e-2*dx else calculate x=x+1 and e=e+2*dy and i=i+1. STEP 9: repeat steps 7 and 8 until i<=dx. STEP 10: Display a line.
PROGRAM: #include<stdio.h> #include<graphics.h> #include<math.h> #include<conio.h> void main() { float x,y,x1,y1,x2,y2,dx,dy,e; int i,gd,gm; clrscr(); printf("Enter the (x1,y1) coordinate:"); scanf("%f%f",&x1,&y1); printf("Enter the (x2,y2) coordinate:"); scanf("%f%f",&x2,&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI\\"); dx=abs(x2-x1); dy=abs(y2-y1); x=x1; y=y1; e=2*dy-dx; i=1; do { putpixel(x,y,15); while(e>=0) { y=y+1; e=e-2*dx; } x=x+1; e=e+2*dy; i=i+1; } while(i<=dx); getch(); closegraph(); } INPUT: Enter the (x1, y1) coordinates: 200 100 Enter the (x2, y2) coordinates: 500 100
RESULT: Thus the above program for implementation of Bresenhams line drawing algorithm was successfully executed.
EX.NO: 1(B) BRESENHAMS CIRCLE DRAWING ALGORITHM DATE:
Aim: To implement the Bresenhams circle drawing algorithm using c language.
ALGORITHM: [ STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: get the radius of a circle. STEP 4: initialize the variable x=0,y=r and d=3-2*r. STEP 5: putpixel(x,y,15). STEP 6: If d<=0 then calculate d=d+4*x+6 else calculate d=d+4*(x-y)+10 and y=y-1. STEP 7: increment x=x+1. STEP 8: repeat steps 5,6 and 7 until x<y. STEP 9: Display a circle.
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { float d; int gd,gm,x,y,r; clrscr(); printf("Enter the radius of a circle:"); scanf("%d",&r); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI\\"); x=0; y=r; d=3-2*r; do { putpixel(200+x,200+y,15); putpixel(200+y,200+x,15); putpixel(200+y,200-x,15); putpixel(200+x,200-y,15); putpixel(200-x,200-y,15); putpixel(200-y,200-x,15); putpixel(200-y,200+x,15); putpixel(200-x,200+y,15); if(d<=0) { d=d+4*x+6; } else { d=d+4*(x-y)+10; y=y-1; } x=x+1; } while(x<y); getch(); closegraph(); }
INPUT: Enter the radius of a circle: 100
RESULT: Thus the above program for implementation of Bresenhams circle drawing algorithm was successfully executed.
Aim: To write a C program for Circle Drawing Using Bresenham Algorithm. ALGORITHM:
Step 1: Start the program. Step 2: Initialize the variables. Step 3: Call the initgraph() function. Step 4: Get the initialize points P1,P2. Step 5: Get the values of Co-Ordinates of the ellipse (x,y). Step 6: Enter the coordinates a,b of the ellipse . Step 7: Display the output. Step 8: Stop the program
PROGRAM #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void main() { int gd,gm,x1=0,y1=0,r1=0,r2=0; float x=0,y=0,t,d,i; clrscr(); printf("Enter the co-ordinates of a circle:"); scanf("%d%d",&x1,&y1); printf("Enter the radius of a circle:"); scanf("%d%d",&r1,&r2); detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:\\TC\\BGI\\"); for(i=0;i<360;i++) { t=3.14/180; d=i*t; x=x1+ceil(r1*sin(d)); y=y1+ceil(r2*cos(d)); putpixel(x,y,15); } getch(); closegraph(); }
Input: Enter the center co-or:100 150 Enter the radius1:40 Enter radius2:20
Result: Thus the program to draw ellipse using Bresenhams ellipse drawing Algorithm was executed successfully.
EX.NO: 2 IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES DATE:
Aim: To write a program to draw a Line, Circle and ellipse Attributes
Functions used: Circle()
The function circle() is used to draw a circle using(x,y) as centre point.
Syntax: circle (x,y,radius)
initgraph().
This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name.
Syntax: Initgraph(gd,gm,path)
Putpixel () The function putpixel() is used to place a pixel at particular coordinate
Syntax: Putpixel(x,y,color)
Algorithm: Step 1: Start Step 2: Get the center point as (xc,yc),get the radius as r. Step 3: Assign y=r,x=0 Step 4: Calculate p=3-2r Step 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and y=y-1 Step 6: Increment x by 1 Step 7:stop
PROGRAM: #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #define ROUND(a) ((int)(a+0.5)) //Line drawing void lineBres(int xa,int ya,int xb,int yb) { int dx=abs(xa-xb),dy=abs(ya-yb); int p=2*dy-dx; int twoDy=2*dy,twoDyDx=2*(dy-dx); int x,y,xEnd; /* Determine which point to use as start,which as end */ if(xa>xb) { x=xb; y=yb; xEnd=xa; } Else { x=xa; y=ya; xEnd=xb; } putpixel(x,y,15); while(x<xEnd) { x++; if(p<0) p+=twoDy; else { y++; p+=twoDyDx; } putpixel(x,y,15); } } //Circle drawing void circleMidPoint(int xCenter,int yCenter,int radius) { int x=0; int y=radius; int p=1-radius; void circlePlotPoints(int,int,int,int); /*plot first set of points*/ circlePlotPoints(xCenter,yCenter,x,y); while(x<y) { x++; if(p<0) p+=2*x+1; else { y--; p+=2*(x-y)+1; } circlePlotPoints(xCenter,yCenter,x,y); } } void circlePlotPoints(int xCenter,int yCenter,int x,int y) { putpixel(xCenter+x,yCenter+y,WHITE); putpixel(xCenter-x,yCenter+y,WHITE); putpixel(xCenter+x,yCenter-y,WHITE); putpixel(xCenter-x,yCenter-y,WHITE); putpixel(xCenter+y,yCenter+x,WHITE); putpixel(xCenter-y,yCenter+x,WHITE); putpixel(xCenter+y,yCenter-x,WHITE); putpixel(xCenter-y,yCenter-x,WHITE); }//Ellipse drawing void ellipsemidpoint(int xcenter,int ycenter,int rx,int ry) { int rx2=rx*rx; int ry2=ry*ry; int tworx2=2*rx2; int twory2=2*ry2; int p,x=0,y=ry,px=0; int py=tworx2*y; void ellipseplotpoints(int,int,int,int); ellipseplotpoints(xcenter,ycenter,x,y); p=ROUND(ry2-(rx2*ry)+(0.25*rx2)); while(px<py) { x++; px+=twory2; if(p<0) p+=ry2+px; else { y--; py-=tworx2; p+=ry2+px-py; } ellipseplotpoints(xcenter,ycenter,x,y); } p=ROUND(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2); while(y>0) { y--; py-=tworx2; if(p>0) p+=rx2-py; else { x++; px+=twory2; p+=rx2-px+px; } ellipseplotpoints(xcenter,ycenter,x,y); } } void ellipseplotpoints(int xcenter,int ycenter,int x,int y) { putpixel(xcenter+x,ycenter+y,5); putpixel(xcenter-x,ycenter+y,5); putpixel(xcenter+x,ycenter-y,5); putpixel(xcenter-x,ycenter-y,5); } void main() { int ch,c; co: clrscr(); printf("\n\t\tBRESENHAM BDRAWINGS\n"); printf("\n\t\t1-Line drawing"); printf("\n\t\t2-Circle drawing"); printf("\n\t\t3-Ellipse drawing"); printf("\n\t\t4-Exit"); printf("\nEnter your choice :"); scanf("%d",&ch); int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, " "); switch(ch) { case 1: int x1,y1,x2,y2; printf("Enter the starting co-ordinates: "); scanf("%d %d",&x1,&y1); printf("Enter the ending co-ordinates: "); scanf("%d %d",&x2,&y2); lineBres(x1,y1,x2,y2); getch(); printf("\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):"); scanf("%d",&c); if (c==1) goto co; break;
case 2: int xc1,yc1,r; printf("Enter the centre co-ordinates: "); scanf("%d %d",&xc1,&yc1); printf("Enter the radius: "); scanf("%d",&r); circleMidPoint(xc1,yc1,r); getch(); printf("\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):"); scanf("%d",&c); if(c==1) goto co; break;
case 3: int xc,yc,rx,ry; printf("Enter the value of xcenter and ycenter co-ordinates: "); scanf("%d %d",&xc,&yc); printf("Enter the radius of x and y: "); scanf("%d %d",&rx,&ry); ellipsemidpoint(xc,yc,rx,ry); getch(); printf("\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):"); scanf("%d",&c); if (c==1) goto co; break;
case 4: break; }}
Result: Thus the program to draw and implement Line, Circle and ellipse Attributes was executed successfully.
EX.NO:3 TWO DIMENSIONAL TRANSFORMATIONS DATE:
AIM: To write a c program for 2D transformation.
ALGORITHM:
STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: Display a Menu and get choice from user. STEP 4: get the object (triangle) coordinates. STEP 5: Display the original object position. STEP 6: a. if the choice is 1 then get translation vertex from user and translate the object to given vertex. b. if the choice is 2 then get rotation angle and pivot point from user and rotate the object about pivot point. c. if the choice is 3 then get rotation angle from user and rotate the object about origin. d. if the choice is 4 then get scaling factor and fixed point from user and scale the object from fixed point. e. if the choice is 5 then get shear value and fixed point from user and shear the object from fixed point. f. If the choice is 6 then reflect the object. g. if the choice is 7 then exit the program. STEP 7: Display the transformed object.
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> #include<stdlib.h> void menu(); void input(); void output(); void translation(); void rotation(); void originrotation(); void scaling(); void shearing(); void reflection(); int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y; float sx,sy; void menu() { printf("-----------------------------------\n"); printf("\tMENU\n"); printf("\t1-Translation\n"); printf("\t2-Rotation\n"); printf("\t3-Rotation about origen\n"); printf("\t4-Scaling\n"); printf("\t5-Shearing\n"); printf("\t6-Reflection\n"); printf("\t7-Exit\n"); printf("-----------------------------------\n"); printf("Enter your choice:"); scanf("%d",&option); switch(option) { case 1: input(); translation(); break; case 2: input(); rotation(); break; case 3: input(); originrotation(); break; case 4: input(); scaling(); break; case 5: input(); shearing(); break; case 6: input(); reflection(); break; case 7: exit(0); break; } } void input() { printf("Enter the number of vertices:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the coordinates:"); scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]); } } void output() { cleardevice(); for(i=0;i<n;i++) { line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); } } void translation() { output(); printf("Enter the transformation vertex tx,ty:\n"); scanf("%d%d",&tx,&ty); for(i=0;i<=n;i++) { a[i][0]=a[i][0]+tx; a[i][1]=a[i][1]+ty; } output(); delay(2); menu(); } void rotation() { output(); printf("Enter the rotation angle:"); scanf("%d",&y); printf("Enter the pivot point:"); scanf("%d%d",&fx,&fy); k=(y*3.14)/180; for(i=0;i<=n;i++) { a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k); a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k); } output(); delay(2); menu(); } void originrotation() { output(); printf("Enter the rotation angle:"); scanf("%d",&y); k=(y*3.14)/180; for(i=0;i<=n;i++) { a[i][0]=a[i][0]*cos(k)-a[i][1]*sin(k); a[i][1]=a[i][0]*sin(k)-a[i][1]*cos(k); } output(); } void scaling() { output(); printf("Enter the scaling factor:\n"); scanf("%f%f",&sx,&sy); printf("Enter the fixed piont:"); scanf("%d%d",&fx,&fy); for(i=0;i<=n;i++) { a[i][0]=a[i][0]*sx+fy*(1-sx); a[i][1]=a[i][1]*sy+fy*(1-sy); } output(); delay(2); menu(); } void shearing() { output(); printf("Enter the shear value:"); scanf("%d",&sh); printf("Enter the fixed piont:"); scanf("%d%d",&fx,&fy); printf("Enter the axis for shearing if x-axis then 1 if y-axis then 0:"); scanf("%d",&axis); for(i=0;i<=n;i++) { if(axis==1) { a[i][0]=a[i][0]+sh*(a[i][1]-fy); } else { a[i][1]=a[i][1]+sh*(a[i][0]-fx); } } output(); delay(2); menu(); } void reflection() { for(i=0;i<=n;i++) { temp=a[i][0]; a[i][0]=a[i][1]; a[i][1]=temp; } output(); } void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\TC\\BGI\\"); menu(); getch(); } INPUT: ---------------------------------------------------------- MENU 1-Translation 2-Rotation 3-Rotation about origin 4-Scaling 5-Shearing 6-Reflection 7-Exit Enter your choice: 1 Enter the number of vertices: 3 Enter the coordinates: 30 150 10 200 Enter the coordinates: 10 200 60 200 Enter the coordinates: 60 200 30 150 RESULT: Thus the above program for 2D transformation was successfully executed.
EX.NO: 4 COMPOSITE 2D TRANSFORMATION DATE: AIM: To write a c program for composite 2D transformation.
ALGORITHM:
STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: Display a Menu and get choice from user. STEP 4: get the object (triangle) coordinates. STEP 5: Display the original object position. STEP 6: a. if the choice is 1 then get translation vertex from user and translate the object to given vertex. b. if the choice is 2 then get rotation angle and pivot point from user and rotate the object about pivot point. c. if the choice is 3 then get rotation angle from user and rotate the object about origin. d. if the choice is 4 then get scaling factor and fixed point from user and scale the object from fixed point. e. if the choice is 5 then get shear value and fixed point from user and shear the object from fixed point. f. If the choice is 6 then reflect the object. g. if the choice is 7 then exit the program. STEP 7: Display the transformed object.
PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> #include<stdlib.h> void menu(); void input(); void output(); void translation(); void rotation(); void originrotation(); void scaling(); void shearing(); void reflection(); int a[10][2],i,x,option,temp,angle,tx,ty,tx1,ty1,tx2,ty2,fx,fy,sh,k,n,axis,y,y1,y2,k1,k2,sh1,sh2; float sx,sy,sx1,sy1,sx2,sy2; void menu() { printf("-----------------------------------\n"); printf("\tMENU\n"); printf("\t1-Translation\n"); printf("\t2-Rotation\n"); printf("\t3-Rotation about origen\n"); printf("\t4-Scaling\n"); printf("\t5-Shearing\n"); printf("\t6-Reflection\n"); printf("\t7-Exit\n"); printf("-----------------------------------\n"); printf("Enter your choice:"); scanf("%d",&option); switch(option) { case 1: input(); translation(); break; case 2: input(); rotation(); break; case 3: input(); originrotation(); break; case 4: input(); scaling(); break; case 5: input(); shearing(); break; case 6: input(); reflection(); break; case 7: exit(0); break; } } void input() { printf("Enter the number of vertices:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the coordinates:"); scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]); } } void output() { cleardevice(); for(i=0;i<n;i++) { line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); } } void translation() { output(); printf("Enter the transformation vertex tx1,ty1 and tx2,ty2:\n"); scanf("%d%d%d%d",&tx1,&ty1,&tx2,&ty2); tx=tx1+tx2; ty=ty1+ty2; for(i=0;i<=n;i++) { a[i][0]=a[i][0]+tx; a[i][1]=a[i][1]+ty; } output(); } void rotation() { output(); printf("Enter the rotation angle:"); scanf("%d%d",&y1,&y2); printf("Enter the pivot point:"); scanf("%d%d",&fx,&fy); k1=(y1*3.14)/180; k2=(y2*3.14)/180; k=k1*k2; for(i=0;i<=n;i++) { a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k); a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k); } output(); } void originrotation() { output(); printf("Enter the rotation angle:"); scanf("%d",&y1,&y2); k1=(y1*3.14)/180; k2=(y2*3.14)/180; k=k1*k2; for(i=0;i<=n;i++) { a[i][0]=a[i][0]*cos(k)-a[i][1]*sin(k); a[i][1]=a[i][0]*sin(k)-a[i][1]*cos(k); } output(); } void scaling() { output(); printf("Enter the scaling factor:\n"); scanf("%f%f%f%f",&sx1,&sy1,&sx2,&sy2); printf("Enter the fixed piont:"); scanf("%d%d",&fx,&fy); sx=sx1*sx2; sy=sy1*sy2; for(i=0;i<=n;i++) { a[i][0]=a[i][0]*sx+fy*(1-sx); a[i][1]=a[i][1]*sy+fy*(1-sy); } output(); } void shearing() { output(); printf("Enter the shear value:"); scanf("%d%d",&sh1,&sh2); printf("Enter the fixed piont:"); scanf("%d%d",&fx,&fy); printf("Enter the axis for shearing if x-axis then 1 if y-axis then 0:"); scanf("%d",&axis); sh=sh1*sh2; for(i=0;i<=n;i++) { if(axis==1) { a[i][0]=a[i][0]+sh*(a[i][1]-fy); } else { a[i][1]=a[i][1]+sh*(a[i][0]-fx); } } output(); } void reflection() { for(i=0;i<=n;i++) { temp=a[i][0]; a[i][0]=a[i][1]; a[i][1]=temp; } output(); } void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\TC\\BGI\\"); menu(); getch(); }
RESULT: Thus the above program for composite 2D transformation was successfully executed.
Aim: To implement Cohen-Sutherland clipping& WindowingAlgorithm.
Functions used:
Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax: line (x1,y1,x2,y2)
initgraph(). This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name.
Syntax: Initgraph(gd,gm,path)
Setcolor(). This function changes the drawing colour.
Syntax: Setcolor(value of the color)
Settextstyle(). The function settextstyle() is used to change the style of the text.
Syntax: Settextstyle(font,direction,charsize)
Where font is the constant value or the font filename, direction is the number either 0 or 1, which makes the output to display in horizontal, or in vertical direction, charsize is the character size or magnification factor and it varies from 1 to 10.
Outtext(). This function display a text message on upper left of the screen
Syntax: Outtext(message);
Algorithm:
Step 1. Create a class sulc with functions drawwindow, drawline, setcode, visibility and reset endpoint. Step 2. Using the function line set the parameters to draw window. Step 3. Using the function defined in class sulc, setcode is used to save the line inside the window and to the line outside the window. Step 4. Using the function visibility i).check the code to know the points inside or outside the window. ii).if the code value is zero the point is inside the window. Step 5. Using the function reset end point i). if the code value for the line is outside the window. ii).reset the endpoint to the boundary of the window. Step 6. Initialize the graphics functions Step 7. Declare the variables x1, x2, y1, y2 of array type. Step 8. Get the value of two endpoints x1, y1 and x2, y2 to draw the line. Step 9. Using the object c, display the window before clipping. Step 10. Using the function setcode, visibility display the clipped window only with lines inside the window class was displayed after clipping.
Enter the no.of lines: 1 Enter end-point1(x1,y1):30 40 Enter end-point1(x2,y2):300 400
Result: Thus the program to implement Cohen-Sutherland clipping& Windowing was executed successfully EX NO: 6 COHEN SUTHERLAND 2D LINE CLIPPING AND WINDOWING DATE: Aim: To implement Sutherland Hodgeman Polygon clipping Algorithm
Functions used: Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax: line (x1,y1,x2,y2)
initgraph(). This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name.
Syntax: Initgraph(gd,gm,path)
Setcolor(). This function changes the drawing colour.
Syntax: Setcolor(value of the color)
Settextstyle(). The function settextstyle() is used to change the style of the text.
Syntax: Settextstyle(font,direction,charsize)
Where font is the constant value or the font filename, direction is the number either 0 or 1, which makes the output to display in horizontal, or in vertical direction, charsize is the character size or magnification factor and it varies from 1 to 10.
Outtext(). This function display a text message on upper left of the screen
Syntax: Outtext(message);
Algorithm:
Step 1. Create a class sulc with functions drawwindow, drawline, setcode, visibility and reset endpoint. Step 2. Using the function line set the parameters to draw window. Step 3. Using the function defined in class sulc, setcode is used to save the line inside the window and to the line outside the window. Step 4. Using the function visibility i).check the code to know the points inside or outside the window. ii).if the code value is zero the point is inside the window. Step 5. Using the function reset end point i). if the code value for the line is outside the window. ii).reset the endpoint to the boundary of the window. Step 6. Initialize the graphics functions Step 7. Declare the variables x1, x2, y1, y2 of array type. Step 8. Get the value of two endpoints x1, y1 and x2, y2 to draw the line. Step 9. Using the object c, display the window before clipping. Step 10. Using the function setcode, visibility display the clipped window only with lines inside the window class was displayed after clipping.
PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> #include<dos.h> #include<process.h> int pixels[2][4]; float xn1,xn2,yn1,yn2,x3,y3,m; int xmin,ymin,xmax,ymax,x1,y1,x2,y2; int choice,ed[20],num; void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax) { int i,j,f1; for(i=0;i<2;i++) for(j=0;j<4;j++) pixels[i][j]=0; if(y1>ymax) pixels[0][0]=1; if(y1<ymin) pixels[0][1]=1; if(x1>xmax) pixels[0][2]=1; if(x1<xmin) pixels[0][3]=1; if(y2>ymax) pixels[1][0]=1; if(y2<ymin) pixels[1][1]=1; if(x2>xmax) pixels[1][2]=1; if(x2<xmin) pixels[1][3]=1; for(j=0;j<4;j++) { if(pixels[0][j]==0&&pixels[1][j]==0) continue; if(pixels[0][j]==1&&pixels[1][j]==1) { f1=3; break; } f1=2; } switch(f1) { case 1:line(320+x1,240-y1,320+x2,240-y2); break; case 3:printf(\n\n\t\LINE IS NOT VISIBLE..\"); break; case 2:m=(y2-y1)/(x2-x1); xn1=x1; yn1=y1; xn2=x2; yn2=y2; if(pixels[0][0]==1) { xn1=x1+(ymax-y1)/m; yn1=ymax;3 } if(pixels[0][1]==1) { xn1=x1+(ymin-y1)/m; yn1=ymin; } if(pixels[0][2]==1) { yn1=y1+(xmax-x1)*m; xn1=xmax; } if(pixels[0][3]==1) { yn1=y1+(xmin-x1)*m; xn1=xmin; } if(pixels[1][0]==1) { xn2=x2+(ymax-y2)/m; yn2=ymax; } if(pixels[1][1]==1) { xn2=x2+(ymin-y2)/m; yn2=ymin; } if(pixels[1][2]==1) { yn2=y2+(xmax-x2)*m; xn2=xmax; } if(pixels[1][3]==1) { yn2=y2+(xmin-x2)*m; xn2=xmin; } line(320+xn1,240-yn1,320+xn2,240-yn2); break; } } void cohen() { clearviewport(); line(320+xmin,240-ymin,320+xmin,240-ymax); line(320+xmin,240-ymax,320+xmax,240-ymax); line(320+xmax,240-ymax,320+xmax,240-ymin); line(320+xmax,240-ymin,320+xmin,240-ymin); line(320+x1,240-y1,320+x2,240-y2); getch(); cleardevice(); line(320+xmin,240-ymin,320+xmin,240-ymax); line(320+xmin,240-ymax,320+xmax,240-ymax); line(320+xmax,240-ymax,320+xmax,240-ymin); line(320+xmax,240-ymin,320+xmin,240-ymin); su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax); getch(); } void main() { int gd=DETECT,gm,i,j; initgraph(&gd,&gm,..\\bgi); printf(\n\n\t\t\Enter the coordinate of the Clipping Window\"); printf(\n\n\t\t\Enter X(min),Y(min)\:=); scanf(%d%d,&xmin,&ymin); printf(\n\n\t\t\Enter X(max),Y(max)\:=); scanf(%d%d,&xmax,&ymax); printf(\n\n\t\tEnter the coordinates of the Line); printf(\n\n\t\t Enter X(1) & Y(1):); scanf(%d%d,&x1,&y1); printf(\n\n\t\t Enter X(2) & Y(2):); scanf(%d%d,&x2,&y2); clrscr(); cohen(); }
Result
Sutherland Hodgeman Polygon clipping Algorithm was executed successfully.
EX.NO: 7 THREE DIMENSIONAL(3D) TRANSFORMATIONS DATE: Aim: To perform 3D transformations such as translation, rotation and scaling.
ALGORITHM:
STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: display the original object. STEP 4: get translation vertex from user and translate the object to given vertex. STEP 5: display the translated object. STEP 6: get scaling factors from user and scale the object by given values. STEP 7: display the scaled object. STEP 8: get rotation angle from user and rotate the object about x-axis,y-axis and z-axis. STEP 9: display the rotated object.
RESULT: Thus the above program for 3D transformation was successfully executed.
EX.NO: 8 COMPOSITE 3D TRANSFORMATIONS DATE:
AIM: To write a c program for composite 3D transformation.
ALGORITHM:
STEP 1: include the graphics and other header files. STEP 2: initialize graphics mode and graphics driver. STEP 3: display the original object. STEP 4: get translation vertex from user and translate the object to given vertex. STEP 5: display the translated object. STEP 6: get scaling factors from user and scale the object by given values. STEP 7: display the scaled object. STEP 8: get rotation angle from user and rotate the object about x-axis,y-axis and z-axis. STEP 9: display the rotated object.
RESULT: Thus the above program for composite 3D transformation was successfully executed.
EX.NO: DRAWING THREE DIMENSIONAL OBJECTS AND SCENES DATE: Aim: To write program to visualize the Drawing three dimensional objects and Scenes
Functions used:
Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax: line (x1,y1,x2,y2)
initgraph(). This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name. Syntax: Initgraph(gd,gm,path) Algorithm: Step 1. Create a class parallel. Step 2. Create a constructor parallel i).initialize graphics Step 3. Create a function initialize i).draw the x,y,z axis Step 4. Create a function projection i).get the reference angle alpha as j. ii).assign the value of k as 45. iii).compute the following fi=(3.14/180)*k; a1=(3.14/180)*j; z=pz1[1]; i=z/tan(a1); i1=floor(i*cos(fi)); i2=floor(i*sin(fi)); iv).Calculate px3[1]=px1[1]+i1; py3[1]=py1[1]+i2; px3[2]=px1[2]+i1; py3[2]=py1[2]+i2; px3[3]=px1[3]+i1; py3[3]=py1[3]+i2; px3[4]=px1[4]+i1; py3[4]=py1[4]+i2; v).compute the following z=pz1[5]; i=z/tan(a1); i1=floor(i*cos(fi)); i2=floor(i*sin(fi)); vi). calculate px3[5]=px1[5]+i1; py3[5]=py1[5]+i2; px3[6]=px1[6]+i1; py3[6]=py1[6]+i2; px3[7]=px1[7]+i1; py3[7]=py1[7]+i2; px3[8]=px1[8]+i1; py3[8]=py1[8]+i2; vii).compute the values to screen coordinate value. viii).join the coordinate using line function ix).display the projected object.
Result: Thus the program to visualize the Drawing three dimensional objects and Scenes was executed successfully.
EX.NO:10 FRACTAL IMAGES DATE:
Aim:
To write program to draw the Generating Fractal images
Functions used: Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)
Syntax: line (x1,y1,x2,y2)
initgraph(). This function takes thee arguments and they are i).the video driver to be used (gd). ii).the graphics mode (gm). iii).the path name.
Syntax: Initgraph(gd,gm,path)
Algorithm: Step1:Pres() i).Initialise graphics. Step 2: Initialize() i).Draw x,y,z axis ii).Number all axis with regular interval 1 to 8 step 3:Draw-obj i). Get the value of each p6 of 3D object ii).Convert the value to screen coordinate values iii).Join the coordinate using the function. Step 4:Proj() i).Get the reference point in z-axis as j ii).Get the position of the view plane as k iii).Assign the values for Zprp=j and Zrp=k iv).Compute z=pz1[1]; u=(zprp-zvp)/(zprp-z); v).Calculate px3[1]=px1[1]*u; py3[1]=py1[1]*u; px3[2]=px1[2]*u; py3[2]=py1[2]*u; px3[3]=px1[3]*u; py3[3]=py1[3]*u; px3[4]=px1[4]*u; py3[4]=py1[4]*u; vi).Compute z=pz1[5]; u=(zprp-zvp)/(zprp-z); vii).Calculate px3[5]=px1[5]*u; py3[5]=py1[5]*u; px3[6]=px1[6]*u; py3[6]=py1[6]*u; px3[7]=px1[7]*u; py3[7]=py1[7]*u; px3[8]=px1[8]*u; py3[8]=py1[8]*u; viii).Compute the values to screen coordinate value. ix).Join the coordinate using line function. x).Display the projected object.