Cgma Lab Programs
Cgma Lab Programs
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{
float x,y,x1,y1,x2,y2,dx,dy,length;
int gm,gd=DETECT,i;
printf("enter the value of x1:\t");
scanf("%f",&x1);
printf("enter the value of y1:\t");
scanf("%f",&y1);
printf("enter the value of x2:\t");
scanf("%f",&x2);
printf("enter the value of y2:\t");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
length=dx;
}
else
{
length=dy;
}
dx=(x2-x1)/length;
dy=(y2-y1)/length;
x=x1+0.5;
y=y1+0.5;
i=1;
while(i<=length)
{
putpixel(x,y,15);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
main()
{ float x,y,r,p;
int gm,gd=DETECT;
printf("Enter the radius: ");
scanf("%f",&r);
x=0;
y=r;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
p=1-r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,15);
putpixel(200+x,200-y,15);
putpixel(200+y,200-x,15);
putpixel(200-x,200-y,15);
putpixel(200-y,200-x,15);
putpixel(200-x,200+y,15);
putpixel(200-y,200+x,15);
if(p<0)
{
++x;
p=p+(2*x)+1;
}
else
{
++x;
--y;
p=p+(2*x)-(2*y)+1;
}
delay(100);
}while(x<y);
getch();
closegraph();
}
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
void main()
{ int gm,gd=DETECT;
int Rx,Ry,P10,x,y,a,b,P20;
printf("Enter Value of Rx and Ry: ");
scanf("%d %d",&Rx,&Ry);
x=0;
y=Ry;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
P10=(Ry*Ry)-(Rx*Rx)-(Rx*Rx*Ry)+(0.25*Rx*Rx);
do
{
putpixel(200+x,200+y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
putpixel(200-x,200-y,15);
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
if(P10<0)
{
x++;
P10=P10+a+(Ry*Ry);
}
else
{
x++;
y--;
P10=P10+a+(Ry*Ry)-b;
}
delay(100);
}while(a>b);
P20=((Ry*Ry)*((x+0.5)*(x+0.5)))+((Rx*Rx)*((y-1)*(y-1)))-((Rx*Rx)*(Ry*Ry));
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
do
{
putpixel(200+x,200+y,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200+y,15);
putpixel(200-x,200-y,15);
a=(2*Ry*Ry*x)+(2*Ry*Ry);
b=(2*Rx*Rx*y)+(2*Rx*Rx);
if(P20>0)
{
y--;
P20=P20-b+(Rx*Rx);
}
else
{
x++;
y--;
P20=P20-b+a+(Rx*Rx);
}
delay(100);
}while(y>0);
getch();
closegraph();
}
Problem 5: WAP to design national flag using set of lines generated by DDA or
Bresenham line drawing algorithm.
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
// Driver Code
void main()
{
// Initialize of gdriver with
// DETECT macros
initgraph(&gd, &gm, "C:\\turboc3\\bgi");
// Create a Circle
circle(450, 190, 30);
floodfill(452, 188, 15);
LAB EXERCISE #2
return code;
}
while (true) {
if ((code1 == 0) && (code2 == 0)) {
// If both endpoints lie within rectangle
accept = true;
break;
}
else if (code1 & code2) {
// If both endpoints are outside rectangle,
// in same region
break;
}
else {
// Some segment of line lies within the
// rectangle
int code_out;
double x, y;
// Driver code
int main()
{
// First Line segment
// P11 = (5, 5), P12 = (7, 7)
cohenSutherlandClip(5, 5, 7, 7);
return 0;
}
#include <SFML/Graphics.hpp>
#include <iostream>
#include <utility>
#include <vector>
// Calculating P1 - P0
pair<int, int> P1_P0
= make_pair(line[1].first - line[0].first,
line[1].second - line[0].second);
if (denominator[i] > 0)
tE.push_back(t[i]);
else
tL.push_back(t[i]);
}
return newPair;
}
// Driver code
int main()
{
// Driver code
int main()
{
int XY[6][2] = {{10, 10}, {-10, 10}, {400, 100},
{100, 400}, {400, 400}, {100, 40}};
LAB EXERCISE #3
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the starting point of line segment:");
scanf("%d %d",&x1,&y1);
printf("Enter the ending point of line segment:");
scanf("%d %d",&x2,&y2);
printf("Enter translation distances tx,ty:\n");
scanf("%d%d",&tx,&ty);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=y2+ty;
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x4+2,y4+2,"Line after translation");
getch();
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,sx,sy,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the starting point coordinates:");
scanf("%f %f",&x1,&y1);
printf("Enter the ending point coordinates:");
scanf("%f %f",&x2,&y2);
printf("Enter scaling factors sx,sy:\n");
scanf("%f%f",&sx,&sy);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
x3=x1*sx;
y3=y1*sy;
x4=x2*sx;
y4=y2*sy;
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after scaling");
getch();
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,x3,y3,x4,y4,a,t;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter coordinates of starting point:\n");
scanf("%f%f",&x1,&y1);
printf("Enter coordinates of ending point\n");
scanf("%f%f",&x2,&y2);
printf("Enter angle for rotation\n");
scanf("%f",&a);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
t=a*(3.14/180);
x3=(x1*cos(t))-(y1*sin(t));
y3=(x1*sin(t))+(y1*cos(t));
x4=(x2*cos(t))-(y2*sin(t));
y4=(x2*sin(t))+(y2*cos(t));
setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after rotation");
getch();
}
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
float shx,shy;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter shear factor shy along y-axis :");
scanf("%f",­);
line(100,10,200,10);
line(200,10,200,200);
line(200,200,100,200);
line(100,200,100,10);
printf("Y-shear");
setcolor(12);
line(100,10+(shy*100),200,10+(shy*200));
line(200,10+(shy*200),200,200+(shy*200));
line(200,200+(shy*200),100,200+(shy*100));
line(100,200+(shy*100),100,10+(shy*100));
getch();
closegraph();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void rotate();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n3D Transformation Rotating\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw(); getch();
cleardevice();
rotate();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void rotate()
{
float t;
int a1,b1,a2,b2,dep;
printf("Enter the angle to rotate=");
scanf("%f",&t);
t=t*(3.14/180);
a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t);
a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t);
b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t);
b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t);
if(a2>a1)
dep=(a2-a1)/4;
else
dep=(a1-a2)/4;
bar3d(a1,b1,a2,b2,dep,1); setcolor(5);
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void scale();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Scaling\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
scale();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void scale()
{
int x,y,a1,a2,b1,b2,dep;
printf("\n\n Enter scaling Factors:");
scanf("%d%d",&x,&y);
a1=mx+(x1-mx)*x;
a2=mx+(x2-mx)*x;
b1=my+(y1-my)*y;
b2=my+(y2-my)*y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void trans();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Translation\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
trans();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void trans()
{
int a1,a2,b1,b2,dep,x,y;
printf("\n Enter the Translation Distances:");
scanf("%d%d",&x,&y);
a1=x1+x;
a2=x2+x;
b1=y1+y;
b2=y2+y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
LAB EXERCISE #4
Program 1: WAP to draw 2D Bezier Curves.
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
void bezier (int x[4], int y[4])
{
int gd = DETECT, gm;
int i;
double t;
initgraph (&gd, &gm, "C:\\TurboC3\\BGI");
for (t = 0.0; t < 1.0; t += 0.0005)
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +
3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];
double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +
3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
for (i=0; i<4; i++)
putpixel (x[i], y[i], YELLOW);
getch();
closegraph();
return;
}
void main()
{
int x[4], y[4];
int i;
printf ("Enter the x- and y-coordinates of the four control points.\n");
for (i=0; i<4; i++)
scanf ("%d%d", &x[i], &y[i]);
bezier (x, y);
}
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<SDL2/SDL.h>
int i = 0 ;
int x[4] , y[4] , flagDrawn = 0 ;
while (!done)
{
SDL_Event event;
int j ;
LAB EXERCISE #5
Program 1: WAP for filling a given rectangle object with color using flood fill
algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void floodFill(int x,int y,int ncolor,int ocolor)
{
if(getpixel(x,y)==ocolor)
{
putpixel(x,y,ncolor);
floodFill(x+1,y,ncolor,ocolor);
floodFill(x-1,y,ncolor,ocolor);
floodFill(x,y+1,ncolor,ocolor);
floodFill(x,y-1,ncolor,ocolor);
}
delay(1);
}
void main()
{
int x,y,ncolor=BLUE,ocolor=WHITE;
int midx,midy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C://tc//bgi");
cleardevice();
printf("enter seed point:");
scanf("%d%d",&x,&y);
midx=getmaxx()/2;
midy=getmaxy()/2;
setbkcolor(RED);
setpalette(ocolor,GREEN);
fillellipse(midx,midy,50,25);
fillellipse(midx+100,midy+100,50,25);
floodFill(x,y,ncolor,ocolor);
getch();
closegraph();
}
boundaryFill(x,y);
getch();
closegraph();
}
Program 3: WAP to illustrate the use of setfill function.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph();
}