Professional Documents
Culture Documents
putpixel(x,int(y+0.5),8);
x++;
y=(m*x)+b;
}
else
{
while(y<=y2)
{
putpixel(int(x+0.5),y,8);
y++;
x=(y-b)/m;
}
}
setcolor(8);
outtextxy(100,60,”Line using polynomial method”);
getch();
}
2DDA Line Drawing
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
intgd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}
#include<graphics.h>
#include<math.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,e;
int i,gd=DETECT,gm;
clrscr();
/* Read two end points of line */
cout<<"Enter the value of x1 :\t";
cin>>x1;
cout<<"Enter the value of y1 :\t";
cin>>y1;
cout<<"Enter the value of x2 :\t";
cin>>x2;
cout<<"Enter the value of y2 :\t";
cin>>y2;
/* Initialise graphics mode*/
initgraph(&gd,&gm,"c:/tc/bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
/* Initialise starting point
-----------------------------*/
x = x1;
y = y1;
/* Initialise decision variable
-------------------------------- */
e=2*dy-dx;
i=1; /* Initialise loop counter */
do
{
putpixel(x,y,WHITE);
delay(60);
while(e >= 0)
{
y=y+1;
e=e-2*dx;
}
x=x+1;
e=e+2*dy;
i=i+1;
}
while(i <= dx);
getch();
closegraph();
}
putpixle(midx+x,midy-y.BLUE);
putpixel(midx+y,midy+x,GREEN);
putpixel(midx+y,midy-x,RED);
putpixel(midx+x,midy-y,ORANGE);
putpixel(midx-x,midy-y,PURPLE);
putpixel(midx-y,midy+x,GREY);
putpixel(midx-x,midy+y,LIGHTRED);
if (d <= 0)
{
d = d + 4*x + 6;
}
else
{
d = d + 4*(x-y) + 10;
y = y - 1;
}
x = x + 1;
delay(50); /* Delay is purposely inserted to see
observe the circle drawing process */
}
while(x < y);
getch();
closegraph();
}
OUTPUT :
7Program to draw a RECTANGLE:
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
int left = 150, top = 150;
int right = 450, bottom = 450;
initgraph(&gd, &gm, "");
rectangle(left, top, right, bottom);
getch();
closegraph();
return 0;
}
OUTPUT :
8 Program to draw a CIRCLE:
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
circle(250, 200, 50);
getch();
closegraph();
return 0;
}
OUTPUT :
9 Program to draw 3 LINEs:
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
line(150, 150, 450, 150);
line(150, 200, 450, 200);
line(150, 250, 450, 250);
getch();
closegraph();
}
OUTPUT :
10 Program to draw ECILPSE:
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
int x = 250, y = 200;
int start_angle = 0;
int end_angle = 360;
int x_rad = 100;
int y_rad = 50;
initgraph(&gd, &gm, "");
ellipse(x, y, start_angle,
end_angle, x_rad, y_rad);
getch();
closegraph();
return 0;
}
OUTPUT :
11 Program to clear screen in graphics mode and set current
position to(0,0)
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setbkcolor(GREEN);
outtext("Press any key to clear the screen.");
getch();
cleardevice();
outtext("Press any key to exit...");
getch();
closegraph();
return 0;
}
OUTPUT :
12 CIRCLE DRAWING USING ( POLYNOMIAL
METHOD ALGORITHM)
#include<graphics.h>
#include<conio.h>
#include<math.h>
voidsetPixel(int x, int y, int h, int k)
{
putpixel(x+h, y+k, WHITE);
putpixel(x+h, -y+k, WHITE);
putpixel(-x+h, -y+k, WHITE);
putpixel(-x+h, y+k, WHITE);
putpixel(y+h, x+k, WHITE);
putpixel(y+h, -x+k, WHITE);
putpixel(-y+h, -x+k, WHITE);
putpixel(-y+h, x+k, WHITE);
}
main()
{
intgd=0, gm,h,k,r;
double x,y,x2;
h=200, k=200, r=100;
initgraph(&gd, &gm, "C:\\TC\\BGI ");
setbkcolor(WHITE);
x=0,y=r;
x2 = r/sqrt(2);
while(x<=x2)
{
y = sqrt(r*r - x*x);
setPixel(floor(x), floor(y), h,k);
x += 1;
}
getch();
closegraph();
return 0;
}
OUTPUT :