Professional Documents
Culture Documents
Puneet Graphics
Puneet Graphics
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 2
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 3
11) Draw a rectangle using Bresenhams algorithm for line.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gdriver =DETECT,gmode;
int x1,y1,x2,y2,c=15;
initgraph(&gdriver,&gmode,"");
line(100,100,400,100);
line(100,100,100,300);
line(100,300,400,300);
line(400,100,400,300);
getch();
closegraph();
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 4
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 5
12) Draw a pentagon using DDA algorithm for line
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void dda(int x1,int y1,int x2,int y2)
{
float dx,dy,len,xi,yi,i=1;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>=abs(dy))
len=abs(dx);
else
len=abs(dy);
xi=x1+0.5;
yi=y1+0.5;
putpixel(xi,yi,WHITE);
dx=(dx)/len;
dy=(dy)/len;
while(i<=len)
{
xi=xi+dx;
yi=yi+dy;
putpixel(xi,yi,WHITE);
i++;
}
}
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
dda(50,150,100,100);
dda(100,100,150,150);
dda(150,150,125,200);
dda(125,200,75,200);
dda(75,200,50,150);
getch();
closegraph();
return 0;
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 6
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 7
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 8
13) Implement a circle using direct equation of the circle
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void plotcircle(int,int,int,int);
void main()
{
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 9
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 10
14)Draw an ellipse using trigonometric functions
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
int gd=DETECT,gm;
double a=100,b=30,x,y,theta;
initgraph(&gd,&gm," ");
cleardevice();
for(theta =0;theta<=360;theta++)
{
x = 200+a*cos((theta*3.141)/180);
y = 200+b*sin((theta*3.141)/180);
putpixel(x,y,YELLOW);
}
getch();
closegraph();
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 11
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 12
15) Implement Mid Point Algorithm for circle
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void mcircle(int xc,int yc,int r);
void drawcircle(int xc,int yc,int x,int y);
void main()
{
int gdriver =DETECT,gmode;
initgraph(&gdriver,&gmode,"");
int xc,yc,r;
xc=200;
yc=200;
r= 80;
mcircle(xc,yc,r);
getch();
closegraph();
}
void mcircle(int xc,int yc,int r)
{
int x,y,p;
x=0;
y=r;
p=1-r;
while(x < y)
{
drawcircle(xc,yc,x,y);
if (p<0)
{
p=p+2*x+3;
x=x+1;
}
else
{
p=p+2*x-2*y+5;
x=x+1;
y=y-1;
}
}
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 13
void drawcircle(int xc,int yc,int x,int y)
{
int c = 15;
putpixel(xc+x,yc+y,c);
putpixel(xc+x,yc-y,c);
putpixel(xc-x,yc-y,c);
putpixel(xc-x,yc+y,c);
putpixel(xc+y,yc+x,c);
putpixel(xc+y,yc-x,c);
putpixel(xc-y,yc-x,c);
putpixel(xc-y,yc+x,c);
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 14
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 15
16)Use the mouse functionality in c++
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
union REGS in,out;
int callmouse()
{
in.x.ax=1;
int86(51,&in,&out);
return 1;
}
int mousehide()
{
in.x.ax=2;
int86(51,&in,&out);
return 1;
}
int main()
{
int x,y,cl,a,b;
clrscr();
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 16
int g=DETECT,m;
initgraph(&g,&m,"c:\tc\bgi");
a=100;
b=400;
setposi(a,b);
callmouse();
do
{
mouseposi(x,y,cl);
gotoxy(10,9);
printf("\n\t Mouse Position is: %3d,%3d",x,y);
printf("\n\t Click: %d",cl);
printf("\n\t Press any key to hide the mouse");
}
while(!kbhit());
getch();
mousehide();
printf("\n\t Press any key to exit");
getch();
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 17
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 18
17)To draw 3D object using Palettes
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = VGA, gmode = VGAHI, errorcode;
struct palettetype pal;
int i, ht, y, xmax;
setcolor(15);
rectangle(200,200,300,300);
line(200,200,350,100);
line(300,200,450,100);
line(350,100,450,100);
line(450,100, 450,200 );
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 19
line(450,200,300,300);
// setcolr(15);
setfillstyle(SOLID_FILL, 3);
floodfill(250,250,15);
setfillstyle(SOLID_FILL, 7);
floodfill(350,150,15);
setfillstyle(SOLID_FILL, 9);
floodfill(330,230,15);
/* clean up */
getch();
closegraph();
return 0;
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 20
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 21
18)Rotating a polygon
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void drawp(float v[3][3]);
void vertices(float r[3][3]);
void rotation(float r[3][3],int angle);
void multiply(float a[3][3],float b[3][3],float res[3][3]);
void draw(float d[3][3]);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float
r[3][3],s[3][3],v[3][3],res[3][3],t[3][3],tn[3][3],res1[3][3],res2[3][3],res3[3][3],res4[3][3];
vertices(v);
drawp(v);
rotation(r,10);
multiply(v,r,res);
setcolor(5);
drawp(res);
getch();
closegraph();
}
void vertices(float v[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
v[i][j]=0;
}
v[i][2]=1;
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 22
v[0][0]=100;
v[0][1]=100;
v[1][0]=50;
v[1][1]=150;
v[2][0]=200;
v[2][1]=200;
}
void translate(float v[3][3],int tx,int ty)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
v[i][j]=0;
if (i==j)
{ v[i][j]=1;}
}
}
v[2][0]=tx;
v[2][1]=ty;
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void dispmat(float v[3][3]);
void vertices(float v[3][3]);
void rotate(float rt[3][3], int th);
void mult(float a[3][3],float b[3][3], float c[3][3]);
void drawp(float v[3][3]);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float v[3][3],r[3][3],rt[3][3];
setcolor(5);
vertices(v);
drawp(v);
rotate(rt,10);
mult(v,rt,r);
getch();
setcolor(4);
drawp(r);
getch();
closegraph();
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 26
v[0][1] = 300;
v[1][0] = 250;
v[1][1] = 350;
v[2][0] = 400;
v[2][1] = 300;
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 28
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 29
20)Scale and then rotate the polygon about origin
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void drawp(float v[3][3]);
void vertices(float r[3][3]);
void rotation(float r[3][3],int angle);
void multiply(float a[3][3],float b[3][3],float res[3][3]);
void draw(float d[3][3]);
void scaling(float s2[3][3],int sx,int sy);
void translate(float v[3][3],int tx,int ty);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float
r[3][3],s[3][3],v[3][3],res[3][3],t[3][3],tn[3][3],res1[3][3],res2[3][3],res3[3][3],res4[3][3];
vertices(v);
drawp(v);
scaling(s,1,2);
rotation(r,10);
multiply(v,s,res);
multiply(res,r,res1);
setcolor(5);
drawp(res1);
getch();
closegraph();
}
void vertices(float v[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
v[i][j]=0;
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 30
v[i][2]=1;
}
v[0][0]=100;
v[0][1]=100;
v[1][0]=50;
v[1][1]=150;
v[2][0]=200;
v[2][1]=200;
}
void translate(float v[3][3],int tx,int ty)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
v[i][j]=0;
if (i==j)
{ v[i][j]=1;}
}
}
v[2][0]=tx;
v[2][1]=ty;
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 32
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 33
21) Scale then rotate the polygon about fixed point,
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void drawp(float v[3][3]);
void vertices(float r[3][3]);
void rotation(float r[3][3],int angle);
void multiply(float a[3][3],float b[3][3],float res[3][3]);
void draw(float d[3][3]);
void scaling(float s2[3][3],int sx,int sy);
void translate(float v[3][3],int tx,int ty);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float
r[3][3],s[3][3],v[3][3],res[3][3],t[3][3],tn[3][3],res1[3][3],res2[3][3],res3[3][3],res4[3][3];
vertices(v);
// cout<<"Vertics Matrix\n";
drawp(v);
// cout<<"Translate Matrix\n";
translate(t,-300,-300);
// drawp(t);
// cout<<"Negative Scaling Matrix\n";
scaling(s,-1,-2);
// drawp(s);
// cout<<"Rotation Matrix\n";
rotation(r,25);
// drawp(r);
// cout<<"Mult res1= v* -t \n ";
multiply(v,t,res);
// draw(res);
// cout<<"Mult res=res1*(-sx,-sy) \n";
multiply(res,s,res1);
// draw(res1);
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 34
// cout<<"Mult res2=res1*r \n";
multiply(res1,r,res2);
// draw(res2);
// cout<<"Scaling Matrix\n";
scaling(s,1,2);
// draw(s);
// cout<<"Translate Matrix\n";
translate(t,300,300);
// draw(t);
getch();
closegraph();
}
void vertices(float v[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
v[i][j]=0;
}
v[i][2]=1;
}
v[0][0]=300;
v[0][1]=300;
v[1][0]=250;
v[1][1]=350;
v[2][0]=400;
v[2][1]=300;
}
void translate(float v[3][3],int tx,int ty)
{
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 35
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
v[i][j]=0;
if (i==j)
{ v[i][j]=1;}
}
}
v[2][0]=tx;
v[2][1]=ty;
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 37
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 38
22) Implemen t Bresenhams algorithm for Circle.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
r=100;
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if(p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
getch();
closegraph();
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 39
}
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 40
23) Draw a polygon using OpenGl.
/*
* GL01Hello.cpp: Test OpenGL C/C++ Setup
*/
#include <windows.h> // For MS Windows
#include <GL/glut.h> // GLUT, includes glu.h and gl.h
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 41
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 42
MCA 4th sem /MCA 406 / CRNo. 186008 / URNo :1811443 Page 43