0% found this document useful (0 votes)
1K views

Code:: 01. Write A Program in C To Draw A Smiley Face

The document contains 4 code snippets that draw shapes using computer graphics algorithms in C programming language. The first snippet draws a smiley face using circles and ellipses. The second and third snippets implement the Digital Differential Analyzer (DDA) and Bresenham's line drawing algorithms respectively and use them to draw the letters L and T. The fourth snippet uses Bresenham's algorithm to draw the letters L and T.

Uploaded by

Raj Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Code:: 01. Write A Program in C To Draw A Smiley Face

The document contains 4 code snippets that draw shapes using computer graphics algorithms in C programming language. The first snippet draws a smiley face using circles and ellipses. The second and third snippets implement the Digital Differential Analyzer (DDA) and Bresenham's line drawing algorithms respectively and use them to draw the letters L and T. The fourth snippet uses Bresenham's algorithm to draw the letters L and T.

Uploaded by

Raj Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

01. Write a Program in C to draw a smiley face.

Code:
#include<stdio.h>
#include <graphics.h>
#include <conio.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(YELLOW);
circle(300, 100, 40);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(300, 100, YELLOW);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(310, 85, 2, 6);
fillellipse(290, 85, 2, 6);
ellipse(300, 100, 205, 335, 20, 9);
ellipse(300, 100, 205, 335, 20, 10);
ellipse(300, 100, 205, 335, 20, 11);
getch();
return 0;
}

Output:

170102200 Misha Kumari


2. Write a Program in C to implement DDA line drawing
Algorithm and use it to draw letters L and T of
English Alphabet.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void lineUsingDDA(int x1,int y1,int x2,int y2)
{
int step;
int xincr,yincr;
int dx=(x2-x1);
int k=0;
int dy=(y2-y1);
if(abs(dx)>abs(dy))
step=dx;
else
step=dy;
xincr=dx/step;
yincr=dy/step;
putpixel(x1,y1,GREEN);
for(k=0;k<step;k++)
{
x1=x1+xincr;
y1=y1+yincr;
putpixel(x1,y1,GREEN);
}
}

void main()
{int i=0;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(WHITE);
lineUsingDDA(20,20,50,20);
lineUsingDDA(50,20,50,140);
lineUsingDDA(50,140,140,140);
lineUsingDDA(140,140,140,170);
lineUsingDDA(20,170,140,170);
lineUsingDDA(20,20,20,170);
lineUsingDDA(180,20,280,20);
lineUsingDDA(180,20,180,50);
lineUsingDDA(280,20,280,50);
lineUsingDDA(240,50,240,150);
lineUsingDDA(220,150,240,150);
lineUsingDDA(220,50,220,150);

170102200 Misha Kumari


lineUsingDDA(220,150,240,150);
lineUsingDDA(240,50,280,50);
lineUsingDDA(180,50,220,50);
getch();

Output:

170102200 Misha Kumari


3. Write a Program in C to implement Bresenham’s
line drawing Algorithm and also print the series of
intermediate points plotted in tabular form.
Code:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>

void drawLineUsingBresenham(int x1,int y1,int x2,int


y2) {
int k=0;
int i=0;
int dx=x2-x1;
int dxi=dx;
int p=0;
int dy=y2-y1;
if(dx==0)
{
int t=x1;
x1=y1;
y1=t;
t=x2;
x2=y2;
y2=t;
dx=x2-x1;
dy=y2-y1;
}
else
putpixel(x1,y1,GREEN);
p=(2*dy)-dx;
printf("\nk\tPk\txk+1\tyk+1\n");
for(i=0;i<dx;i++)
{
if(p<0)
{
k++;
x1=x1+1;
if(dxi==0)
putpixel(y1,x1,GREEN);
else
putpixel(x1,y1,GREEN);
p=p+(2*dy);
printf("%d\t%d\t%d\t%d\n",k,p,x1,y1);
printf("--------------------------------- \n");
}
else if(p>=0)
{
k++;
x1=x1+1;

170102200 Misha Kumari


y1=y1+1;
p=p+((2*dy)-(2*dx));

if(dxi==0)
putpixel(y1,x1,GREEN);
else
putpixel(x1,y1,GREEN);
printf("%d\t%d\t%d\t%d\n",k,p,x1,y1);
printf("--------------------------------- \n");
}
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawLineUsingBresenham(20,10,30,18);
}
Output:

170102200 Misha Kumari


4. Use the Bresenham’s line drawing Algorithm in
previous question to draw letters L and T of
English Alphabet.
Code:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>

void drawLineUsingBresenham(int x1,int y1,int x2,int


y2) {
int k=0;
int i=0;
int dx=x2-x1;
int dxi=dx;
int p=0;
int dy=y2-y1;
if(dx==0)
{
int t=x1;
x1=y1;
y1=t;
t=x2;
x2=y2;
y2=t;
dx=x2-x1;
dy=y2-y1;
}
else
putpixel(x1,y1,GREEN);
p=(2*dy)-dx;
for(i=0;i<dx;i++)
{
if(p<0)
{
k++;
x1=x1+1;
if(dxi==0)
putpixel(y1,x1,GREEN);
else
putpixel(x1,y1,GREEN);
p=p+(2*dy);
}
else if(p>=0)
{ k++; x1=x1+1;
y1=y1+1;
p=p+((2*dy)-
(2*dx));
if(dxi==0)

170102200 Misha Kumari


putpixel(y1,x1,GREEN);
else
putpixel(x1,y1,GREEN);
}
}
}

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(WHITE);
drawLineUsingBresenham(20,20,50,20);
drawLineUsingBresenham(50,20,50,140);
drawLineUsingBresenham(50,140,140,140);
drawLineUsingBresenham(140,140,140,170);
drawLineUsingBresenham(20,170,140,170);
drawLineUsingBresenham(20,20,20,170);
drawLineUsingBresenham(180,20,280,20);
drawLineUsingBresenham(180,20,180,50);
drawLineUsingBresenham(280,20,280,50);
drawLineUsingBresenham(240,50,240,150);
drawLineUsingBresenham(220,50,220,150);
drawLineUsingBresenham(220,150,240,150);
drawLineUsingBresenham(240,50,280,50);
drawLineUsingBresenham(180,50,220,50);
getch();
}

Output:

170102200 Misha Kumari

You might also like