Graphics Ass Docs
Graphics Ass Docs
NAME IDNO
#include <graphics.h>
#include <iostream>
#include <conio.h>
#include <math.h>
int p = 2 * dy - dx;
x = xEnd;
y = yEnd;
xEnd = x0;
}
else
x = x0;
y = y0;
putpixel(x, y, WHITE);
// cout << -1 << " " << p << " " << x << " " << y << "\n";
int cnt = 0;
x++;
if (p < 0)
p += twoDy;
else
y++;
p += twoDyMinusDxh;
putpixel(x, y, WHITE);
// cout <<cnt++ <<" " <<p<<" " << x << " " << y << "\n";
int main()
{
getch();
closegraph();
return 0;
For DDA:
#include<graphics.h>
#include<iostream>
#include<conio.h>
#include<math.h>
for(int i=0;i<steps;i++){
x += xinc;
y += yinc;
// cout << round (x) <<" " << round(y) << "\n";
int main(){
getch();
closegraph();
return 0;
2, Implement the animation of any primitives using OpenGL library by taking input from console
line (from user)
#include<stdio.h>
#include <gl/glut.h>
int fullscreen = 0;
int mouseDown = 0;
void drawBox()
glBegin(GL_QUADS);
// FRONT
// BACK
glVertex3f(-0.5f, -0.5f, -0.5f);
// LEFT
// RIGHT
// TOP
// BOTTOM
glEnd();
int init(void)
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);
return 1;
}
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(
drawBox();
glFlush();
glutSwapBuffers();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(grow_shrink, resize_f * w / h, resize_f, 100 * resize_f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
void idle(void)
if (!mouseDown)
xrot += 0.3f;
yrot += 0.4f;
glutPostRedisplay();
//{
//}
switch(key)
case 27 :
exit(1);
break;
case 'w':
case 'W':
tra_x += 0.1f;
break;
case 's':
case 'S':
tra_x -= 0.1f;
break;
case 'a':
case 'A':
tra_z -= 0.1f;
break;
case 'd':
case 'D':
tra_z += 0.1f;
break;
case 'u':
case 'U':
xrot += 1.0f;
yrot += 1.0f;
xdiff += 1.0f;
ydiff += 1.0f;
break;
case 'y':
case 'Y':
xrot -= 1.0f;
yrot -= 1.0f;
xdiff += 1.0f;
ydiff += 1.0f;
break;
case 'h':
case 'H':
mySpecialFunction(key, x, y);
break;
case 'Z':
case 'z':
grow_shrink--;
resize(500, 500);
break;
case 'X':
case 'x':
grow_shrink++;
resize(500, 500);
break;
glutPostRedisplay();
}
void specialKeyboard(int key, int x, int y)
if (key == GLUT_KEY_F1)
fullscreen = !fullscreen;
if (fullscreen)
glutFullScreen();
else
glutReshapeWindow(500, 500);
glutPositionWindow(50, 50);
mouseDown = 1;
xdiff = x - yrot;
ydiff = -y + xrot;
}
else
mouseDown = 0;
if (mouseDown)
yrot = x - xdiff;
xrot = y + ydiff;
glutPostRedisplay();
glutInit(&argc, argv);
glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeyboard);
glutMouseFunc(mouse);
glutMotionFunc(mouseMotion);
glutReshapeFunc(resize);
//glutIdleFunc(idle);
if (!init())
return 1;
glutMainLoop();
return 0;
3, Draw line, circle, arc, triangle, rectangle, polygon, sphere and do all the transformation of it
(translate, rotate, scale) using c++ graphics code.
For line:
#include<iostream>
#include<graphics.h>
#include<math.h>
class transform
{
public:
int m,a[20][20],c[20][20];
int i,j,k;
public:
void object();
void accept();
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
c[i][j]=0;
for(int k=0;k<m;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
};
void transform::object()
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,NULL);
line(300,0,300,600);
line(0,300,600,300);
for( i=0;i<m-1;i++)
line(300+a[i][0],300-a[i][1],300+a[i+1][0],300-a[i+1][1]);
line(300+a[0][0],300-a[0][1],300+a[i][0],300-a[i][1]);
for( i=0;i<m-1;i++)
line(300+c[i][0],300-c[i][1],300+c[i+1][0],300-c[i+1][1]);
line(300+c[0][0],300-c[0][1],300+c[i][0],300-c[i][1]);
int temp;
closegraph();
void transform::accept()
cout<<"\n";
cin>>m;
cout<<"\nEnter The Coordinates :";
for(int i=0;i<m;i++)
for(int j=0;j<3;j++)
if(j>=2)
a[i][j]=1;
else
cin>>a[i][j];
int main()
int ch,tx,ty,sx,sy;
float deg,theta,b[20][20];
transform t;
t.accept();
cout<<"\n1.Translation"
"\n2.Scaling"
"\n3.Rotation";
cin>>ch;
switch(ch)
{
cin>>tx>>ty;
b[0][0]=b[2][2]=b[1][1]=1;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=tx;
b[2][1]=ty;
t * b;
t.object();
break;
cin>>sx>>sy;
b[0][0]=sx;
b[1][1]=sy;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=b[2][1]=0;
b[2][2] = 1;
t * b;
t.object();
break;
cin>>deg;
theta=deg*(3.14/100);
b[0][0]=b[1][1]=cos(theta);
b[0][1]=sin(theta);
b[1][0]=sin(-theta);
b[0][2]=b[1][2]=b[2][0]=b[2][1]=0;
b[2][2]=1;
t * b;
t.object();
break;
default:
cout<<"\nInvalid choice";
getch();
return 0;
B,for rectangle:
#include<iostream>
#include<graphics.h>
#include<math.h>
int gd=DETECT,gm,s;
initgraph(&gd,&gm,(char*)"");
cout<<"1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing "<<endl;
cout<<"Selection:";
cin>>s;
switch(s)
case 1:
{ int x1=200,y1=150,x2=300,y2=250;
int tx=50,ty=50;
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(4);
rectangle(x1+tx,y1+ty,x2+tx,y2+ty);
getch();
break;
case 2:
{ long x1=200,y1=200,x2=300,y2=300;
double a;
rectangle(x1,y1,x2,y2);
cout<<"Angle of rotation:";
cin>>a;
a=(a*3.14)/180;
long xr=x1+((x2-x1)*cos(a)-(y2-y1)*sin(a));
long yr=y1+((x2-x1)*sin(a)+(y2-y1)*cos(a));
setcolor(2);
rectangle(x1,y1,xr,yr);
getch();
break;
case 3:
int x1=30,y1=30,x2=70,y2=70,y=2,x=2;
cout<<"Before scaling"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
cout<<"After scaling"<<endl;
setcolor(10);
rectangle(x1*x,y1*y,x2*x,y2*y);
getch();
break;
case 4:
{
int x1=200,y1=300,x2=500,y2=300,x3=350,y3=400;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
setcolor(5);
line(x1,-y1+500,x2,-y2+500);
line(x1,-y1+500,x3,-y3+500);
line(x2,-y2+500,x3,-y3+500);
getch();
break;
case 5:
int x1=400,y1=100,x2=600,y2=100,x3=400,y3=200,x4=600,y4=200,shx=2;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);
x2=x2+shx*y2;
x3=x3+shx*y3;
x4=x4+shx*y4;
setcolor(13);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);
getch();
default:
cout<<"Invalid Selection"<<endl;
break;
closegraph();
return 0;
A,rectangle:
#include<stdio.h>
#include<math.h>
#include<graphics.h>
char s[100];
int x,y;
scanf("%d%d",&x,&y);
setcolor(GREEN);
sprintf(s,"Translated Rectangle");
outtextxy(80,100,s);
outtextxy(80,110,s);
rectangle(x1+x,y1+y,x2+x,y2+y);
return;
int t,mx,my,rx1,ry1,rx2,ry2,rx3,ry3,rx4,ry4;
float a;
scanf("%d",&t);
setcolor(YELLOW);
sprintf(s,"Rotated Rectangle");
outtextxy(80,390,s);
sprintf(s,"At %d Degree",t);
outtextxy(90,400,s);
while(t>90)
t = t - 90;
a = (float)t*3.14/180;
mx = (x1+x2)/2;
my = (y1+y2)/2;
circle(mx,my,2);
line(rx2,ry2,rx4,ry4);
line(rx3,ry3,rx2,ry2);
line(rx4,ry4,rx1,ry1);
return;
int mx,my,sx,sy;
scanf("%d%d",&sx,&sy);
setcolor(GREEN);
sprintf(s,"Scaled Rectangle");
outtextxy(300,400,s);
outtextxy(300,410,s);
mx = abs(x2 - x1)*sx/4;
my = abs(y2 - y1)*sy/4;
rectangle(x1-mx,y1-my,x2+mx,y2+my);
return;
int main()
{
int x1,x2,y1,y2,gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
sprintf(s,"Orignal Rectangle");
outtextxy(300,100,s);
line(0,250,500,250);
line(250,0,250,500);
setcolor(RED);
x1 = 300;
y1 = 150;
x2 = 400;
y2 = 200;
rectangle(x1,y1,x2,y2);
x1 = 100;
y1 = 150;
x2 = 200;
y2 = 200;
rectangle(x1,y1,x2,y2);
translation(x1,y1,x2,y2);
x1 = 100;
y1 = 350;
x2 = 200;
y2 = 300;
setcolor(RED);
rectangle(x1,y1,x2,y2);
circle((x1+x2)/2,(y1+y2)/2,2);
rotation(x1,y1,x2,y2);
x1 = 300;
y1 = 300;
x2 = 400;
y2 = 350;
setcolor(RED);
rectangle(x1,y1,x2,y2);
scaling(x1,y1,x2,y2);
getch();
return 0;
C,For triangle:
#include<iostream>
#include<graphics.h>
#include<math.h>
class transform
public:
int m,a[20][20],c[20][20];
int i,j,k;
public:
void object();
void accept();
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
c[i][j]=0;
for(int k=0;k<m;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
};
void transform::object()
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,NULL);
line(300,0,300,600);
line(0,300,600,300);
for( i=0;i<m-1;i++)
line(300+a[i][0],300-a[i][1],300+a[i+1][0],300-a[i+1][1]);
line(300+a[0][0],300-a[0][1],300+a[i][0],300-a[i][1]);
for( i=0;i<m-1;i++)
line(300+c[i][0],300-c[i][1],300+c[i+1][0],300-c[i+1][1]);
line(300+c[0][0],300-c[0][1],300+c[i][0],300-c[i][1]);
int temp;
closegraph();
void transform::accept()
cout<<"\n";
cin>>m;
for(int i=0;i<m;i++)
{
for(int j=0;j<3;j++)
if(j>=2)
a[i][j]=1;
else
cin>>a[i][j];
int main()
int ch,tx,ty,sx,sy;
float deg,theta,b[20][20];
transform t;
t.accept();
cout<<"\n1.Translation"
"\n2.Scaling"
"\n3.Rotation";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nTRANSLATION OPERATION\n";
cin>>tx>>ty;
b[0][0]=b[2][2]=b[1][1]=1;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=tx;
b[2][1]=ty;
t * b;
t.object();
break;
cin>>sx>>sy;
b[0][0]=sx;
b[1][1]=sy;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=b[2][1]=0;
b[2][2] = 1;
t * b;
t.object();
break;
cin>>deg;
theta=deg*(3.14/100);
b[0][0]=b[1][1]=cos(theta);
b[0][1]=sin(theta);
b[1][0]=sin(-theta);
b[0][2]=b[1][2]=b[2][0]=b[2][1]=0;
b[2][2]=1;
t * b;
t.object();
break;
default:
cout<<"\nInvalid choice";
getch();
return 0;
CIRCLE:
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
int main()
int gd=DETECT,gm,s;
initgraph(&gd,&gm,(char*)"");
cout<<"1.Translation\n2.Rotation\n3.Scaling "<<endl;
cout<<"Selection:";
cin>>s;
switch(s)
case 1:
{ int x1=200,y1=150,r=50;
int tx=50,ty=50;
setcolor(3);
circle(x1,y1,r);
setcolor(4);
circle(x1+tx,y1+ty,r);
getch();
break;
case 2:
{ long x1=200,y1=200,r=50;
double a;
setcolor(3);
circle(x1,y1,r);
cout<<"Angle of rotation:";
cin>>a;
a=(a*3.14)/180;
long xr=x1+((x1)*cos(a)-(y1)*sin(a));
long yr=y1+((x1)*sin(a)+(y1)*cos(a));
setcolor(2);
circle(x1+xr,y1+yr,r);
getch();
break;
case 3:
int x1=30,y1=30,r=20,x=2,y=2;
cout<<"Before scaling"<<endl;
setcolor(3);
circle(x1,y1,r);
cout<<"After scaling"<<endl;
setcolor(10);
circle(x1,y1,r);
getch();
default:
cout<<"Invalid Selection"<<endl;
break;
closegraph();
return 0;
For arc:
#include<iostream>
#include<graphics.h>
#include<math.h>
int main()
int gd=DETECT,gm,s;
int x1,y1,tx,ty,r,x,y,a1,a2;
long xr,yr;
initgraph(&gd,&gm,(char*)"");
cin>>x1>>y1;
cout<<"enter the angle one and two"<<endl;
cin>>a1>>a2;
cin>>r;
cout<<"1.Translation\n2.Rotation\n3.Scaling "<<endl;
cout<<"Selection:";
cin>>s;
switch(s)
case 1:
cin>>tx>>ty;
setcolor(3);
arc(x1,y1,a1,a2,r);
setcolor(4);
arc(x1+tx,y1+ty,a1,a2,r);
getch();
break;
case 2:
double a;
cout<<"arc before rotation"<<endl;
setcolor(3);
arc(x1,y1,a1,a2,r);
cout<<"Angle of rotation:";
cin>>a;
a=(a*3.14)/180;
xr=x1+((x1)*cos(a)-(y1)*sin(a));
yr=y1+((x1)*sin(a)+(y1)*cos(a));
setcolor(2);
arc(x1+xr,y1+yr,a1,a2,r);
getch();
break;
case 3:
cin>>x>>y;
setcolor(3);
arc(x1,y1,a1,a2,r);
setcolor(10);
arc(x1*x,y1*y,a1,a2,r*2);
getch();
default:
cout<<"Invalid Selection"<<endl;
break;
closegraph();
return 0;