-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineDDA.cpp
More file actions
83 lines (79 loc) · 1.87 KB
/
lineDDA.cpp
File metadata and controls
83 lines (79 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Line drawing using DDA algorithm
Author:Sadeed Ameen PO
Roll No:267
*/
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include<iostream>
using namespace std;
double X1, Y1, X2, Y2;
float round_value(float v)
{
return floor(v + 0.5);
}
void LineDDA(void)
{
double dx=(X2-X1);
double dy=(Y2-Y1);
double steps;
float xInc,yInc,x=X1,y=Y1;
/* Find out whether to increment x or y */
steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
/* Plot the points */
glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(x,y);
int i;
/* For every step, find an intermediate vertex */
for(i=0;i<steps;i++)
{
x+=xInc;
y+=yInc;
glVertex2d(round_value(x), round_value(y));
cout<<"x="<<round_value(x);
}
glEnd();
glFlush();
}
void Init()
{
/* Set clear color to white */
glClearColor(1.0,1.0,1.0,0);
// Set fill color
glColor3f(0.0,0.0,1.0);
gluOrtho2D(0 , 640 , 0 , 480);
}
int main(int argc, char **argv)
{
cout<<"Enter two end points of the line to be drawn:\n";
cout<<"\n************************************";
cout<<"\nEnter Point1( X1 , Y1):\n";
cin>>X1;
cin>>Y1;
cout<<"\n************************************";
cout<<"\nEnter Point1( X2 , Y2):\n";
cin>>X2;
cin>>Y2;
/* Initialise GLUT library */
glutInit(&argc,argv);
/* Set the initial display mode */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
/* Set the initial window position and size */
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
/* Create the window with title "DDA_Line" */
glutCreateWindow("Line Drawing using DDA");
/* Initialize drawing colors */
Init();
/* Call the displaying function */
glutDisplayFunc(LineDDA);
/* Keep displaying untill the program is closed */
glutMainLoop();
}
//End of program