Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Table of contains

Lab Lab content


No.
1 Program to implement DDA Line Drawing Algorithm
2 Program to implement Bresenham's Line Drawing
Algorithm
3 Program to draw a circle using Midpoint Algorithm
4 program to implement 2d reflection code
5 program to rotate a line in a computer Graphics
6 OpenGL program to Implement Brenham’s line drawing
algorithm
1. Program to implement DDA Line Drawing Algorithm

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

voidDDA_line(int x1, int y1, int x2, int y2) {


// calculate the number of steps needed
int steps = fmax(abs(x2 - x1), abs(y2 - y1));

// calculate the increment for each step in x and y


float dx = (float)(x2 - x1) / steps;
floatdy = (float)(y2 - y1) / steps;

// start the line from (x1, y1)


float x = x1;
float y = y1;

// plot each point of the line


for (int i = 0; i < steps; i++) {
x += dx;
y += dy;
printf("(%d, %d)\n", (int)round(x), (int)round(y));
}
}

int main() {
int x1 = 2, y1 = 2, x2 = 8, y2 = 5;
DDA_line(x1, y1, x2, y2);
return 0;
}
2. Program to implement Bresenham's Line Drawing Algorithm

#include <stdio.h>
#include <graphics.h>

voidBresenham(int x1, int y1, int x2, int y2) {


int dx = x2 - x1;
intdy = y2 - y1;
int d = 2 * dy - dx;
intincrE = 2 * dy;
intincrNE = 2 * (dy - dx);
int x = x1, y = y1;
putpixel(x, y, WHITE);
while (x < x2) {
if (d <= 0) {
d += incrE;
x++;
} else {
d += incrNE;
x++;
y++;
}
putpixel(x, y, WHITE);
}
}

int main() {
intgd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1, y1, x2, y2;
printf("Enter the start point (x1, y1) of the line: ");
scanf("%d%d", &x1, &y1);
printf("Enter the end point (x2, y2) of the line: ");
scanf("%d%d", &x2, &y2);
Bresenham(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}

This program uses the graphics library graphics.h to draw a line using Bresenham's line drawing
algorithm. The user inputs the start and end points (x1, y1) and (x2, y2) of the line. The Bresenham
function implements the algorithm, which calculates the intermediate points on the line using the
incremental approach. The putpixel function plots a pixel at the specified coordinates. The initgraph
function initializes the graphics system and creates a graphics window. The getch function waits for a
key press. The closegraph function closes the graphics system.
3. Program to draw a circle using Midpoint Algorithm

#include <stdio.h>

voidBresenham_line(int x1, int y1, int x2, int y2) {


int dx = x2 - x1;
intdy = y2 - y1;
int p = 2 * dy - dx;
int x = x1;
int y = y1;

while (x <= x2) {


printf("(%d, %d)\n", x, y);
x++;
if (p < 0) {
p += 2 * dy;
} else {
y++;
p += 2 * (dy - dx);
}
}
}

int main() {
int x1 = 2, y1 = 2, x2 = 8, y2 = 5;
Bresenham_line(x1, y1, x2, y2);
return 0;
}

This program uses the graphics library graphics.h to draw the circle on the screen. The user inputs the
center (xc, yc) and the radius r of the circle. The midpoint algorithm is then used to plot the points of
the circle. The putpixel function is used to set a pixel at the specified location with a specified color.

4. WAP Program to implement 2d reflection code.

#include <graphics.h>
#include <stdio.h>

voidreflect_x(int x, int y, intx_ref) {


intx_reflected = 2 * x_ref - x;
putpixel(x_reflected, y, WHITE);
}
voidreflect_y(int x, int y, inty_ref) {
inty_reflected = 2 * y_ref - y;
putpixel(x, y_reflected, WHITE);
}

int main() {
intgd = DETECT, gm;
initgraph(&gd, &gm, "");

int x, y;
intx_ref = getmaxx() / 2;
inty_ref = getmaxy() / 2;
printf("Enter a point (x, y): ");
scanf("%d%d", &x, &y);
reflect_x(x, y, x_ref);
reflect_y(x, y, y_ref);

getch();
closegraph();
return 0;
}
This program uses the graphics library graphics.h to reflect a point (x, y) about the x-axis and the y-
axis. The user inputs a point (x, y). The program then calculates the reflected point about the x-axis
and the y-axis, using the formulas x_reflected = 2 * x_ref - x and y_reflected = 2 * y_ref - y
respectively, where x_ref and y_ref are the x and y coordinates of the reflection axes. The putpixel
function is used to set a pixel at the specified location with a specified color.
5. program to rotate a line in a computer Graphics.

#include <graphics.h>
#include <math.h>
#include <stdio.h>
voidrotate_line(int x1, int y1, int x2, int y2, int angle) {
int x1_new = (x1 * cos(angle) - y1 * sin(angle));
int y1_new = (x1 * sin(angle) + y1 * cos(angle));
int x2_new = (x2 * cos(angle) - y2 * sin(angle));
int y2_new = (x2 * sin(angle) + y2 * cos(angle));
line(x1_new, y1_new, x2_new, y2_new);
}
int main() {
intgd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1, y1, x2, y2, angle;
printf("Enter the start point (x1, y1) of the line: ");
scanf("%d%d", &x1, &y1);
printf("Enter the end point (x2, y2) of the line: ");
scanf("%d%d", &x2, &y2);
printf("Enter the angle of rotation in degrees: ");
scanf("%d", &angle);
angle = angle * 3.14159265 / 180;
rotate_line(x1, y1, x2, y2, angle);
getch();
closegraph();
return 0;
}

This program uses the graphics library graphics.h to rotate a line. The user inputs the start and end
points (x1, y1) and (x2, y2) of the line, and the angle of rotation angle in degrees. The program
then calculates the new start and end points after rotation, using the formula (x_new, y_new) = (x
* cos(angle) - y * sin(angle), x * sin(angle) + y * cos(angle)). The line function is used to draw a
line from the start point to the end point. The cos and sin functions are from the math library
math.h.
6. OpenGL program to Implement Brenham’s line drawing algorithm.
#include <GL/glut.h>
#include <stdio.h>

voidBresenham(int x1, int y1, int x2, int y2) {


int dx = x2 - x1;
intdy = y2 - y1;
int d = 2 * dy - dx;
intincrE = 2 * dy;
intincrNE = 2 * (dy - dx);
int x = x1, y = y1;
glBegin(GL_POINTS);
glVertex2i(x, y);
while (x < x2) {
if (d <= 0) {
d += incrE;
x++;
} else {
d += incrNE;
x++;
y++;
}
glVertex2i(x, y);
}
glEnd();
glFlush();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
int x1, y1, x2, y2;
printf("Enter the start point (x1, y1) of the line: ");
scanf("%d%d", &x1, &y1);
printf("Enter the end point (x2, y2) of the line: ");
scanf("%d%d", &x2, &y2);
Bresenham(x1, y1, x2, y2);
}

int main(intargc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing Algorithm");
glClearColor(0.0, 0.0, 0.0, 0.0);
gluOrtho2D(-250, 250, -250, 250);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

This program uses the OpenGL library glut.h to draw a line using Bresenham's line drawing
algorithm. The user inputs the start and end points (x1, y1) and (x2, y2) of the line. The
Bresenham function implements the algorithm, which calculates the intermediate points on the
line using the incremental approach. The glBegin and glEnd functions are used to specify the start
and end of a set of vertices that define a primitive. The glVertex2i function sets a vertex with
integer coordinates. The glFlush function forces execution of all OpenGL commands in the
pipeline. The glClear function clears the color buffer. The glClearColor function sets the clear
color for the color buffer. The gluOrtho2D function sets up a 2D orthographic projection matrix.
The glutDisplayFunc function sets the display callback for the current window. The
glutMainLoop function enters the GLUT event processing loop.

You might also like