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
+14
View File
@@ -0,0 +1,14 @@
# cg/dda - DDA line drawing algorithm
Companion code for https://ankurm.com/c-program-to-implement-dda-line-drawing-algorithm/
| File | What it is | Build |
|---|---|---|
| `src/dda_console.c` | Plain C, no graphics library. Prints the step table and an ASCII plot. | `gcc -std=c11 -Wall -Wextra -o dda_console src/dda_console.c -lm` |
| `src/dda_sdl.c` | SDL2 version. Draws pixels into an off-screen surface and saves a BMP (or `--window`). | `gcc -std=c11 -Wall -Wextra -o dda_sdl src/dda_sdl.c $(sdl2-config --cflags --libs) -lm` |
| `src/dda_graphics_h.cpp` | Corrected Turbo C++ / WinBGIm `graphics.h` version. | Turbo C++ or MinGW + WinBGIm only |
| `legacy/dda_original_2015.cpp` | The original 2015 program, kept for comparison (has an integer-division bug). | same as above |
Usage: `./dda_console x1 y1 x2 y2` (default `2 3 10 8`), `SDL_VIDEODRIVER=dummy ./dda_sdl 2 3 10 8 out.bmp`.
Tested with gcc 13 and SDL 2.30 on Linux. The `graphics.h` programs were only compile-checked against a stub header that prints each `putpixel` call; they were not run against a real BGI driver.
+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;
}
+58
View File
@@ -0,0 +1,58 @@
/* DDA line drawing - console version. No graphics library needed.
* Prints the step table and an ASCII plot.
* Build: gcc -std=c11 -Wall -Wextra -o dda_console src/dda_console.c -lm
* Run: ./dda_console x1 y1 x2 y2 (or run with no arguments for the default 2 3 10 8)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define W 40
#define H 20
int main(int argc, char **argv) {
int x1 = 2, y1 = 3, x2 = 10, y2 = 8;
if (argc == 5) {
x1 = atoi(argv[1]); y1 = atoi(argv[2]);
x2 = atoi(argv[3]); y2 = atoi(argv[4]);
}
int dx = x2 - x1;
int dy = y2 - y1;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy); /* larger of |dx|, |dy| */
if (steps == 0) { /* both endpoints equal */
printf("Start and end are the same point (%d,%d)\n", x1, y1);
return 0;
}
float xinc = (float)dx / steps; /* float division on purpose */
float yinc = (float)dy / steps;
printf("P1=(%d,%d) P2=(%d,%d) dx=%d dy=%d steps=%d xinc=%.3f yinc=%.3f\n\n",
x1, y1, x2, y2, dx, dy, steps, xinc, yinc);
printf("%4s %8s %8s %6s %6s\n", "k", "x", "y", "px", "py");
static char grid[H][W + 1];
for (int r = 0; r < H; r++) {
for (int c = 0; c < W; c++) grid[r][c] = '.';
grid[r][W] = '\0';
}
float x = (float)x1, y = (float)y1;
for (int k = 0; k <= steps; k++) {
int px = (int)lroundf(x);
int py = (int)lroundf(y);
printf("%4d %8.3f %8.3f %6d %6d\n", k, x, y, px, py);
if (px >= 0 && px < W && py >= 0 && py < H)
grid[py][px] = '#';
x += xinc;
y += yinc;
}
printf("\nPlot (origin top-left, y grows downward, '#' = pixel):\n");
int maxrow = (y1 > y2 ? y1 : y2) + 1;
if (maxrow > H) maxrow = H;
for (int r = 0; r < maxrow; r++) printf("%s\n", grid[r]);
return 0;
}
+38
View File
@@ -0,0 +1,38 @@
// DDA line drawing - Turbo C++ / WinBGIm version (graphics.h, BGI).
// Corrected version of the 2015 program: the original divided int by int,
// so the increments truncated to 0 or +/-1 and lines came out horizontal/45-degree.
// Needs Turbo C++ (path to BGI folder below) or MinGW + WinBGIm. Not buildable on modern Linux/macOS.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <iostream.h>
int main(void) {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:\\tc\\bgi");
int x1, y1, x2, y2;
cout << "\n Enter X1,Y1,X2,Y2: ";
cin >> x1 >> y1 >> x2 >> y2;
int dx = x2 - x1;
int dy = y2 - y1;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
if (steps == 0) steps = 1; // single point
float xinc = (float)dx / steps; // float division
float yinc = (float)dy / steps;
float x = x1, y = y1;
for (int i = 0; i <= steps; i++) {
putpixel((int)(x + 0.5), (int)(y + 0.5), WHITE);
x += xinc;
y += yinc;
}
getch();
closegraph();
return 0;
}
+73
View File
@@ -0,0 +1,73 @@
/* DDA line drawing - SDL2 version (modern replacement for graphics.h).
* Draws into an off-screen 64x40 pixel surface (each cell shown 10x10) and saves dda.bmp,
* so it also runs on a machine with no display (SDL_VIDEODRIVER=dummy).
* Build: gcc -std=c11 -Wall -Wextra -o dda_sdl src/dda_sdl.c $(sdl2-config --cflags --libs) -lm
* Run: ./dda_sdl x1 y1 x2 y2 [out.bmp] (default 2 3 10 8 dda.bmp)
* add --window to open a real window and wait for a key/close instead of saving.
*/
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define GW 64
#define GH 40
#define CELL 10
static void cell(SDL_Surface *s, int px, int py, Uint32 col) {
SDL_Rect r = { px * CELL, py * CELL, CELL - 1, CELL - 1 };
SDL_FillRect(s, &r, col);
}
int main(int argc, char **argv) {
int window = 0;
int a[4] = { 2, 3, 10, 8 };
const char *out = "dda.bmp";
int n = 0;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--window")) window = 1;
else if (n < 4) a[n++] = atoi(argv[i]);
else out = argv[i];
}
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
if (SDL_Init(SDL_INIT_VIDEO) != 0) { fprintf(stderr, "SDL_Init: %s\n", SDL_GetError()); return 1; }
SDL_Surface *s = SDL_CreateRGBSurfaceWithFormat(0, GW * CELL, GH * CELL, 32, SDL_PIXELFORMAT_RGBA32);
if (!s) { fprintf(stderr, "surface: %s\n", SDL_GetError()); return 1; }
SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 255, 255, 255));
Uint32 grid = SDL_MapRGB(s->format, 235, 238, 243);
Uint32 ink = SDL_MapRGB(s->format, 74, 124, 199);
for (int gy = 0; gy < GH; gy++)
for (int gx = 0; gx < GW; gx++) cell(s, gx, gy, grid);
int dx = x2 - x1, dy = y2 - y1;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
if (steps == 0) steps = 1;
float xinc = (float)dx / steps, yinc = (float)dy / steps;
float x = (float)x1, y = (float)y1;
int plotted = 0;
for (int i = 0; i <= steps; i++) {
int px = (int)lroundf(x), py = (int)lroundf(y);
if (px >= 0 && px < GW && py >= 0 && py < GH) { cell(s, px, py, ink); plotted++; }
x += xinc; y += yinc;
}
printf("DDA (%d,%d)->(%d,%d): steps=%d, pixels plotted=%d\n", x1, y1, x2, y2, steps, plotted);
if (!window) {
if (SDL_SaveBMP(s, out) != 0) { fprintf(stderr, "save: %s\n", SDL_GetError()); return 1; }
printf("saved %s (%dx%d)\n", out, s->w, s->h);
} else {
SDL_Window *w = SDL_CreateWindow("DDA", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, s->w, s->h, 0);
SDL_Surface *ws = SDL_GetWindowSurface(w);
SDL_BlitSurface(s, NULL, ws, NULL);
SDL_UpdateWindowSurface(w);
SDL_Event e;
while (SDL_WaitEvent(&e) && e.type != SDL_QUIT && e.type != SDL_KEYDOWN) {}
SDL_DestroyWindow(w);
}
SDL_FreeSurface(s);
SDL_Quit();
return 0;
}