Sunteți pe pagina 1din 38

Programming Laboratory (210251)

Index
Batch: C1 & C2
Title

Date
Group A

1.

Writing a C++ Program to emulate CPU Architecture (Central


Bus) Develop register, ALU level GUI to display results.

2.

Writing a C++ class for displaying pixel or point on the screen.

3.

Write a C++ class for a Line drawing method using overloading


DDA and Bresenhams Algorithms, inheriting the pixel or point.

4.

Write a C++ class for a circle drawing inheriting line class.


Group B

1.

Write a program in C++ to draw a line with line style (Thick,


Thin, Dotted).

2.

Write a program in C++ to draw a circle of desired radius.

3.

Write a C++ program to draw convex polygons (Square,


Rectangle, Triangle).

4.

Write a program in C++ to test that given point is inside the


polygon.

5.

Write a C++ program to fill polygon using scan line algorithm.

6.

Write a Java program to draw a line with line style (Thick, Thin,
Dotted).
Group C

1.

Use Maya to draw a Bouncing ball animation.

Group A

Sign

Programming Laboratory (210251)

CPU Architecture
Title: Writing a C++ Program to emulate CPU Architecture (Central Bus) Develop register, ALU
level GUI to display results.
Aim: To understand the working of CPU architecture, perform some arithmetic operation using
ALU and display results.
C++ code:
#include<string.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<stdio.h>
class computer
{
int c1;
int j;
char n1[5],n2[5];
char *d1;
int a1,b1;
char a[5];
int poly[20];
public:
computer()
{ j=0;
}
void biu();
void alu();
void flags();
void registers();
void segments();
void buses();
void exp1();
void display();
};
void computer::biu()
{
rectangle(300,20,600,470); //CPU
poly[]={340,35,335,100,355,100,360,70,395,100,415,100,410,35,340,35};//draw CPU polygon
2

Programming Laboratory (210251)

setfillstyle(1,RED);
char ope[2];
ope[0]=a[2];
ope[1]='\0';
outtextxy(340,45,"Operation");
outtextxy(370,60,ope);
}
void computer::segments()
{
rectangle(320,130,420,230); //segments
for(int k=1;k<5;k++)
{ int a=320;
int b=130+20*k;
int c= 420;
int d=130+20*k;
line(a,b,c,d);
}
outtextxy(330,133,"ES=0000h");
outtextxy(330,153,"DS=0000h");
outtextxy(330,173,"CS:0000h");
outtextxy(330,193,"IP:0001h");
outtextxy(330,213,"DI:ALU");
rectangle(480,28,560,50); //memory interface
outtextxy(494,38,"Memory ");
rectangle(480,80,540,180); // instn queue
for(k=1;k<5;k++)
{ int a=480;
int b=80+20*k;
int c= 540;
int d=80+20*k;
line(a,b,c,d);
}
outtextxy(510,167,"1");
outtextxy(546,100,"Instruction");
outtextxy(546,115,"Queue");
rectangle(470,220,550,240); //control sys
outtextxy(480,222,"Program Counter");
}
void computer::registers()
{
rectangle(320,310,420,450); //registers
for(int k=1;k<8;k++)
{ int a=320;
int b=310+20*k;
int c= 420;
3

Programming Laboratory (210251)

int d=310+20*k;
line(a,b,c,d);
outtextxy(304,312,"AX");
outtextxy(304,332,"BX");
outtextxy(304,352,"CX");
outtextxy(304,372,"DX");
outtextxy(330,394,"SP=0001h");
outtextxy(330,414,"BP=0000h");
outtextxy(367,434,"FLAG");
}
}
void computer::alu()
{
outtextxy(490,320,"ALU");
int poly2[]={450,330,470,390,530,390,550,330,530,330,520,350,480,350,470,330,450,330};
drawpoly(9,poly2);
fillpoly(1,poly2);
}
void computer::flags()
{
rectangle(440,410,540,450); //flags
line(440,430,540,430);
for(int k=1;k<8;k++)
{ int a=440+12.5*k;
int b=410;
int c= 440+12.5*k;
int d=450;
line(a,b,c,d);
}
outtextxy(443,420,"-");
outtextxy(456,420,"-");
outtextxy(469,420,"-");
outtextxy(482,420,"-");
outtextxy(494,420,"O");
outtextxy(507,420,"D");
outtextxy(520,420,"I");
outtextxy(533,420,"T");
outtextxy(443,437,"S");
outtextxy(456,437,"Z");
outtextxy(469,437,"-");
outtextxy(482,437,"A");
outtextxy(494,437,"-");
outtextxy(507,437,"P");
outtextxy(520,437,"-");
outtextxy(533,437,"C");
4

Programming Laboratory (210251)

char l=a[2];
switch(l)
{
case '+': setfillstyle(1,RED);
floodfill(538,448,15); //carry
setfillstyle(1,RED);
floodfill(512,442,15); //parity
setfillstyle(1,RED);
floodfill(489,443,15); //auxill
setfillstyle(1,RED);
floodfill(492,424,15); //over
break;
case '-': setfillstyle(1,BLUE);
floodfill(441,441,15); //sign
setfillstyle(1,BLUE);
floodfill(454,441,15) ; //zero
setfillstyle(1,BLUE);
floodfill(538,448,15);
setfillstyle(1,BLUE);
floodfill(512,442,15);
break;
case '*': setfillstyle(1,GREEN);
floodfill(454,441,15) ;
setfillstyle(1,GREEN);
floodfill(489,443,15);
setfillstyle(1,GREEN);
floodfill(538,448,15);
setfillstyle(1,GREEN);
floodfill(512,442,15);
break;
case '/':
setfillstyle(1,BROWN);
floodfill(454,441,15) ;
setfillstyle(1,BROWN);
floodfill(489,443,15);
setfillstyle(1,BROWN);
floodfill(538,448,15);
setfillstyle(1,BROWN);
floodfill(512,442,15);
break;
}
}
void computer::buses()
{
setcolor(10);
line(420,40,470,40);
5

Programming Laboratory (210251)

line(470,40,460,37);
line(470,40,460,43);
delay(500);
line(510,55,510,75);
line(510,75,505,72);
line(510,75,515,72);
delay(500);
line(510,185,510,210);
line(510,210,505,206);
line(510,210,515,206);
delay(500);
setcolor(15);
int
poly1[]={340,260,340,280,455,280,455,290,460,310,470,290,465,290,465,280,535,280,535,290,
540,310,550,290,545,290,545,280,570,280,570,440,590,440,590,260,340,260};
setfillstyle(8,RED);
fillpoly(21,poly1);
delay(1000);
setcolor(10);
line(540,360,570,360);
line(570,360,560,350);
line(570,360,560,370);
delay(1000);
line(490,390,490,410);
line(490,410,487,405);
line(490,410,493,405);
delay(1000);
line(540,430,570,430);
line(540,430,543,425);
line(540,430,543,435);
line(570,430,568,425);
line(570,430,568,435);
setcolor(15);
}
void computer::exp1()
{
cout<<"\n Enter expression:";
for(int i=0;i<3;i++)
{
cin>>a[i];
}
a[3]='\0';
i=0;
while(isdigit(a[i]))
{ n1[i]=a[i];
6

Programming Laboratory (210251)

i++;
}
n1[i]='\0';
i=2;
while(isdigit(a[i]))
{ n2[j]=a[i];
i++;
j++;
}
n2[j]='\0';
a1= atoi(n1);
b1=atoi(n2);
if(a[1]=='+')
c1=a1+b1;
else if(a[1]=='-')
c1=a1-b1;
else if(a[1]=='*')
c1=a1*b1;
else if(a[1]=='/')
c1=a1/b1;
else if(a[1]=='%')
c1=a1%b1;
else
cout<<"\n Enter correctly:";
}
void computer:: display()
{
outtextxy(324,313,n1);
outtextxy(324,333,n2);
char n3[2];
n3[0]='=';
n3[1]='\0';
char n4[50];
itoa(a1,n1,2);
itoa(b1,n2,2);
outtextxy(340,313,strcat("=",n1));
outtextxy(340,333,n3);
outtextxy(346,333,n2);
itoa(c1,n4,2);
outtextxy(322,373,n4);
outtextxy(480,365,n4);
flushall();
char v[20];
itoa(c1,v,10);
outtextxy(72,130,v);
7

Programming Laboratory (210251)

}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
computer c;
c.exp1();
delay(500);
c.biu();
delay(500);
c.segments();
delay(500);
c.registers();
delay(500);
c.display();
delay(500);
c.alu();
delay(500);
c.flags();
delay(500);
c.buses();
getch();
}
Graphics function used:
1. outtextxy
2. line
3. rectangle
4. drawpoly
5. fillpoly
6. setfillstyle
7. floodfill
Output:

Displaying Pixel or Point


Title: Writing a C++ class for displaying pixel or point on the screen.
Aim: To understand, how to display pixel on screen.
8

Programming Laboratory (210251)

C++ Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class pixel
{
public:
int x,y;
void disp(int,int);
};
void pixel::disp(int p,int q)
{
putpixel(p,q,WHITE);
}
void main()
{
class pixel p;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tcplus\\bgi");
cout<<"Enter position of pixel";
cin>>p.x>>p.y;
p.disp(p.x,p.y);
getch();
}
Output:

DDA and Bresenham Line


Title: Write a C++ class for a Line drawing method using overloading DDA and Bresenhams
Algorithms, inheriting the pixel or point.

Programming Laboratory (210251)

C++ Code for DDA Line Drawing Algorithm:


#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
class line
{
public:
int x1,y1,x2,y2;
void drawl(int,int,int,int);
};
void line::drawl(int x1,int y1,int x2,int y2)
{
int i=1,dx,dy,step,x,y;
float xinc,yinc;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx<=dy)
{
step=dy;
}
else
{step=dx;}
xinc=dx/step;
yinc=dy/step;
x=x1;y=y1;
putpixel(x,y,WHITE);
while(i<=step)
{
x=x+xinc+0.5;
y=y+yinc+0.5;i++;
putpixel(x,y,WHITE);
}
}
void main()
{
class line l;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"Enter coordinates of end points";
cin>>l.x1>>l.y1>>l.x2>>l.y2;
l.drawl(l.x1,l.y1,l.x2,l.y2);
getch();
}

10

Programming Laboratory (210251)

Output:
C++ Code for Bresenhams Line Drawing Algorithm:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class line
{
public:
int x1,y1,x2,y2;
void drawl(int,int,int,int);
};
void line::drawl(int x1,int y1,int x2,int y2)
{
int dx=x2-x1,dy=y2-y1,di=2*dx-dy,ds=2*dy,dt=2*(dy-dx);
putpixel(x1,y1,WHITE);
while(x1<=x2)
{
x1++;
if(di<0)
{di+=ds;}
else
{
y1++;
di+=dt;
}
putpixel(x1,y1,WHITE);
}
}
void main()
{
class line l;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"Enter coordinates of end points x1,y1,x2,y2";
cin>>l.x1>>l.y1>>l.x2>>l.y2;
l.drawl(l.x1,l.y1,l.x2,l.y2);
getch();
}

Circle Drawing
Title: Write a C++ class for a circle drawing inheriting line class.
C++ Code:
11

Programming Laboratory (210251)

#include<conio.h>
#include<iostream.h>
#include<graphics.h>
class linep //line class
{
public:
int x1,y1,x2,y2;
};
class circle:public linep //circle class inherit from linep class
{
public:
void disp(int,int,int);
};
void circle::disp(int x,int y,int r)
{
int p,x1,y1;
x1=0;
y1=r;
p=3-r;
while(x1<=y1)
{
if(p<0)
{
x1++;
p+=4*x1+6;
}
else
{
x1++;y1--;
p+=4*(x1-y1)+10;
}
putpixel(x1+x,y1+y,1);
putpixel(x1+x,y-y1,1);
putpixel(x-x1,y1+y,1);
putpixel(x-x1,y-y1,1);
putpixel(x+y1,y+x1,1);
putpixel(x+y1,y-x1,1);
putpixel(x-y1,y+x1,1);
putpixel(x-y1,y-x1,1);
}
}
void main()
{
12

Programming Laboratory (210251)

class circle c;
class linep p;
int gd=DETECT,gm,r;
initgraph(&gd,&gm,"C:\\tc\\bgi");
cout<<"Enter radius of circle";
cin>>r;
cout<<"Enter co-ordinates of circle";
cin>>p.x2>>p.y2;
c.disp(p.x2,p.y2,r);
getch();
}
Output:

13

Programming Laboratory (210251)

Line Drawing
Title: Write a program in C++ to draw a line with line style (Thick, Thin, Dotted).
C++ code:
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
class linep
{
public:
int x1,y1,x2,y2;
void drawl(int,int,int,int);
void thick(int,int,int,int,int);
void dotted(int,int,int,int);
};
void linep::thick(int x1,int y1,int x2,int y2,int w)
{
int i,dx,dy,step,xinc,yinc;
dx=x2-x1;
dy=y2-y1;
if(dy>=dx)
{
step=dy;
}
else
{
step=dx;
}
xinc=dx/step;
yinc=dy/step;
while(x1<=x2)
{
x1=x1+xinc+0.5;
y1=y1+yinc+0.5;
for(i=0;i<w;i++)
{
putpixel(x1+i,y1,WHITE);
}
}
}
void linep::drawl(int x1,int y1,int x2,int y2)
{
int dx,dy,step,xinc,yinc;
dx=x2-x1;
14

Programming Laboratory (210251)

dy=y2-y1;
if(dy>=dx)
{
step=dy;
}
else
{
step=dx;
}
xinc=dx/step;
yinc=dy/step;
putpixel(x1,y1,WHITE);
while(x1<=x2)
{
x1=x1+xinc+0.5;
y1=y1+yinc+0.5;
putpixel(x1,y1,WHITE);
}
}
void linep::dotted(int x1,int y1,int x2,int y2)
{
int i=0,dx,dy,step,xinc,yinc;
dx=x2-x1;
dy=y2-y1;
if(dy>=dx)
{
step=dy;
}
else
{
step=dx;
}
xinc=dx/step;
yinc=dy/step;
putpixel(x1,y1,WHITE);
while(x1<=x2)
{
if(i%2==0)
{
putpixel(x1,y1,WHITE);
}
x1=x1+xinc+0.5;
y1=y1+yinc+0.5;
i++;
}
15

Programming Laboratory (210251)

}
void main()
{
class linep p;
int gd=DETECT,gm,n,i,w;
initgraph(&gd,&gm,"C:\\TC\\bgi");
cout<<"Enter co-ordinates of line x1,y1,x2,y2";
cin>>p.x1>>p.y1>>p.x2>>p.y2;
cout<<"Enter your choice 1. Thin Line 2. Thick line 3. Dotted line";
cin>>i;
switch(i)
{
case 1: p.drawl(p.x1,p.y1,p.x2,p.y2); break;
case 2:
cout<<"Enter width of line in pixel";
cin>>w;
p.thick(p.x1,p.y1,p.x2,p.y2,w); break;
case 3: p.dotted(p.x1,p.y1,p.x2,p.y2); break;
default:
cout<<"Enter proper choice";
}
getch();
}
Output:

16

Programming Laboratory (210251)

Circle of Desired Radius


Title: Write a program in C++ to draw a circle of desired radius.
C++ Code:
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
void display(int,int,int,int);
void main()
{
int p,gd=DETECT,gm,x1,y1,x,y,r;
initgraph(&gd,&gm,"c :\\tc\\bgi");
cout<<"Enter radius of circle";
cin>>r;
cout<<"Enter co-ordinates of circle";
cin>>x>>y;
x1=0;
y1=r;
p=3-r;
while(x1<=y1)
{
if(p<0)
{
x1++;
p+=4*x1+6;
}
else
{
x1++;y1--;
p+=4*(x1-y1)+10;
}
display(x1,y1,x,y);
}
getch();
}
void display(int x1,int y1,int x,int y)
{
putpixel(x1+x,y1+y,1);
putpixel(x1+x,y-y1,1);
putpixel(x-x1,y1+y,1);
putpixel(x-x1,y-y1,1);
putpixel(x+y1,y+x1,1);
putpixel(x+y1,y-x1,1);
17

Programming Laboratory (210251)

putpixel(x-y1,y+x1,1);
putpixel(x-y1,y-x1,1);
}
Output:

18

Programming Laboratory (210251)

Draw Polygons
Title: Write a C/C++ program to draw convex polygons (Square, Rectangle, and Triangle).
C++ Code:
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
class line
{
public:
int x1,y1,x2,y2;
void drawl(int,int,int,int);
};
void line::drawl(int x1,int y1,int x2,int y2)
{
int i=1,dx,dy,step,x,y;
float xinc,yinc;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx<=dy)
{
step=dy;
}
else
{step=dx;}
xinc=dx/step;
yinc=dy/step;
x=x1;y=y1;
while(i<=step)
{
x=x+xinc+0.5;
y=y+yinc+0.5;i++;
putpixel(x,y,WHITE);
}
}
void main()
{
int i,x1,y1,x2,y2,gd=DETECT,gm,len,wid,size, base,height;
initgraph(&gd,&gm,"c:\\tc\\bgi");
class line l;
cout<<"Enter value of x1,y1";
cin>>x1>>y1;
19

Programming Laboratory (210251)

cout<<"Enter your choice 1. Rectangle 2. Square 3. Triangle";


cin>>i;
switch(i)
{
case 1:
cout<<"Enter length & Width of Rectangle";
cin>>len>>wid;
x2=x1+wid;
y2=y1;
l.drawl(x1,y1,x2,y2);
x1=x1+wid;
y1=y2;
y2=y2+len;
x2=x1;
l.drawl(x1,y1,x2,y2);
x1=x2;
y1=y2;
x2=x2-wid;
y2=y2;
l.drawl(x2,y2,x1,y1);
x1=x2;
y1=y2;
x2=x1;
y2=y2-len;
l.drawl(x2,y2,x1,y1);
break;
case 2:
cout<<"Enter Size of Square";
cin>>size;
x2=x1+size;
y2=y1;
l.drawl(x1,y1,x2,y2);
x1=x1+size;
y1=y2;
y2=y2+size;
x2=x1;
l.drawl(x1,y1,x2,y2);
x1=x2;
y1=y2;
x2=x2-size;
y2=y2;
l.drawl(x2,y2,x1,y1);
x1=x2;
y1=y2;
x2=x1;
20

Programming Laboratory (210251)

y2=y2-size;
l.drawl(x2,y2,x1,y1);
break;
case 3:
int x3,y3;
cout<<"Enter base and height of triangle ";
cin>>base>>height;
x2=x1+base;
y2=y1;
l.drawl(x1,y1,x2,y2);
x3=x1;
y3=y1+height;
l.drawl(x1,y1,x3,y3);
l.drawl(x2,y2,x3,y3);
break;
default: cout<<"Enter correct choice";
}
getch();
}
Output:

21

Programming Laboratory (210251)

Inside Test
Title: Write a program in C++ to test that given point is inside the polygon (Rectangle).
C++ Code:
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
class line
{
public:
int x1,y1,x2,y2;
void drawl(int,int,int,int);
};
void line::drawl(int x1,int y1,int x2,int y2)
{
int i=1,dx,dy,step,x,y;
float xinc,yinc;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx<=dy)
{
step=dy;
}
else
{step=dx;}
xinc=dx/step;
yinc=dy/step;
x=x1;y=y1;
putpixel(x,y,WHITE);
while(i<=step)
{
x=x+xinc+0.5;
y=y+yinc+0.5;
i++;
putpixel(x,y,WHITE);
}
}
void main()
{
class line l;
int i,x1=100,y1=100,x2,y2,gd=DETECT,gm,len=50,wid=34,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
22

Programming Laboratory (210251)

x2=x1+wid;
y2=y1;
l.drawl(x1,y1,x2,y2);
x1=x1+wid;
y1=y2;
y2=y2+len;
x2=x1;
l.drawl(x1,y1,x2,y2);
x1=x2;
y1=y2;
x2=x2-wid;
y2=y2;
l.drawl(x2,y2,x1,y1);
x1=x2;
y1=y2;
x2=x1;
y2=y2-len;
l.drawl(x2,y2,x1,y1);
cout<<"Enter position of point";
cin>>x>>y;
if(((x<x1)&&(y<y1))||((x>x2)&&(y>y2)))
{
putpixel(x,y,BLUE);
cout<<"Point outside of the polgon";
}
else
{
cout<<"Point inside of the polgon";
}
getch();
}
Output:

23

Programming Laboratory (210251)

Scan Line
Title: Write a C++ program to fill polygon using scan line algorithm.
C++ Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
struct edge
{
int x1,y1,x2,y2,flag;
};
void main()
{
int n,i,j,k,gd=DETECT,gm, x[10],y[10],ymax=0,ymin=480,yy,temp;
struct edge ed[10],temped;
float dx,dy,m[10],x_int[10],inter_x[10];
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"\n Enter the number of vertices of the graph: ";
cin>>n;
cout<<"\n Enter the co-ordinates of vertices: \n";
for(i=0;i<n;i++)
{
cout<<"x"<<i;
cin>>x[i]>>endl;
cout<<"y"<<i;
cin>>y[i]>>endl;
if(y[i]>ymax)
ymax=y[i];
if(y[i]<ymin)
ymin=y[i];
ed[i].x1=x[i];
ed[i].y1=y[i];
}
for(i=0;i<n-1;i++) //store the edge information
{
ed[i].x2=ed[i+1].x1;
ed[i].y2=ed[i+1].y1;
ed[i].flag=0;
}
ed[i].x2=ed[0].x1;
ed[i].y2=ed[0].y1;
ed[i].flag=0;
24

Programming Laboratory (210251)

for(i=0;i<n-1;i++) //check for y1>y2 if not then interchange it


{
if(ed[i].y1<ed[i].y2)
{
temp=ed[i].x1;
ed[i].x1=ed[i].x2;
ed[i].x2=temp;
temp=ed[i].y1;
ed[i].y1=ed[i].y2;
ed[i].y2=temp;
}
}
for(i=0;i<n;i++) //draw polygon
{
line(ed[i].x1,ed[i].y1,ed[i].x2,ed[i].y2);
}
for(i=0;i<n-1;i++) //storing the edges as y1,y2,x1
{
for(j=0;j<n-1;j++)
{
if(ed[j].y1<ed[j+1].y1)
{
temped=ed[j];
ed[j]=ed[j+1];
ed[j+1]=temped;
}
if(ed[j].y1==ed[j+1].y1)
{
if(ed[j].y2<ed[j+1].y2)
{
temped=ed[j];
ed[j]=ed[j+1];
ed[j+1]=temped;
}
if(ed[j].y2==ed[j+1].y2)
{
if(ed[j].x1<ed[j+1].x1)
{
temped=ed[j];
ed[j]=ed[j+1];
ed[j+1]=temped;
}
}
}
}
25

Programming Laboratory (210251)

}
for(i=0;i<n;i++) //calculate 1/slope
{
dx=ed[i].x2-ed[i].x1;
dy=ed[i].y2-ed[i].y1;
if(dy==0)
m[i]=0;
else
m[i]=dx/dy;
inter_x[i]=ed[i].x1;
}
yy=ymax;
while(yy>ymin) //Mark active edges
{
for(i=0;i<n;i++)
{
if(yy>ed[i].y2 && yy<=ed[i].y1 && ed[i].y1!=ed[i].y2)
ed[i].flag=1;
else
ed[i].flag=0;
}
j=0;
for(i=0;i<n;i++) //Finding x intersections
{
if(ed[i].flag==1)
{
if(yy==ed[i].y1)
{
x_int[j]=ed[i].x1;
j++;
if(ed[i-1].y1==yy&&ed[i-1].y1<yy)
{
x_int[j]=ed[i].x1;
j++;
}
if(ed[i+1].y1==yy&&ed[i+1].y1<yy)
{
x_int[j]=ed[i].x1;
j++;
}
}
else
{
26

Programming Laboratory (210251)

x_int[j]=inter_x[i]+(-m[i]);
inter_x[i]=x_int[j];
j++;
}
}
}
for(i=0;i<j;i++) //sorting the x intersections
{
for(k=0;k<j-1;k++)
{
if(x_int[k]>x_int[k+1])
{
temp=x_int[k];
x_int[k]=x_int[k+1];
x_int[k+1]=temp;
}
}
}
for(i=0;i<j;i+=2) //Extracting x values to draw a line
{
line(x_int[i],yy,x_int[i+1],yy);
}
yy--;
}
delay(3000);
getch();
}
Output:

27

Programming Laboratory (210251)

Title: Write a Java program to draw a line with line style (Thick, Thin, Dotted).
JAVA Code:
import java.awt.*;
import javax.swing.*;
public class drawline extends JFrame{
public drawline(){
super("Lines");
setSize(400,400);
setVisible(true);
}
Stroke[] linestyles=new Stroke[]{
new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL),
new BasicStroke(25.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER),
new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND),};
Stroke thindashed=new
BasicStroke(2.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL,1.0f, new float[]
{8.0f,3.0f,2.0f,3.0f},0.0f);
final static float dash1[]={10.0f};
final static BasicStroke dashed =new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
final static float thick1[]={10.0f};
final static BasicStroke thickdash =new BasicStroke(10.0f,
BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, thick1, 0.0f);
public void paint(Graphics g){
int xpoints[]={220,350,350,220};
int ypoints[]={220,220,320,320};
int npoints=4;
int xpoints1[]={60,120,180};
int ypoints1[]={320,220,320};
int npoints1=3;
Graphics2D g2d = (Graphics2D) g;
super.paint(g);
((Graphics2D)g).setStroke(dashed);
g.setColor(Color.black);
g2d.drawLine(50,50,200,50);
g2d.setColor(Color.black);
g.drawString("Dotted Line", 210,55);
((Graphics2D)g).setStroke(linestyles[3]);
g.setColor(Color.red);
g2d.drawLine(50,170,200,170);
g2d.setColor(Color.black);
g.drawString("Thin Line", 210,175);
28

Programming Laboratory (210251)

}
public static void main(String[] args){
Lines application = new Lines();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Steps for running this program:
1. Compilation of Program
C:\Program Files\Java\jdk1.7\bin>javac drawline.java
2. Run the Program
C:\Program Files\Java\jdk1.7\bin>java drawline
Output:

29

Programming Laboratory (210251)

Bouncing Ball using Maya


Title: Use Maya to draw a Bouncing ball animation.

Introduction:

Maya 3D animation, modeling, simulation, rendering and compositing software


offers a comprehensive creative feature set for 3D computer animation, modeling,
simulation and rendering on a highly extensible production platform. Maya provides
high-end character and effects toolsets along with increased productivity for
modeling, texturing and shade creation tasks.

30

Programming Laboratory (210251)

31

Programming Laboratory (210251)

C graphics using graphics.h functions or WinBGIM (Windows 7) can be used to draw different
shapes, display text in different fonts, change colors and many more. Using functions of
graphics.h in turbo C compiler you can make graphics programs, animations, projects and
games.
In a C/C++ Program first of all, we have to initialize the graphics drivers on the computer. This is
done using the initgraph method provided in graphics.h library.
Graphics mode Initialization:
First of all we have to call the initgraph function that will intialize the graphics mode on the
computer.
Prototype of initigraph:
void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);
Initgraph initializes the graphics system by loading a graphics driver from disk (or validating a
registered driver) then putting the system into graphics mode. Initgraph also resets all graphics
settings (color, palette, current position, viewport, etc.) to their defaults, then resets graph result
to 0.
*graphdriver
Integer that specifies the graphics driver to be used. You can give graphdriver a value using a
constant of the graphics_drivers enumeration type.
*graphmode
Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver
= DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver.
You can give *graphmode a value using a constant of the graphics_modes enumeration type.
*pathtodriver
Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.
1.
2.

If theyre not there, initgraph looks in the current directory.


If pathtodriver is null, the driver files must be in the current
directory.

*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode values or
youll get unpredictable results. (The exception is graphdriver = DETECT.)
After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set
to the current graphics mode. You can tell initgraph to use a particular graphics driver and mode,
32

Programming Laboratory (210251)

or to autodetect the attached video adapter at run time and pick the corresponding driver. If you
tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode.
Generally, initgraph loads a graphics driver by allocating memory for the driver (through
_graphgetmem), then loading the appropriate .BGI file from disk. As an alternative to this
dynamic loading scheme, you can link a graphics driver file directly into your executable
program file.
List of few functions from graphics.h file:
Circle Drawing
Arc Drawing
Screen
Pixel
Rectangle
Polygon
Line
Text print

Name of Function
circle
arc
getbkcolor, getcolor, setbkcolor, setcolor
getpixel, getx, gety, putpixel
rectangle
drawpoly, fillpoly, floodfill
line, linerel, lineto
outtextxy

getpixel(int x, int y);


putpixel(int x,int y, int color);
setbkcolor(BLUE);
getbkcolor(void);
setcolor(int color);
getcolor(void);
outtextxy function display text or string at a specified point(x,y) on the screen.
void outtextxy(int x, int y, char *string);
x, y are coordinates of the point and third argument contains the address of string to be displayed.
line(int x1, int y1, int x2, int y2)
linerel(int dx,int dy): draw a line relative distance.
lineto(int x,int y): draw a line original point to x,y point.
rectangle(left,top,right,bottom)
circle(int x, int y, int radius)
arc(int x,int y, int stangle, int endangle, int radius)
33

Programming Laboratory (210251)

drawpoly(int numpoints, int far *polypoints);


setfillstyle(int pattern, int color);
EMPTY_FILL: 0, SOLID_FILL: 1
fillpoly(int numpoints, int far *polypoints);
fillpoly draws outline of polygon using current line style and color.
floodfill(int x,int y int border);
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill
the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled
otherwise outside will be filled, border specifies the color of boundary of area. To change fill
pattern and fill color use setfillstyle. Code given below draws a circle and then fills it.
Computer Graphics:
Computer graphics remains one of the most existing and rapidly growing computer fields.
Computer graphics may be defined as a pictorial representation or graphical representation of
objects in a computer.
Applications of computer graphics:
There are many interesting applications of computer graphics. Three common applications are
graphic user interface (GUI), computer-aided design (CAD), and computer games.
Major components (hardware and software) are needed for computer graphics:
Besides the basic computer, some special devices and software may be required especially for
computer graphics. For hardware, a special high-resolution, color monitor is often demanded and
some input tools, e.g. mouse and joy-sticker, and hard-copy devices, e.g. high-resolution color
printer, may be required. For software, some special purpose utilities (device-dependent and
device-independent) are needed for handling processing in computer graphics.
Random/Raster Scan:
Random scan is a method in which the display is made by the electronic beam, which is
directed, only to the points or part of the screen where the picture is to be drawn.
The Raster scan system is a scanning technique in which the electrons sweep from top to
bottom and from left to right. The intensity is turned on or off to light and unlight the pixel.
Refreshing of the screen:
Refreshing of screen is done by keeping the phosphorus glowing to redraw the picture
repeatedly. i.e. by quickly directing the electronic beam back to the same points.
Aspect Ratio:
34

Programming Laboratory (210251)

The ratio of vertical points to the horizontal points necessary to produce length of lines in both
directions of the screen is called Aspect Ratio. Usually the aspect ratio is .
Addressability:
Addressability is the number of individual dots per inch (d.p.i.) that can be created. If the address
of the current dot is (x, y) then the next dot will be (x + y), (x + y + 1) etc.
Dot size:
The diameter of a single dot on the devices output.
Interdot distance:
Interdot distance is the reciprocal of addressability. If the addressability is large, the interdot
distance will be less. The interdot distance should be less to get smooth shapes.
C++:
Purpose of C++ programming was to add object orientation to the C programming language,
which is in itself one of the most powerful programming languages.
The core of the pure object-oriented programming is to create an object, in code, that has certain
properties and methods. While designing C++ modules, we try to see whole world in the form of
objects.
There are a few principle concepts that form the foundation of object-oriented
programming:
1. Object:
This is the basic unit of object oriented programming. That is both data and function that
operate on data are bundled as a unit called as object.
2. Class:
We define a blueprint for an object. This doesn't actually define any data, but it does
define what the class name means, that is, what an object of the class will consist of and
what operations can be performed on such an object.
There are three types of data members:
Public
Private
Protected
Syntax:
class classname{
private: //by default data members are private
public:
protected:
}; // end of the Class
3. Inheritance:
35

Programming Laboratory (210251)

One of the most useful aspects of object-oriented programming is code reusability. As the
name suggests Inheritance is the process of forming a new class from an existing class
that is from the existing class called as base class, new class is formed called as derived
class. This is a very important concept of object-oriented programming since this feature
helps to reduce the code size.
When creating a class, instead of writing completely new data members and member
functions, the programmer can designate that the new class should inherit the members of
an existing class. This existing class is called the base class, and the new class is referred
to as the derived class.
A class can be derived from more than one classes, which means it can inherit data and
functions from multiple base classes. To define a derived class, we use a class derivation
list to specify the base class(es). A class derivation list names one or more base classes
and has the form:
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is the
name of a previously defined class. If the access-specifier is not used, then it is private by
default.
class shape // shape is base class
{
public:
int length, width;
};
class rectangle:public shape // rectangle is derived class
{
public:
int get area()
{ int a;
a=length*width;
return a;
}
};
4. Abstraction:
Data abstraction refers to, providing only essential information to the outside world and
hiding their background details, i.e., to represent the needed information in program
without presenting the details. For example, a database system hides certain details of
how data is stored and created and maintained. Similar way, C++ classes provides
different methods to the outside world without giving internal detail about those methods
and data.
36

Programming Laboratory (210251)

5. Encapsulation:
Encapsulation is placing the data and the functions that work on that data in the same
place. While working with procedural languages, it is not always clear which functions
work on which variables but object-oriented programming provides you framework to
place the data and the relevant functions together in the same object.
6. Overloading:
The concept of overloading is also a branch of polymorphism. When the exiting operator
or function is made to operate on new data type, it is said to be overloaded.
When we call overloaded function or operator, the compiler determines the most
appropriate definition to use by comparing the argument types you used to call the
function or operator with the parameter types specified in the definitions. The process of
selecting the most appropriate overloaded function or operator is called overload
resolution
class dispdata{
public:
void disp(int i) {cout<<i;}
void disp(float f) {cout<<f;}
void disp(double b){cout<<b;}
};
void main()
{
int a=5;
float b=2.6;
double c=1.98654;
cout<<a<<endl<<b<<endl<<c;
getch();
}
7. Polymorphism:
The ability to use an operator or function in different ways in other words giving different
meaning or functions to the operators or functions is called polymorphism. Poly refers
too many. That is a single function or an operator functioning in many ways different
upon the usage is called polymorphism.
TIFF:
TIFF is a computer file format for storing raster graphics images, popular among graphic artists,
the publishing industry. The TIFF format is widely supported by image-manipulation
applications, by publishing and page layout applications, and by scanning, faxing, word
processing, optical character recognition and other applications. Adobe Systems, which acquired
Aldus, now holds the copyright to the TIFF specification.
GTK+:
37

Programming Laboratory (210251)

GTK+ (previously GIMP Toolkit, sometimes incorrectly referred to as the GNOME Toolkit) is
a cross-platform widget toolkit for creating graphical user interfaces. It is licensed under the
terms of the GNU LGPL, allowing both free and proprietary software to use it. It is one of the
most popular toolkits for the Wayland and X11 windowing systems, along with Qt.
The name GTK+ originates from GTK; the plus was added to distinguish an enhanced version. It
was originally created for the GNU Image Manipulation Program (GIMP), a free software raster
graphics editor.

38

S-ar putea să vă placă și