You are on page 1of 14

Problem: ___ ___ / ___ / 2018

1. WAP in C to draw line and circle.

Source Code:-

#include<stdio.h>
#include<conio.h>
#include <graphics.h>

int main(void)
{
int gdriver = DETECT, gmode;
clrscr();

initgraph(&gdriver, &gmode, "c://tc/bgi");

circle(450, 250, 80);


line(100, 100, 300, 300);
ellipse(250,200,0,360,150,50);

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

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

Output:-

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

2. WAP in C to draw a Stickman(movable).

Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main()
{
int gdriver = DETECT, gm, i=0;
clrscr();

initgraph(&gdriver, &gm, "c://tc/bgi");

for(i=50;i<getmaxx();i++)
{
circle(50+i, 100, 40);
line(50+i, 140, 50+i, 280);
line(10+i, 200, 90+i, 200);
line(10+i, 340, 50+i, 280);
line(50+i, 280, 90+i, 340);

delay(10);
cleardevice();
}

getch();
closegraph();
return 0;

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

Output:-

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

3. WAP in C to draw a line using DDA Algorithm.

Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

int main()
{
int gd=DETECT, gm, i;
float x, y, x1, y1, dx, dy, x2, y2, step;

clrscr();

initgraph(&gd, &gm, "C://TC/BGI");

printf("Enter Starting Point Value: ");


scanf("%f %f",&x1,&y1);

printf("Enter End Point Value: ");


scanf("%f %f",&x2, &y2);

dx = abs(x2-x1);
dy = abs(y2-y1);

if(dx>=dy)
step=dx;
else
step=dy;

dx = dx/step;
dy = dy/step;

x = x1;
y = y1;

for(i=1;i<step;i++)
{
putpixel(x, y, 5);
x = x + dx;
y = y + dy;
}

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

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

Output:-

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

4. WAP in C to draw a line using Bresenham Line


Drawing Algorithm.

Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main()
{
int gd = DETECT, gm, i;
float x, y, p, x1, y1, x2, y2, dx, dy;

clrscr();

initgraph(&gd, &gm, "C://TC/BGI");

printf("Enter Originating Point: ");


scanf("%f %f",&x1, &y1);

printf("Enter Ending Point: ");


scanf("%f %f",&x2, &y2);

dx = x2 - x1;
dy = y2 - y1;

p = 2*dy - dx;
x = x1;
y = y1;

while(x<=x2)
{
if(p<0)
{
p = p + 2*dy;
}
else
{
y = y+1;
p = p + 2*dy - 2*dx;
}

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

x = x+1;
putpixel(x,y,5);
}

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

Output:-

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

5. WAP in C to draw a circle using Mid-Point Circle


generation Algorithm.

Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void drawCircle(int, int, int);


void circleDraw(int, int, int, int);

int main()
{
int gd=DETECT, gm;
int x1, y1, r;

clrscr();

initgraph(&gd, &gm, "C://TC/BGI");

printf("Enter Origin: ");


scanf("%d %d", &x1, &y1);

printf("Enter Radius: ");


scanf("%d",&r);

drawCircle(x1, y1, r);

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

void drawCircle(int x1, int y1, int r)


{
int x=0, y=r;
int d=1-r;

circleDraw(x1,y1,x,y);

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

while(x<=y)
{
if(d<0)
{
x=x+1;
d=d+(2*x)+1;
}
else
{
x=x+1;
y=y-1;
d=d+2*(x-y)+1;
}

circleDraw(x1,y1,x,y);
}
}

void circleDraw(int x1, int y1, int x, int y)


{
putpixel(x1+x,y1+y,15);
putpixel(x1-x,y1+y,15);
putpixel(x1+x,y1-y,15);
putpixel(x1-x,y1-y,15);
putpixel(x1+y,y1+x,15);
putpixel(x1-y,y1+x,15);
putpixel(x1+y,y1-x,15);
putpixel(x1-y,y1-x,15);
}

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

Output:-

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

6. WAP in C to draw circle using Bresenham Circle


Generating Algorithm.

Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void circleDraw(int, int, int);


void drawCircle(int, int, int, int);

void circleDraw(int x1, int y1, int r)


{
int x=0, y=r;
int d=3-2*r;

while(y>=x)
{
drawCircle(x1, y1, x, y);
x++;

if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}

drawCircle(x1, y1, x, y);


delay(50);
}
}

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

void drawCircle(int x1, int y1, int x, int y)


{
putpixel(x1+x,y1+y, 15);
putpixel(x1-x,y1+y, 15);
putpixel(x1+x,y1-y, 15);
putpixel(x1-x,y1-y, 15);
putpixel(x1+y,y1+x, 15);
putpixel(x1-y,y1+x, 15);
putpixel(x1+y,y1-x, 15);
putpixel(x1-y,y1-x, 15);
}

int main()
{
int gd=DETECT, gm;
int x1, y1, r;

clrscr();

initgraph(&gd, &gm, "C://TC/BGI");

printf("Enter Origin Point: ");


scanf("%d %d",&x1,&y1);

printf("Enter Radius: ");


scanf("%d",&r);

circleDraw(x1, y1, r);

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

Vibhor Singhal Page: ____ 2101097(75)


Problem: ___ ___ / ___ / 2018

Output:-

Vibhor Singhal Page: ____ 2101097(75)

You might also like