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

Jaypee University of Engineering

The program demonstrates the DDA line drawing algorithm. It takes input of x1, y1, x2, y2 coordinates and calculates the slope. If slope is less than 1, it uses x as the counting variable and calculates corresponding y values using the slope. Otherwise, it uses y as the counting variable and calculates corresponding x values using the inverse slope. It plots the points using putpixel to generate the line segment.

Uploaded by

Aniruddha Mishra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Jaypee University of Engineering

The program demonstrates the DDA line drawing algorithm. It takes input of x1, y1, x2, y2 coordinates and calculates the slope. If slope is less than 1, it uses x as the counting variable and calculates corresponding y values using the slope. Otherwise, it uses y as the counting variable and calculates corresponding x values using the inverse slope. It plots the points using putpixel to generate the line segment.

Uploaded by

Aniruddha Mishra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Exercise List (week wise)

Using C only
1) Program to initialize the graph and draw lines of various
types.

PROGRAM
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
int main(void)
{
int gdriver =DETECT, gmode, errorcode;
int xmax, ymax;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if(errorcode != grOk)
{
printf("Graphics error: %s\ n", grapherrormsg(errorcode));
SUMIT GUPTA
111318

printf("press any key to halt");


getch();
exit(1);
}
rectangle(200,100,620,420);
rectangle(230,130,400,400);
setlinestyle(SOLID_LINE,1,1);
line(250,370,380,370);
outtextxy(420,370,"0:SOLID");
setlinestyle(USERBIT_LINE,0xfff0,1);
line(250,340,380,340);
outtextxy(420,340,"1:LONG DASH");
setlinestyle(DOTTED_LINE,1,1);
line(250,310,380,310);
outtextxy(420,310,"2:DOTTED");
setlinestyle(USERBIT_LINE,0xF5F5,1);
line(250,280,380,280);
outtextxy(420,280,"3:DASH DOT");
setlinestyle(USERBIT_LINE,0XF0F0,1);
line(250,250,380,250);
outtextxy(420,250,"4:MEDIUN DASH");
setlinestyle(USERBIT_LINE,0XF5F5,1);
line(250,220,380,220);
outtextxy(420,220,"5:DASH WITH 2 DOT");
setlinestyle(USERBIT_LINE,0XEEEE,1);
line(250,190,380,190);
outtextxy(420,190,"6:SHORT DASH");
setlinestyle(USERBIT_LINE,0XD5D5,1);
line(250,160,380,160);
outtextxy(420,160,"7:DASH WITH 3 DOT");
outtextxy(210,370,"0-");
SUMIT GUPTA
111318

outtextxy(210,340,"1-");
outtextxy(210,310,"2-");
outtextxy(210,280,"3-");
outtextxy(210,250,"4-");
outtextxy(210,220,"5-");
outtextxy(210,190,"6-");
outtextxy(210,160,"7-");
getch();
closegraph();
return 0;
}

OUTPUT

SUMIT GUPTA
111318

2) Program to draw a rectangle, lines as its


diagonals and a circle also fill appropriate
colors in different regions.

PROGRAM
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
void main()
{
SUMIT GUPTA
111318

int x, y,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\tc\lib");
x = getmaxx() / 2;
y = getmaxy() / 2;
setcolor(4);
rectangle(x-150,y-150,x+150,y+150);
line(x-150,y-150,x+150,y+150);
line(x+150,y-150,x-150,y+150);
circle(x,y,150);
setfillstyle(SOLID_FILL,2);
floodfill(x+11,y,4);
setfillstyle(SOLID_FILL,14);
floodfill(x-10,y,4);
setfillstyle(SOLID_FILL,1);
floodfill(x,y+11,4);
setfillstyle(SOLID_FILL,4);
floodfill(x,y-13,4);
setfillstyle(SOLID_FILL,11);
floodfill(x-145,y-120,4);
setfillstyle(SOLID_FILL,1);
floodfill(x-125,y-149,4);
setfillstyle(SOLID_FILL,3);
floodfill(x+145,y+129,4);
setfillstyle(SOLID_FILL,6);
floodfill(x+125,y+148,4);
setfillstyle(SOLID_FILL,15);
floodfill(x-145,y+130,4);
SUMIT GUPTA
111318

int gd=DETECT,gm,errorcode;
initgraph(&gd,&gm,"");
x=getmaxx()/2;
y=getmaxy()/2;
setcolor(3);
rectangle(x-170,y-130,x+190,y+105);
setfillstyle(1,15);
floodfill(x-167,y-128,3);
circle(x-130,y-90,40);
setfillstyle(1,2);
floodfill(x-133,y-85,3);
circle(x-60,y-80,25);
setfillstyle(1,2);
floodfill(x-57,y-75,3);
circle(x-20,y-100,15);
setfillstyle(1,2);
floodfill(x-22,y-95,3);
circle(x+5,y-80,10);
setfillstyle(1,2);
floodfill(x+7,y-77,3);
circle(x+20,y-65,5);
setfillstyle(1,2);
floodfill(x+22,y-63,3);
ellipse(x-150,y-10,0,360,10,20);
setfillstyle(1,10);
floodfill(x-145,y-5,3);
ellipse(x-110,y-30,360,0,20,10);
SUMIT GUPTA
111318

setfillstyle(1,12);
floodfill(x-113,y-28,3);
ellipse(x-100,y,360,0,30,15);
setfillstyle(1,10);
floodfill(x-98,y-2,3);
ellipse(x-40,y-10,0,360,20,40);
setfillstyle(1,10);
floodfill(x-37,y-8,3);
ellipse(x-140,y+30,360,0,20,10);
setfillstyle(1,10);
floodfill(x-137,y+33,3);
ellipse(x-90,y+70,360,0,70,20);
setfillstyle(1,12);
floodfill(x-88,y+72,3);
circle(x-15,y+90,10);
setfillstyle(1,5);
floodfill(x-12,y+93,3);
circle(x+5,y,15);
setfillstyle(1,5);
floodfill(x+6,y-1,3);
rectangle(x+100,y+85,x+140,y+100);
setfillstyle(1,4);
floodfill(x+102,y+88,3);
rectangle(x+120,y+60,x+165,y+75);
setfillstyle(1,8);
floodfill(x+122,y+63,3);
rectangle(x+140,y+20,x+170,y+50);
SUMIT GUPTA
111318

setfillstyle(1,9);
floodfill(x+143,y+23,3);
rectangle(x+55,y+60,x+70,y+95);
setfillstyle(1,6);
floodfill(x+57,y+63,3);
rectangle(x+85,y+45,x+105,y+65);
setfillstyle(1,9);
floodfill(x+87,y+47,3);
rectangle(x+25,y+25,x+75,y+45);
setfillstyle(1,4);
floodfill(x+27,y+27,3);
poly1[0]=x+35;
poly1[1]=y-110;
poly1[2]=x+35;
poly1[3]=y-50;
poly1[4]=x+65;
poly1[5]=y-110;
poly1[6]=poly1[0];
poly1[7]=poly1[1];
drawpoly(4,poly1);
setfillstyle(1,6);
fillpoly(4,poly1);
poly2[0]=x+80;
poly2[1]=y-95;
poly2[2]=x+65;
poly2[3]=y-45;
poly2[4]=x+130;
SUMIT GUPTA
111318

poly2[5]=y-55;
poly2[6]=poly2[0];
poly2[7]=poly2[1];
drawpoly(4,poly2);
setfillstyle(1,14);
fillpoly(4,poly2);
poly3[0]=x+115;
poly3[1]=y-125;
poly3[2]=x+95;
poly3[3]=y-90;
poly3[4]=x+145;
poly3[5]=y-100;
poly3[6]=poly3[0];
poly3[7]=poly3[1];
drawpoly(4,poly3);
setfillstyle(1,14);
fillpoly(4,poly3);
poly4[0]=x+155;
poly4[1]=y-100;
poly4[2]=x+130;
poly4[3]=y-70;
poly4[4]=x+170;
poly4[5]=y-65;
poly4[6]=poly4[0];
poly4[7]=poly4[1];
drawpoly(4,poly4);
setfillstyle(1,14);
SUMIT GUPTA
111318

fillpoly(4,poly4);
poly5[0]=x+25;
poly5[1]=y-50;
poly5[2]=x;
poly5[3]=y-45;
poly5[4]=x+15;
poly5[5]=y-25;
poly5[6]=poly5[0];
poly5[7]=poly5[1];
drawpoly(4,poly5);
setfillstyle(1,14);
fillpoly(4,poly5);
poly6[0]=x+25;
poly6[1]=y-25;
poly6[2]=x+85;
poly6[3]=y-35;
poly6[4]=x+30;
poly6[5]=y-10;
poly6[6]=poly6[0];
poly6[7]=poly6[1];
drawpoly(4,poly6);
setfillstyle(1,14);
fillpoly(4,poly6);
poly7[0]=x+115;
poly7[1]=y-40;
poly7[2]=x+85;
poly7[3]=y-15;
SUMIT GUPTA
111318

poly7[4]=x+115;
poly7[5]=y+5;
poly7[6]=poly7[0];
poly7[7]=poly7[1];
drawpoly(4,poly7);
setfillstyle(1,1);
fillpoly(4,poly7);
poly8[0]=x+155;
poly8[1]=y-55;
poly8[2]=x+170;
poly8[3]=y-10;
poly8[4]=x+125;
poly8[5]=y-20;
poly8[6]=poly8[0];
poly8[7]=poly8[1];
drawpoly(4,poly8);
setfillstyle(1,12);
fillpoly(4,poly8);
poly9[0]=x+20;
poly9[1]=y+60;
poly9[2]=x+10;
poly9[3]=y+75;
poly9[4]=x+20;
poly9[5]=y+90;
poly9[6]=x+30;
poly9[7]=y+75;
poly9[8]=poly9[0];
SUMIT GUPTA
111318

4. Program to demonstrate put pixel.


PROGRAM
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
void main()
{
int x,y,k,i;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\tc\lib");
x=getmaxx()/2;
y=getmaxy()/2;
rectangle(x-200,y-75,x+225,y+75);
for(i=1;i<70;i++)
{
delay(10);
putpixel(x-180+i,y-60,rand()%15);
}
for(i=1;i<55;i++)
{
delay(10);
putpixel(x-180,y-60+i,rand()%15);
}
for(i=1;i<70;i++)
{
delay(10);
putpixel(x-180+i,y-5,rand()%15);
}
for(i=1;i<55;i++)
{
delay(10);
putpixel(x-110,y-5+i,rand()%15);
}
for(i=1;i<70;i++)
SUMIT GUPTA
111318

{
delay(10);
putpixel(x-110-i,y+50,rand()%15);
}
for(i=1;i<110;i++)
{
delay(10);
putpixel(x-90,y-60+i,rand()%15);
}
for(i=1;i<65;i++)
{
delay(10);
putpixel(x-90+i,y+50,rand()%15);
}
for(i=1;i<110;i++)
{
delay(10);
putpixel(x-25,y+50-i,rand()%15);
}
for(i=1;i<110;i++)
{
delay(10);
putpixel(x,y-60+i,rand()%15);
}
for(i=1;i<50;i++)
{
delay(10);
putpixel(x+i,y-60+i,rand()%15);
}
for(i=1;i<50;i++)
{
delay(10);
putpixel(x+50+i,y-10-i,rand()%15);
}
for(i=1;i<110;i++)
{
SUMIT GUPTA
111318

delay(10);
putpixel(x+100,y-60+i,rand()%15);
}
for(i=1;i<110;i++)
{
k= rand()%15;
delay(10);
putpixel(x+126,y-60+i,k);
putpixel(x+127,y-60+i,k);
putpixel(x+128,y-60+i,k);
putpixel(x+129,y-60+i,k);
}
for(i=1;i<50;i++)
{
delay(10);
putpixel(x+150+i,y-60,rand()%15);
}
for(i=1;i<110;i++)
{
delay(10);
putpixel(x+175,y-60+i,rand()%15);
}
getch();
}

OUTPUT

SUMIT GUPTA
111318

5.Program to Generate a Line using DDA Algorithm.


PROGRAM
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x,y,x1,y1,x2,y2;
float m,yf,xf,m_inv;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\bor\\turbo5\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
printf("enter x1: ");
scanf("%d",&x1);
printf("enter y1: ");
scanf("%d",&y1);
printf("enter x2: ");
SUMIT GUPTA
111318

scanf("%d",&x2);
printf("enter y2: ");
scanf("%d", &y2);
printf("two co-ordinates are :(%d,%d);(%d,%d)",x1,y1,x2,y2);
m=(y2-y1)/(x2-x1);
printf("\n slope is %f",m);
if(x1<x2)
{
if(m<1)
{
x=x1;
yf=y1;
while(x<=x2)
{
putpixel(x,yf,10);
delay(20);
x++;
yf=yf+m;
}
}
else
{
y=y1;
xf=x1;
m_inv=(1/m);
while(y<=y2)
{
putpixel(xf,y,10);
delay(20);
SUMIT GUPTA
111318

xf=xf+m_inv;
y++;
}
}
}
else
{
if(m<=1)
{
while(x2<=x1)
{
y1=y1-m;
x1=x1-1 ;
delay(15);
putpixel(x1,y1,10);
}
}
else
{
while(y2<=y1)
{
x1=x1-(1/m);
y1=y1-1 ;
delay(15);
putpixel(x1,y1,10);
}
}
}
/* clean up */
getch();
SUMIT GUPTA
111318

closegraph();
return 0;
}

OUTPUT:

SUMIT GUPTA
111318

You might also like