Code Competitions

Coding competition

Tuesday, 14 May 2013

Create a header file and include DDA algorithm as a function and draw rectangle ,triangle and pentagon

Code for header file “dda.h

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void bres(int,int,int,int);
void bres(int x1,int y1,int x2,int y2)
{
float dx,dy,Xinc,Yinc,x,y;
int gd=DETECT,gm;
int temp,temp1,i;
initgraph(&gd,&gm,"");
if(x1>x2 || y1>y2)
{
temp=x1;
x1=x2;
x2=temp;
temp1=y1;
y1=y2;
y2=temp1;
}
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>dy)
  temp=dx;
 else
  temp=dy;
Xinc=dx/temp;
Yinc=dy/temp;
x=x1;
y=y1;
putpixel(x,y,3);
for(i=1;i<=temp;i++)
{
x+=Xinc;
y+=Yinc;
putpixel((int)x,(int)y,3);
}
}


Code For Main Program


#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include”z:\dd.h”
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
//code for rectangle.
bres(20,20,100,20);
bres(20,20,20,100);
bres(100,20,100,100);
bres(20,100,100,100);
//code for triangle.
bres(250,250,350,125);
bres(350,125,450,250);
bres(250,250,450,250);
//code for pentagon
bres(100,100,150,200);
bres(100,100,50,200);
bres(50,200,50,300);
bres(150,200,150,300);
bres(50 ,300,150,300);
getch();
}

OUTPUT:
  
 

No comments:

Post a Comment