Add cg/dda: DDA line drawing in C (console), SDL2 and graphics.h with the original 2015 program for comparison

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3obqqePfZ9m2Jk5Ub
This commit is contained in:
2026-11-26 18:07:04 +05:30
co-authored by Claude Sonnet 5.5
parent 9ed8479cec
commit 0c77aad947
5 changed files with 237 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
// The ORIGINAL 2015 program exactly as first published (kept for comparison).
// Known bug: dx = dx / length and dy = dy / length are int / int, so the increments truncate.
//DDA
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int main(void) {
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph( & gdriver, & gmode, "C:\\tc\\bgi");
cout << "\n Enter X1,Y1,X2,Y2";
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int dx = x2 - x1;
int dy = y2 - y1;
int length;
if (dx >= dy)
length = dx;
else
length = dy;
dx = dx / length;
dy = dy / length;
int sx;
if (dx >= 0)
sx = 1;
else
sx = -1;
int sy;
if (dy >= 0)
sy = 1;
else
sy = -1;
float x = x1 + 0.5 * (sx);
float y = y1 + 0.5 * (sy);
int i = 0;
while (i <= length) {
putpixel(int(x), int(y), 15);
x = x + dx;
y = y + dy;
i = i + 1;
}
/* clean up */
getch();
closegraph();
return 0;
}