CG LAB Practical - Final Record - New
CG LAB Practical - Final Record - New
(Deemed to be University)
(Established Under section 3 of UGC act 1956)
Accredited with A+ Grade by NAAC in the Second Cycle
Coimbatore-641021.
NAME :
REG.NO:
KARPAGAM ACADEMY OF HIGHER EDUCATION
(Deemed to be University)
(Established Under section 3 of UGC act 1956)
Accredited with A+ Grade by NAAC in the Second Cycle
Coimbatore-641021.
BONAFIDE CERTIFICATE
REGISTER NUMBER:
STAFF
PAGENO
S.NO DATE NAME OF THE EXPERIMENT SIGNATURE
Aim:
Algorithm:
Else
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step9: x = x + xinc
y = y + yinc
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
intgd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(WHITE);
printf("Enter coordinates for 1st point of line:");
scanf("%d%d",&x0,&y0);
printf("Enter coordinates for 2nd point of line:");
scanf("%d%d",&x1,&y1);
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}
Output:
Result:
Thus the program to draw the line is successfully coded, compiled and then executed successfully.
Ex.No. : 2
Date :
BRESENHAM'S LINE DRAWING ALGORITHM
Aim :
Algorithm:
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2
If x > = xend
Stop.
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1
Step9: Increment x = x + 1
Step11: Go to step 7
Coding:
#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}
Output :
Result:
Thus the program to draw the line is successfully coded, compiled and then executed successfully.
Ex.No. : 3
Date :
BRESENHAM'S CIRCLE DRAWING ALGORITHM
Aim:
Algorithm:
Step4: Calculate d = 3 - 2r
&nbsy= r
If x > = y
Stop
Step7: Plot eight points by using concepts of eight-way symmetry. The center is at (p, q). Current active
pixel is (x, y).
then d = d + 4x + 6
increment x = x + 1
If d ≥ 0
then d = d + 4 (x - y) + 10
increment x = x + 1
decrement y = y - 1
Step9: Go to step 6
Coding:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void EightWaySymmetricPlot(int xc,intyc,intx,int y)
{
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-y+yc,YELLOW);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
}
void BresenhamCircle(int xc,intyc,int r)
{
int x=0,y=r,d=3-(2*r);
EightWaySymmetricPlot(xc,yc,x,y);
while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)-(4*y)+10;
y=y-1;
}
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
}
}
int main(void)
{
/* request auto detection */
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :");
scanf("%d",&r);
BresenhamCircle(xc,yc,r);
getch();
closegraph();
return 0;
}
Output:
Result:
Thus the program to draw the circle is successfully coded, compiled and then executed successfully.
Ex.No. : 4
Date :
CHARACTER GENERATION ALGORITHM
Aim:
Algorithm:
Step 4: The character 'A' will be generated after the loop ends
Coding:
(Zoomed)
Result:
Thus the program to draw the character is successfully coded, compiled and then executed
successfully.
Ex.No. : 5
Date :
POLYGON CLIPPING USING SUTHERLAND–HODGMAN ALGORITHM
Aim:
Algorithm:
Step 4: Check the four possible cases for any given edge of given polygon against current clipping edge e.
Coding:
new_points[new_poly_size][0] = kx;
new_points[new_poly_size][1] = ky;
new_poly_size++;
}
// Case 3: When only second point is outside
else if (i_pos<0 &&k_pos>= 0)
{
//Only point of intersection with edge is added
new_points[new_poly_size][0] = x_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_points[new_poly_size][1] = y_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_poly_size++;
}
// Case 4: When both points are outside
else
{
//No points are added
}
}
// Copying new points into original array
// and changing the no. of vertices
poly_size = new_poly_size;
for (int i = 0; i<poly_size; i++)
{
poly_points[i][0] = new_points[i][0];
poly_points[i][1] = new_points[i][1];
}
}
Output:
(150, 162) (150, 200) (200, 200) (200, 174)
Result:
Thus the program for polygon clipping is successfully coded, compiled and then executed
successfully.
Ex.No. : 6
Date :
TEXT CLIPPING ALGORITHM
Aim:
Algorithm:
c. Text Clipping
Step 4: Do the necessary steps to clip the unwanted portion of the text
Coding:
//text clipping
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define Round(val)((int)(val+.5))
int maxx, maxy, miny, minx;
void main()
{
int gd = DETECT, gm;
void clipping(int xa, int ya, int xb, int y);
int xa, xb, ya, yb;
printf("Enter the window coordination");
//code - works
scanf("%d%d%d%d", &minx, &maxy, &maxx, &miny);
printf("Enter the two string");
scanf("%s%s”, &xa, &ya);
initgraph(&gd, &gm, "");
rectangle(minx, miny, maxx, maxy);
line(xa, ya, xb, yb);
getch();
closegraph();
}
void clipping(int xa, int ya, int xb, int yb)
{
int Dx = xb - xa, Dy = yb - ya, steps, k;
int visible1 = 0, visible2 = 0;
float xin, yin, x = xa, y = ya;
if (abs(Dx) > abs(Dy))
steps = abs(Dx);
else// NO
steps = abs(Dy);
xin = Dx / (float) steps;
yin = Dy / (float) steps;
putpixel(Round(x), Round(y), 2);
for (k = 0; k < steps; k++) {
x += xin;
y += yin;
if ((y >miny&& y <maxx)) {
visible1 = 1;
putpixel(Round(x), Round(y), 2);
} else
visible2 = 1;
}//works
if (visible1 == 0)
outtextxy(20, 200, "completely visible");
if (visible1 == 1 && visible2 == 1)
outtextxy(20, 20, "partialy visible");
}
Output:
Result:
Thus the program to clip the text is succesfully coded, compiled and then executed successfully.
Ex.No. : 7
Date :
LINE CLIPPING ALGORITHM
Aim :
Algorithm:
Step 3: Get the inputs for clipping area and line position
a. Completely invisible
b. Completely visible
c. Partially visible
Coding:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define Round(val)((int)(val+.5))
int maxx, maxy, miny, minx;
void main()
{
int gd = DETECT, gm;
void clipping(int xa, int ya, int xb, int y);
int xa, xb, ya, yb;
printf("Enter the window coordination");
scanf("%d%d%d%d", &minx, &maxy, &maxx, &miny);
printf("Enter the two end points for the line");
scanf("%d%d%d%d", &xa, &ya, &xb, &yb);
initgraph(&gd, &gm, "");
rectangle(minx, miny, maxx, maxy);
line(xa, ya, xb, yb);
getch();
closegraph();
}
void clipping(int xa, int ya, int xb, int yb)
{
int Dx = xb - xa, Dy = yb - ya, steps, k;
int visible1 = 0, visible2 = 0;
float xin, yin, x = xa, y = ya;
if (abs(Dx) > abs(Dy))
steps = abs(Dx);
else
steps = abs(Dy);
xin = Dx / (float) steps;
yin = Dy / (float) steps;
putpixel(Round(x), Round(y), 2);
for (k = 0; k < steps; k++) {
x += xin;
y += yin;
if ((y >miny&& y <maxx)) {
visible1 = 1;
putpixel(Round(x), Round(y), 2);
} else
visible2 = 1;
}
if (visible1 == 0)
outtextxy(20, 200, "completely visible");
if (visible1 == 1 && visible2 == 1)
outtextxy(20, 20, "partialy visible");
if (visible1 == 1 && visible2 == 0)
outtextxy(20, 20, "completely visible");
}
Output:
Result:
Thus the program to clip the line is successfully coded, compiled and then executed successfully.
Ex.No. : 8
Date :
2-D BASIC TRANSFORMATIONS
Aim:
To perform 2-D basic transformations such as translation, rotation and scaling using C program
Algorithm:
Coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void circlemid(int,int,int);
void circlepoint(int,int,int,int);
int r,x1,x2,y1,y2,x3,y3,x4,y4;
int ch;
int x,y;
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
{
cleardevice();
do
{
clrscr();
printf("TRANSLATION\n");
printf("PRESS 1 FOR LINE\n");
printf("PRESS 2 FOR RECTANGALE\n");
printf("PRESS 3 FOR CIRCLE\n");
printf("PRESS ANY KEY TO EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ENTER STARING POINT\n");
scanf("%d%d",&x1,&y1);
printf("ENTER THE ENDPOINT");
scanf("%d%d",&x2,&y2);
setcolor(RED);
line(x1,y1,x2,y2);
printf("ENTER TRANSLATION FACTOR");
scanf("%d%d",&x,&y);
setcolor(RED);
x3 = x1+x;
x4 = x2+x;
y3 = y1+y;
y4 = y2+y;
line(x3,y3,x4,y4);
getch();
break;
case 2:
printf("ENTER STARING POINT\n");
scanf("%d%d",&x1,&y1);
printf("ENTER THE ENDPOINT");
scanf("%d%d",&x2,&y2);
setcolor(RED);
rectangle(x1,y1,x2,y2);
printf("ENTER TRANSLATION FACTOR");
scanf("%d%d",&x,&y);
setcolor(RED);
x3 = x1+x;
x4 = x2+x;
y3 = y1+y;
y4 = y2+y;
rectangle(x3,y3,x4,y4);
getch();
break;
case 3:
printf("ENTER STARING POINT\n");
scanf("%d%d",&x,&y);
printf("Enter radius of circle");
scanf("%d",&r);
setcolor(RED);
circlemid(x,y,r);
printf("ENTER TRANSLATION FACTOR");
scanf("%d%d",&x1,&y1);
x=x+x1;
y=y+y1;
circlemid(x,y,r);
setcolor(RED);
getch();
break;
default:
exit(0);
}
}
while(ch!=0);
getch();
closegraph();
}
}
void circlemid(int xc,intyc,intrd)
{
int x =0;
int y =rd;
int p = 1-rd;
while(x<y)
{
x++;
if(p<0)
p = p+2*x+1;
else
{
y--;
p = p+2*(x-y)+1;
}
circlepoint(xc,yc,x,y);
delay(200);
}
}
void circlepoint(int xc,intyc,intx,int y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-y,yc-x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-x,yc-y,1);
}
Result:
Thus the program to perform the basic operations on 2-D object are successfully coded, compiled
and then executed successfully.
Ex.No. : 9
Date :
3-D BASIC TRANSFORMATIONS
Aim:
To perform 3-D basic transformations such as translation, rotation and scaling using C program
Algorithm:
Coding:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
void draw();
void trans();
void main()
printf("\n\t\t3D Transmission\n\n");
mx = (x1 + x2) / 2;
my = (y1 + y2) / 2;
draw();
getch();
cleardevice();
trans();
getch();
void draw() {
void trans() {
a1 = x1 + x;
a2 = x2 + x;
b1 = y1 + y;
b2 = y2 + y;
setcolor(5);
draw();
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
void draw();
void rotate();
void main()
mx = (x1 + x2) / 2;
my = (y1 + y2) / 2;
draw();
getch();
cleardevice();
rotate();
getch();
void draw() {
void rotate()
{
float t;
scanf("%f", &t);
t = t * (3.14 / 180);
else
setcolor(5);
//draw();
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
void draw();
void scale();
void main() {
mx = (x1 + x2) / 2;
my = (y1 + y2) / 2;
draw();
getch();
cleardevice();
scale();
getch();
void draw() {
void scale() {
a1 = mx + (x1 - mx) * x;
a2 = mx + (x2 - mx) * x;
b1 = my + (y1 - my) * y;
b2 = my + (y2 - my) * y;
setcolor(5);
draw();
Output:
Result:
Thus the program to perform the basic operations on 3-D object are successfully coded, compiled
and then executed successfully.
Ex.No. : 10
Date :
SHEARING AND REFLECTION OF AN OBJECT
Aim:
Algorithmfor Shearing
Step 6. Do the necessary steps to perform shearing operation on the chosen object
Coding:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
int x1,y1,x2,y2,x,y,x3,y3,x4,y4,ch;
void main()
{
int gd = DETECT,gm,errorcode;
initgraph(&gd,&gm,"c:\\tc\\bgi");
errorcode = graphresult();
if(errorcode!=grOk)
{
printf("Graphics error:%s\n",grapherrormsg(errorcode));
printf("press any key to halt:");
getch();
exit(1);
}
do
{
clrscr();
cout<<" #############MAIN-MENU###############\n";
cout<<" SHEARING\n";
cout<<" 1.LINE\n";
cout<<" 2.RECTANGLE\n";
cout<<" 3.TRIANGLE\n";
cout<<"enter your choice:0 for exit:\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the value of line coordinates:";
cin>>x1>>y1>>x2>>y2;
cout<<"enter the value of shearing for x-axis:";
cin>>x;
cout<<"enter the value of shearing for y-axis:";
cin>>y;
cleardevice();
setcolor(4);
line(x1,y1,x2,y2);
cout<<"now hit a key to see shear in x_axis:";
getch();
setcolor(1);
line(x1,y1,x2*x,y2);
cout<<"\nnow hit a key to see shear in y_axis:";
getch();
setcolor(58);
line(x1,y1,x2,y2*y);
getch();
break;
case 2:
cout<<"enter the top left coordinates:";
cin>>x1>>y1;
cout<<"enter the bottom right coordinates:";
cin>>x2>>y2;
cout<<"enter the value of shearing coordinates for x-shear:";
cin>>x;
cout<<"enter the value of shearing coordinates for y-shear:";
cin>>y;
cleardevice();
setcolor(5);
rectangle(x1,y1,x2,y2);
cout<<"now hit a key to see shear in x_axis:";
getch();
setcolor(1);
rectangle(x1,y1,x2+x*y2,y2);
cout<<"\nnow hit a key to see shear in y_axis:";
getch();
setcolor(58);
rectangle(x1,y1,x2,y2+y*x2);
getch();
break;
case 3:
cout<<"enter the coordinates of triangle:\n";
cin>>x1>>y1>>x2>>y2;
cin>>x3>>y3;
cout<<"enter the value of shearing coordinates for x-shear:";
cin>>x;
cout<<"enter the value of shearing coordinates for y-shear:";
cin>>y;
cleardevice();
setcolor(5);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x2,y2);
cout<<"\nnow hit a key to see shear in x_axis:";
getch();
setcolor(10);
line(x1,y1,x2*x,y2);
line(x1,y1,x3*x,y3);
line(x3*x,y3,x2*x,y2);
cout<<"\nnow hit a key to see shear in y_axis:";
getch();
setcolor(1);
line(x1,y1,x2,y2*y);
line(x1,y1,x3,y3*y);
line(x3,y3*y,x2,y2*y);
getch();
break;
case0:break;
default:cout<<"invalid choice";break;
}}while(ch!=0);
closegraph();
getch();
}
Output:
Step 2: Draw a line in graphics to act as a Y-axis, by passing along 4 values as parameters of the line()
function as line(getmaxx()/2, 0, getmaxx()/2, getmaxy()).
Step 3: Draw a line in graphics to act as X-axis by passing along 4 values as parameters of the line() function
as line(0, getmaxy()/2, getmaxx(), getmaxy()/2).
Step 4: Draw an object using the line() function with parameters from set variables.
Step 5: Perform reflection and draw the object along origin using Step 3 of the technique and color it red to
distinguish it from other objects.
Step 6: Perform reflection and draw the object along the X-axis using Step 1 of the technique and color it
cyan to distinguish it from other objects.
Step 7: Perform reflection and draw the object along the Y-axis using Step 2 of the technique and color it
green to distinguish it from other objects.
Output
Result:
Thus the program to perform the shearing and reflection operations on 2-D object are successfully
coded, compiled and then executed successfully.