DDA Line Drawing Algorithm in C++ using GLUT Basic
DDA Line Drawing Algorithm Given 2 point (x1, y1) to (x2,y2)
1.Read
Line end point (x1,y1)(x2,y2)
2.∆x
= |x2-x1| , ∆y = |y2-y1|
3. If(∆x
≥ ∆y)
Then Length = ∆x
Else
Length = ∆y
5.Select
Raster Unit
1.∆x
= x2-x1 / Length
2.∆y
= y2-y1 / Length
3.This
will make ∆x or ∆y = 1
6.X_(i+1) = x_i+0.5∗Sign(∆x) //Sign retrun 1,0,-1
7.Y_(i+1) = y_i+0.5 ∗Sign(∆y)
8. i=0
whlie(i<=length)
{
plot(x,y)
x = x + ∆x
y = y + ∆y
i=i+1
}
Code Basic
#include<GL/glut.h>
#include<math.h>
#include<iostream>
using namespace std;
float x_1, x_2, y_1 ,y_2;
int sgn(float a){
if(a==0){
return 0;
}
if(a<0){
return -1;
}
else
return 1;
}
void Line(){
float dy,dx, length;
dy = y_2 - y_1;
dx = x_2 - x_1;
if(abs(dx)>=abs(dy)){
length = abs(dx);
}
else{
length = abs(dy);
}
float xin,yin;
xin = (x_2-x_1)/length;
yin = (y_2-y_1)/length;
float x,y;
x = x_1 + 0.5 * sgn(xin);
y = y_1 + 0.5 * sgn(yin);
int i = 0;
while(i<=length){
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
#include<iostream>
using namespace std;
float x_1, x_2, y_1 ,y_2;
int sgn(float a){
if(a==0){
return 0;
}
if(a<0){
return -1;
}
else
return 1;
}
void Line(){
float dy,dx, length;
dy = y_2 - y_1;
dx = x_2 - x_1;
if(abs(dx)>=abs(dy)){
length = abs(dx);
}
else{
length = abs(dy);
}
float xin,yin;
xin = (x_2-x_1)/length;
yin = (y_2-y_1)/length;
float x,y;
x = x_1 + 0.5 * sgn(xin);
y = y_1 + 0.5 * sgn(yin);
int i = 0;
while(i<=length){
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
x = x + xin;
y = y + yin;
i++;
}
glFlush();
}
void init(void)
{
glClearColor(0,0,0,0);
glColor3f(1.0,0.0,0.0);
gluOrtho2D(0,500,0,400);
glClear(GL_COLOR_BUFFER_BIT);
}
int main(int argc,char** argv ){
cout<<" Enter x1, y1 point";
cin>>x_1>>y_1;
cout<<"\n Enter x2, y2 point";
cin>>x_2>>y_2;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,400);
glutCreateWindow("DDA Line");
init();
glutDisplayFunc(Line);
glutMainLoop();
return 0;
}
Comments
Post a Comment