1.
DDA line generation C Program
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void drawLineDDA(int X0, int Y0, int X1, int Y1) {
int dx = X1 - X0;
int dy = Y1 - Y0;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float Xincrement = dx / (float) steps;
float Yincrement = dy / (float) steps;
float x = X0;
float y = Y0;
for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE);
x += Xincrement;
y += Yincrement;
delay(10); // Small delay for visualization
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Update path as needed
int X0, Y0, X1, Y1;
printf("Enter the starting point (X0, Y0): ");
scanf("%d %d", &X0, &Y0);
printf("Enter the ending point (X1, Y1): ");
scanf("%d %d", &X1, &Y1);
drawLineDDA(X0, Y0, X1, Y1);
getch(); // Wait for user input before closing the graphics window
closegraph();
return 0;
}
2. Bresenham's Line Generation
#include <stdio.h>
void drawLine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int i1 = 2 * dy;
int i2 = 2 * (dy - dx);
int d = i1 - dx;
int x = x1, y = y1;
int xend;
if (dx < 0) {
x = x2;
y = y2;
xend = x1;
} else {
x = x1;
y = y1;
xend = x2;
}
printf("(%d, %d)\n", x, y);
while (x <= xend) {
if (d < 0) {
d += i1;
} else {
d += i2;
y++;
x++;
printf("(%d, %d)\n", x, y);
int main() {
int x1, y1, x2, y2;
printf("Enter the coordinates of the starting point (x1, y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter the coordinates of the ending point (x2, y2): ");
scanf("%d %d", &x2, &y2);
drawLine(x1, y1, x2, y2);
return 0;
3. Bresenhams Circle drawing algorithm
#include <stdio.h>
#include <graphics.h>
// Function to draw circle using Bresenham's algorithm
void drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
drawPixel(xc, yc, x, y);
while (y >= x) {
// For each pixel we will draw all eight pixels
x++;
// Check for decision parameter and update
// accordingly
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
} else {
d = d + 4 * x + 6;
}
drawPixel(xc, yc, x, y);
}
}
// Function to draw all eight pixels
void drawPixel(int xc, int yc, int x, int y) {
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
}
int main() {
int gd = DETECT, gm;
int xc = 200, yc = 200, r = 100;
initgraph(&gd, &gm, "");
drawCircle(xc, yc, r);
getch();
closegraph();
return 0;
}
4. Mid point circle drawing algorithm
#include <stdio.h>
#include <graphics.h>
// Function to draw circle using Midpoint Circle Drawing Algorithm
void drawCircle(int xc, int yc, int r) {
int x = r, y = 0;
int P = 1 - r;
drawPixel(xc, yc, x, y);
while (x > y) {
y++;
if (P <= 0) {
P = P + 2*y + 1;
} else {
x--;
P = P + 2*y - 2*x + 1;
}
drawPixel(xc, yc, x, y);
}
}
// Function to draw all eight pixels
void drawPixel(int xc, int yc, int x, int y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
}
int main() {
int gd = DETECT, gm;
int xc = 200, yc = 200, r = 100;
initgraph(&gd, &gm, "");
drawCircle(xc, yc, r);
getch();
closegraph();
return 0;
}