Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AakashCode12/e524d3559053aea17afa6d4dc071fa1c to your computer and use it in GitHub Desktop.
Save AakashCode12/e524d3559053aea17afa6d4dc071fa1c to your computer and use it in GitHub Desktop.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>
void dda (int x1,int y1, int x2, int y2);
void dda (int x1,int y1, int x2, int y2)
{
int i,dx,dy,steps;
float x,y;
float xinc,yinc;
dx=(x2-x1);
dy=(y2-y1);
if(abs(dx)>=abs(dy))
{
steps=dx;
}
else
steps=dy;
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;
y=y1;
putpixel(x,y,WHITE);
for(i=1;i<steps;i++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,WHITE) ;
}
}
void dda_dotted(int x1, int y1, int x2, int y2);
void dda_dotted(int x1,int y1, int x2, int y2){
int i,dx,dy,steps;
float x,y;
float xinc,yinc;
dx=(x2-x1);
dy=(y2-y1);
if ( abs(dx)>= abs(dy))
steps=dx;
else
steps=dy;
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;
y=y1;
putpixel(x,y,WHITE);
for( i=1; i<steps; i++){
if(i%2==0)
{
putpixel(x,y,WHITE);
}
x= x+ xinc;
y= y+yinc;
}
}
void dda_dashed(int x1, int y1, int x2, int y2);
void dda_dashed(int x1,int y1, int x2, int y2){
int i,dx,dy,steps;
float x,y;
float xinc,yinc;
dx=(x2-x1);
dy=(y2-y1);
if ( abs(dx)>= abs(dy))
steps=dx;
else
steps=dy;
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;
y=y1;
putpixel(x,y,WHITE);
for( i=1; i<steps; i++){
if(i%4==0)
{
putpixel(x,y,WHITE);
}
x= x+ xinc;
y= y+yinc;
}
}
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,option;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
cleardevice();
cout<< "DDA Line Generation Algorithm";
cout<<"\n Enter the starting co-ordinates for drawing a line";
cin>>x1>>y1;
cout<<"\n Enter the ending co-ordinates";
cin>>x2>>y2;
cout<<"\nEnter your options:\n1) Normal DDA\n2) Dotted \n3) Dashed\n";
cin>>option;
switch(option){
case 1: dda(x1,y1,x2,y2);
break;
case 2:dda_dotted(x1,y1,x2,y2);
break;
case 3:dda_dashed(x1,y1,x2,y2);
break;
default: cout<<"\n Invalid option";
break;
}
getch();
closegraph();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment