0% found this document useful (0 votes)
8 views15 pages

PPPP

The document contains various C programs demonstrating basic computer graphics techniques, including initializing graphics, drawing lines using DDA and Bresenham's algorithms, drawing arcs and circles using built-in functions, and implementing Bresenham's circle drawing algorithm. Each program includes code snippets and prompts for user input to specify coordinates or parameters. The programs utilize the graphics.h library for rendering graphics on the screen.

Uploaded by

mevoni8999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

PPPP

The document contains various C programs demonstrating basic computer graphics techniques, including initializing graphics, drawing lines using DDA and Bresenham's algorithms, drawing arcs and circles using built-in functions, and implementing Bresenham's circle drawing algorithm. Each program includes code snippets and prompts for user input to specify coordinates or parameters. The programs utilize the graphics.h library for rendering graphics on the screen.

Uploaded by

mevoni8999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Program Name: - WAP to write a basic structure computer graphics program.

CODE

#include <graphics.h>
#include <conio.h>

int main() {

int gd = DETECT, gm;


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

// Close the graphics system


getch();
closegraph();
return 0;
}
Program Name: - WAP to implement DDA line Algorithm.

CODE
#include <stdio.h>
#include <math.h>

int main() {
int x1, y1, x2, y2, dx, dy, steps, k;
float xIncrement, yIncrement, x, y;

printf("Enter the starting coordinates (x1, y1): ");


scanf("%d %d", &x1, &y1);
printf("Enter the ending coordinates (x2, y2): ");
scanf("%d %d", &x2, &y2);

dx = x2 - x1;
dy = y2 - y1;

if (abs(dx) > abs(dy)) {


steps = abs(dx);
} else {
steps = abs(dy);
}

xIncrement = (float)dx / steps;


yIncrement = (float)dy / steps;

x = x1;
y = y1;

printf("DDA Line Drawing Points:\n");

for (k = 0; k < steps; k++) {


printf("(%.0f, %.0f)\n", x, y);
x += xIncrement;
y += yIncrement;
}

printf("(%d, %d)\n", x2, y2);

return 0;
}
Program Name: - WAP to implement Bresenham’s line drawing Algorithms.

#include <stdio.h>
#include <stdlib.h>

int main() {
int x1, y1, x2, y2, dx, dy, sx, sy, err, e2, currentX, currentY;

printf("Enter the starting coordinates (x1, y1): ");


scanf("%d %d", &x1, &y1);
printf("Enter the ending coordinates (x2, y2): ");
scanf("%d %d", &x2, &y2);

dx = abs(x2 - x1);
dy = abs(y2 - y1);
sx = (x1 < x2) ? 1 : -1;
sy = (y1 < y2) ? 1 : -1;
err = dx - dy;
currentX = x1;
currentY = y1;

printf("Bresenham's Line Drawing Points:\n");

while (1) {
printf("(%d, %d)\n", currentX, currentY);

if (currentX == x2 && currentY == y2) {


break;
}

e2 = 2 * err;

if (e2 > -dy) {


err = err - dy;
currentX = currentX + sx;
}

if (e2 < dx) {


err = err + dx;
currentY = currentY + sy;
}
}
return 0;
}

Output
Program Name: - WAP to draw a line using line() function

CODE
#include <graphics.h>
#include <conio.h>

int main() {
// Initialize the graphics system
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

// Define the line endpoints


int x1 = 100, y1 = 100; // Starting point
int x2 = 300, y2 = 200; // Ending point

// Draw a line using the line function


line(x1, y1, x2, y2);

// Pause to view the output


getch();

// Close the graphics system


closegraph();
return 0;
}
Program Name: - WAP to draw arc using arc() function

CODE

#include <graphics.h>
#include <conio.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics mode

// Define the parameters for the arc:


// center (x, y), radius, startAngle, endAngle
int x = 300; // x-coordinate of the center
int y = 200; // y-coordinate of the center
int radius = 100; // radius of the arc
int startAngle = 0; // starting angle of the arc
int endAngle = 180; // ending angle of the arc

// Draw the arc


arc(x, y, startAngle, endAngle, radius);

// Wait for a key press


getch();

// Close the graphics window


closegraph();
return 0;
}
OUTPUT

PASTE YOUR OUTPUT IN THIS BLOCK


Program Name: - WAP to implement Bresenham’s circle drawing Algorithms.

CODE

#include <graphics.h>
#include <conio.h>
#include <math.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // Initialize graphics

int x_center = 250; // X coordinate of center


int y_center = 250; // Y coordinate of center
int radius = 100; // Radius of the circle

int x = 0;
int y = radius;
int d = 3 - 2 * radius; // Initial decision parameter

// Draw the circle using the symmetry


while (x <= y) {
putpixel(x_center + x, y_center + y, WHITE);
putpixel(x_center - x, y_center + y, WHITE);
putpixel(x_center + x, y_center - y, WHITE);
putpixel(x_center - x, y_center - y, WHITE);
putpixel(x_center + y, y_center + x, WHITE);
putpixel(x_center - y, y_center + x, WHITE);
putpixel(x_center + y, y_center - x, WHITE);
putpixel(x_center - y, y_center - x, WHITE);

if (d < 0) {
d = d + 4 * x + 6; // Choose East pixel
} else {
d = d + 4 * (x - y) + 10; // Choose South-East pixel
y--; // Move in Y direction
}
x++; // Move in X direction
}

getch(); // Wait for a key press


closegraph(); // Close the graphics window
return 0;
}
Output
Program Name: - WAP to draw circle using circle() function.

CODE
#include <graphics.h>
#include <conio.h>
#include <math.h>

int main() {
int gd = DETECT, gm;
int x_center = 300; // X coordinate of circle center
int y_center = 300; // Y coordinate of circle center
int radius = 100; // Radius of the circle

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

for(int angle = 0; angle < 360; angle++) {


float rad = angle * (M_PI / 180); // Convert degrees to radians
int x = x_center + radius * cos(rad); // Calculate x coordinate
int y = y_center + radius * sin(rad); // Calculate y coordinate
putpixel(x, y, WHITE); // Plot the pixel
}

getch();

closegraph();
return 0;
}
Output

You might also like