What is the code in c plus plus for line drawing algorithm using OpenGL?

1 answer

Answer

1050086

2026-06-06 10:25

+ Follow

Here is the C code for DDA line drawing...

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

int gd, gm;

float x, y, dx, dy, len;

int x1, y1, x2, y2, i;

detectgraph(&gd, &gm);

initgraph(&gd, &gm,"");

printf("\n Enter the coordinates of line : ");

scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

dx = abs(x2-x1);

dy = abs(y2-y1);

if (dx >= dy) len = dx;

else len = dy;

dx = (x2-x1)/len;

dy = (y2-y1)/len;

x = x1 + 0.5;

y = y1 + 0.5;

i = 1;

while (i <= len)

{

putpixel(x, y, 6);

x = x+dx;

y = y+dy;

i++;

}

getch();

}

-Suraj.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.