Digital Differential Analyzer (DDA) Algorithm

394

DDA_1

 DDA_2

 DDA_3

/*************************************************
* Digital Differential Analyzer (DDA) Algorithm *
*************************************************
* Usage :- Simple and general method for rasterizing lines and triangles
* Author :- Rooparam Choudhary
* Date :- August 29, 2009
* Place :- Shri Mata Vaishno Devi University
* EntryNo :- 2006ECS20
*/

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

#define ROUND(x) ((int)(x+0.5))

int MYCOLOR = WHITE;

inline void setpixel(int, int);
void DDA(int, int, int, int);
void unionJack(void);
void hexagon(int, int);
void randomUnionJack(int);

inline void setpixel(int x, int y){
putpixel(320+x, 240-y, MYCOLOR);
}

void randomUnionJack(int count){
void DJ(int, int, int);
int xr, yr, clr=0;
srand(time(NULL));
while(count-- > 0){
xr = -320 + rand()d1;
yr = -240 + rand()H1;
clr = 0;
if(yr > -xr){
if(xr < 0) clr = 3;
else if(yr < 0) clr = 2;
else if(yr > xr) clr = 4;
else if(yr < xr) clr = 1;
}else if(yr < -xr) {
if(xr > 0) clr = 3;
else if(yr > 0) clr = 2;
else if(yr < xr) clr = 4;
else if(yr > xr) clr = 1;
}
DJ(xr, yr, clr);
}
}

void DJ(int x, int y, int clr){
int shade = 0;
switch(clr){
case 1: shade = RED; break;
case 2: shade = BLUE; break;
case 3: shade = CYAN; break;
case 4: shade = MAGENTA; break;
default: return; break;
}
putpixel(320+x, 240-y, shade);
}

void DDA(int x0, int y0, int x1, int y1) {
int delX = x1-x0, delY = y1-y0, steps, k;
float x=x0, y=y0, incX, incY;

steps = (abs(delX) > abs(delY)) ? abs(delX) : abs(delY) ;

incX = delX / (float)steps;
incY = delY / (float)steps;

setpixel(ROUND(x), ROUND(y));
for(k=0; k<steps; ++k) {
x += incX;
y += incY;
setpixel(ROUND(x), ROUND(y));
}
}

int main(void) {
int gd = DETECT, gm;
char *author = "Coder:- Rooparam Choudhary";
char *algo = "Digital Differential Analyzer Algorithm";
char point[30];
std::cout << "Digital Differential Analyzer Algorithm ";
std::cout << author << std::endl;
initgraph(&gd, &gm, "C:");

setcolor(WHITE);
setbkcolor(GREEN);
outtextxy(320 - textwidth(algo)/2, 5, algo);
outtextxy(getmaxx()-4 -textwidth(author), getmaxy()-20, author);
setcolor(GREEN);
setbkcolor(BLACK);

MYCOLOR = GREEN;
// X-axis
DDA(-320, -1, 320, -1);
DDA(-320, 0, 320, 0);
DDA(-320, 1, 320, 1);
// Y-axis
DDA(-1, 240, -1, -240);
DDA( 0, 240, 0, -240);
DDA( 1, 240, 1, -240);
// Y - X = 0
DDA(-240, -240, 240, 240);
// Y + X = 0
DDA(-240, 240, 240, -240);

MYCOLOR = BROWN;
hexagon(-160, 120);
hexagon(160, 120);
hexagon(160, -120);
hexagon(-160, -120);

std::cout << "Enter samples for union jack (SHOULD BE GEATER THAN 100K): ";
int samp;
std::cin >> samp;
//unionJack();
randomUnionJack(samp);

setcolor(WHITE);
setbkcolor(GREEN);
outtextxy(320 - textwidth(algo)/2, 5, algo);
outtextxy(getmaxx()-4 -textwidth(author), getmaxy()-20, author);
setcolor(GREEN);
setbkcolor(BLACK);

int x0, y0, x1, y1;
MYCOLOR = WHITE;
std::cout << "Enter '-400' to exit ";
std::cout << "Enter the coordinates of end points: [x0 y0 x1 y1] ";

std::cin >> x0;
while(x0 != -400) {
std::cin >> y0 >> x1 >> y1;
sprintf(point, "(M, M)", x0, y0);
outtextxy(320+x0-textwidth(point)/2, 240-y0+2, point);
sprintf(point, "(M, M)", x1, y1);
outtextxy(320+x1-textwidth(point)/2, 240-y1+2, point);
DDA(x0, y0, x1, y1);

setcolor(WHITE);
setbkcolor(GREEN);
outtextxy(320 - textwidth(algo)/2, 5, algo);
outtextxy(getmaxx()-4 -textwidth(author), getmaxy()-20, author);
setcolor(GREEN);
setbkcolor(BLACK);

std::cin >> x0;
}

closegraph();
return 0;
}

void unionJack(void){
for(int y = 240; y>0; --y) {
int x = -321;

MYCOLOR = BLUE;
while(++x < -y)
{ putpixel(320+x, 240-y, MYCOLOR); }
MYCOLOR = CYAN;
while(++x < 0)
{ putpixel(320+x, 240-y, MYCOLOR); } MYCOLOR = MAGENTA;
while(++x < y)
{ putpixel(320+x, 240-y, MYCOLOR); }
MYCOLOR = RED;
while(++x < 321)
{ putpixel(320+x, 240-y, MYCOLOR); }
}
for(int y = -1; y>-241; --y) {
int x = -321;

MYCOLOR = RED;
while(++x < y)
{ putpixel(320+x, 240-y, MYCOLOR); }
MYCOLOR = MAGENTA;
while(++x < 0)
{ putpixel(320+x, 240-y, MYCOLOR); }
MYCOLOR = CYAN;
while(++x < -y)
{ putpixel(320+x, 240-y, MYCOLOR); }
MYCOLOR = BLUE;
while(++x < 321)
{ putpixel(320+x, 240-y, MYCOLOR); }
}
}

void hexagon(int xc, int yc) {
DDA(xc+60, yc+104, xc+120, yc);
DDA(xc+60, yc+104, xc+60, yc-104);
DDA(xc+60, yc+104, xc-60, yc-104);
DDA(xc+60, yc+104, xc-120, yc);
DDA(xc+60, yc+104, xc-60, yc+104);
DDA(xc+120, yc, xc+60, yc-104);
DDA(xc+120, yc, xc-60, yc-104);
DDA(xc+120, yc, xc-120, yc);
DDA(xc+120, yc, xc-60, yc+104);
DDA(xc+60, yc-104, xc-60, yc-104);
DDA(xc+60, yc-104, xc-120, yc);
DDA(xc+60, yc-104, xc-60, yc+104);
DDA(xc-60, yc-104, xc-120, yc);
DDA(xc-60, yc-104, xc-60, yc+104);
DDA(xc-120, yc, xc-60, yc+104);
}