Digital differential analyzer (graphics algorithm): Difference between revisions

Content deleted Content added
Program: Remove "DETECT" (vandalism)
Tag: Reverted
Program: The DDA algorithm requires rounding the floating point x,y to the nearest integer. The sample code should not depend on the behavior of some specific graphics library's putpixel() (for example if it takes int parameters, float->int uses truncation and will produce the wrong answer)
 
(9 intermediate revisions by 9 users not shown)
Line 8:
:<math>m = \frac{y_{\rm end} -y_{\rm start}}{x_{\rm end}-x_{\rm start}}</math>
 
In fact any two consecutive points (x,y) lying on this line segment should satisfy the equation.
 
== Performance ==
Line 34:
 
== Program ==
DDA algorithm Programprogram in Turbo [[C++]]:
 
<syntaxhighlight lang="c++cpp" line="1">
#include <graphics.h>
 
#include <iostream.h>
#include <math.h>
Line 44 ⟶ 43:
#include <conio.h>
 
void main( )
{
float x,
float y,
float x1, y1,
float x2, y2, dx, dy, step;
int i, gd = 0DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
cout << "Enter the value of x1 and y1 : ";
cin >> x1 >> y1;
cout << "Enter the value of x2 and y2: ";
Line 57 ⟶ 59:
dx = (x2 - x1);
dy = (y2 - y1);
 
if (abs(dx) >= abs(dy))
step = abs(dx);
else
step = abs(dy);
 
dx = dx / step;
dy = dy / step;
x = x1;
y = y1;
i = 10;
 
while (i <= step) {
putpixel(round(x), round(y), 5);
x = x + dx;
y = y + dy;
Line 73 ⟶ 78:
delay(100);
}
 
getch();
closegraph();
Line 81 ⟶ 87:
 
* [[Bresenham's line algorithm]] is an algorithm for line rendering.
* [[incrementalIncremental error algorithm]]
* [[Xiaolin Wu's line algorithm]] is an algorithm for line anti-aliasing
 
Line 94 ⟶ 100:
[[Category:Computer graphics algorithms]]
[[Category:Digital geometry]]
[[Category:Articles with example C++ code]]