0% found this document useful (0 votes)
4 views

graphics_Lab

The document contains multiple C++ programs demonstrating various computer graphics techniques using the graphics.h library. It includes implementations of line drawing algorithms (DDA and Bresenham's), functions to draw lines, arcs, circles, and a Bresenham's circle drawing algorithm. Each program initializes the graphics mode, takes user input for coordinates or parameters, and displays the graphical output.

Uploaded by

Bot Arc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

graphics_Lab

The document contains multiple C++ programs demonstrating various computer graphics techniques using the graphics.h library. It includes implementations of line drawing algorithms (DDA and Bresenham's), functions to draw lines, arcs, circles, and a Bresenham's circle drawing algorithm. Each program initializes the graphics mode, takes user input for coordinates or parameters, and displays the graphical output.

Uploaded by

Bot Arc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

//write the basic structure of computer graphics program .


#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
//void initgraph(int *graphicsdiver,int *graphicsmode,char *graphic directory path)
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
//your code,for example:
// line(50,50,100,100);
getch();
closegraph();
return 0;
}
2
// WAP to implement DDA line drawing algorithm
#include<iostream>
#include<graphics.h>

using namespace std;


int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
float x1,x2,y1,y2,xinc,yinc;
cout<<"Enter the co-ordinates(x1,y1,x2,y2): ";
cin>>x1>>y1>>x2>>y2;

float dx = (x2 > x1) ? (x2 - x1) : (x1 - x2);


float dy = (y2 > y1) ? (y2 - y1) : (y1 - y2);
int steps = (dx >= dy) ? dx : dy;

xinc=dx/steps;
yinc=dy/steps;

float x=x1;
float y=y1;
cout<<"Initial coordinate : "<<x<<" "<<y<<"\n";
putpixel(x,y,WHITE);

for(int i=0;i<steps;i++)
{
x=x+xinc;
y=y+yinc;
cout<<"Next coordinate : "<<x<<" "<<y<<"\n";
putpixel(x,y,WHITE);
}
getch();
closegraph();
return 0;
}
3
//WAP to implement Bresenhem's line drawing algorithm
#include<iostream>
#include<graphics.h>

using namespace std;


int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
float x1,x2,y1,y2;
cout<<"Enter the co-ordinates(x1,y1,x2,y2): ";
cin>>x1>>y1>>x2>>y2;

int dx = (x2 > x1) ? (x2 - x1) : (x1 - x2);


int dy = (y2 > y1) ? (y2 - y1) : (y1 - y2);

int P=(2*dy)-dx;
int x=x1;
int y=y1;

putpixel(x,y,WHITE);
cout<<"Initial coordinate : "<<x<<" "<<y<<"\n";
cout<<"Initial P = "<<P<<"\n";
int i=0;

while(i<dx)
{
if (P<0)
{
x=x+1;
putpixel(x,y,WHITE);
P=P+(2*dy);
cout<<"Next coordinate : "<<x<<" "<<y<<"\n";
cout<<"Next P = "<<P<<"\n";
i++;
}
else
{
x=x+1;
y=y+1;
putpixel(x,y,WHITE);
P=P+((2*dy)-(2*dx));
cout<<"Next coordinate : "<<x<<" "<<y<<"\n";
cout<<"Next P = "<<P<<"\n";
i++;
}
}

getch();
closegraph();
return 0;
}
4
//WAP to draw a line using line() function
#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
int x1,x2,y1,y2;
cout<<"Enter the co-ordinates(x1,y1,x2,y2): ";
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
getch();
closegraph();
return 0;
}
5
//WAP to draw a arc using arc() function
#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
int x, y, start_angle, end_angle, radius;
cout<<"Enter the center coordinates (x, y): ";
cin>>x>>y;
cout<<"Enter start angle, end angle, and radius: ";
cin>>start_angle>>end_angle>>radius;

arc(x, y, start_angle, end_angle, radius);

getch();
closegraph();
return 0;
}
6
// WAP to implement Bresenhem's Circle drawing algorithm
#include<iostream>
#include<graphics.h>
#
using namespace std;
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
int p,q,r;
cout<<"Enter Center (p,q): ";
cin>>p>>q;
cout<<"Enter radius: ";
cin>>r;

int d=3-(2*r);

int x=0;
int y=r;

while(x<=y)
{
putpixel(p+x,q+y,WHITE);
putpixel(p-x,q-y,WHITE);
putpixel(p+x,q-y,WHITE);
putpixel(p-x,q+y,WHITE);
putpixel(p+y,q+x,WHITE);
putpixel(p-y,q-x,WHITE);
putpixel(p+y,q-x,WHITE);
putpixel(p-y,q+x,WHITE);
if (d<=0)
{
x=x+1;
d=d+(4*x)+6;
}
else{
x=x+1;
y=y-1;
d=d+(4*(x-y))+10;
}
}

getch();
closegraph();
return 0;
}
7
//WAP to draw a circle using circle() function
#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
int x, y, radius;
cout<<"Enter the center coordinates (x, y): ";
cin>>x>>y;
cout<<"Enter the radius: ";
cin>>radius;

circle(x, y, radius);

getch();
closegraph();
return 0;
}

You might also like