0% found this document useful (0 votes)
54 views23 pages

CG Practical Ayush

The document discusses several computer graphics algorithms: 1) Digital Differential Analyzer (DDA) algorithm for drawing lines by calculating incremental values of x and y over steps. 2) Bresenham's line drawing algorithm which produces vector graphics by applying decision parameters to efficiently draw lines with integer coordinates. 3) Midpoint line drawing algorithm which uses the midpoint formula to determine when to increment x or y and produce a line efficiently. 4) Circle drawing algorithms including Bresenham's and midpoint approaches for vector graphics with integer-based coordinates. 5) Boundary fill and flood fill algorithms for filling enclosed regions using recursion or a stack data structure. 6) Cohen-Suther

Uploaded by

Ayush Srivastava
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)
54 views23 pages

CG Practical Ayush

The document discusses several computer graphics algorithms: 1) Digital Differential Analyzer (DDA) algorithm for drawing lines by calculating incremental values of x and y over steps. 2) Bresenham's line drawing algorithm which produces vector graphics by applying decision parameters to efficiently draw lines with integer coordinates. 3) Midpoint line drawing algorithm which uses the midpoint formula to determine when to increment x or y and produce a line efficiently. 4) Circle drawing algorithms including Bresenham's and midpoint approaches for vector graphics with integer-based coordinates. 5) Boundary fill and flood fill algorithms for filling enclosed regions using recursion or a stack data structure. 6) Cohen-Suther

Uploaded by

Ayush Srivastava
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/ 23

Digital Differential Analyzer

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

void drawLineDDA(int x1, int y1, int x2, int y2) {


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

int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

float xIncrement = (float)dx / steps;


float yIncrement = (float)dy / steps;

float x = x1;
float y = y1;

for (int i = 0; i <= steps; i++) {


putpixel((int)x, (int)y, WHITE); // Typecasting to int
x += xIncrement;
y += yIncrement;
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");

int x1, y1, x2, y2;

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


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

printf("Enter the ending coordinates (x2 y2): ");


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

drawLineDDA(x1, y1, x2, y2);

getch();
return 0;
}
Output DDA

Bresenham's Line Drawing Algorithm

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

void drawLineBresenham(int x1, int y1, int x2, int y2) {


int dx = abs(x2 - x1);
int dy = abs(y2 - y1);

int twoDy = 2 * dy;


int twoDyMinusDx = 2 * (dy - dx);

int x, y, p, xEnd;

// Determine the direction of the line


if (x1 > x2) {
x = x2;
y = y2;
xEnd = x1;
} else {
x = x1;
y = y1;
xEnd = x2;
}

putpixel(x, y, WHITE);

// Initial decision parameter


p = 2 * dy - dx;

// For lines with slope between 0 and 1


if (dy <= dx) {
while (x < xEnd) {
x++;

if (p < 0)
p += twoDy;
else {
y++;
p += twoDyMinusDx;
}

putpixel(x, y, WHITE);
}
}
// For lines with slope greater than 1
else {
while (y < y2) {
y++;

if (p < 0)
p += 2 * dx;
else {
x++;
p += twoDyMinusDx;
}

putpixel(x, y, WHITE);
}
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");
int x1, y1, x2, y2;

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


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

printf("Enter the ending coordinates (x2 y2): ");


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

drawLineBresenham(x1, y1, x2, y2);

getch();
closegraph();
return 0;
}

Output

Midpoint Line Drawing Algorithm

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

void drawLineMidpoint(int x1, int y1, int x2, int y2) {


int dx = abs(x2 - x1);
int dy = abs(y2 - y1);

int twoDy = 2 * dy;


int twoDyMinusDx = 2 * (dy - dx);

int x, y, p, xEnd;

// Determine the direction of the line


if (x1 > x2) {
x = x2;
y = y2;
xEnd = x1;
} else {
x = x1;
y = y1;
xEnd = x2;
}

putpixel(x, y, WHITE);

// Initial decision parameter


p = 2 * dy - dx;

while (x < xEnd) {


x++;

if (p < 0)
p += twoDy;
else {
y++;
p += twoDyMinusDx;
}

putpixel(x, y, WHITE);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");

int x1, y1, x2, y2;

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


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

printf("Enter the ending coordinates (x2 y2): ");


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

drawLineMidpoint(x1, y1, x2, y2);

getch();
return 0;
}

Output

Bresenham's Circle Drawing Algorithm

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

void drawCircle(int xc, int yc, int x, int y)


{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}

void circleBres(int xc, int yc, int r)


{
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
{
x++;
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(50);
}
}

// Driver code
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");

int xc, yc, radius;

printf("Enter the center coordinates (xc yc): ");


scanf("%d %d", &xc, &yc);

printf("Enter the radius of the circle: ");


scanf("%d", &radius);

circleBres(xc, yc, radius);

getch();
return 0;

Output

Mid-point Circle Drawing Algorithm

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

void drawCircle(int xc, int yc, int radius) {


int x = radius;
int y = 0;
int p = 1 - radius;

while (x >= 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);

y++;

if (p <= 0)
p = p + 2*y + 1;
else {
x--;
p = p + 2*y - 2*x + 1;
}
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

int xc, yc, radius;

printf("Enter the center coordinates (xc yc): ");


scanf("%d %d", &xc, &yc);

printf("Enter the radius of the circle: ");


scanf("%d", &radius);

drawCircle(xc, yc, radius);

getch();
closegraph();
return 0;
}
Boundary Fill Algorithm (Using recursive approcah)

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

void boundaryFill(int x, int y, int fill_color, int boundary_color) {


if (getpixel(x, y) != boundary_color && getpixel(x, y) != fill_color) {
putpixel(x, y, fill_color);
boundaryFill(x + 1, y, fill_color, boundary_color);
boundaryFill(x - 1, y, fill_color, boundary_color);
boundaryFill(x, y + 1, fill_color, boundary_color);
boundaryFill(x, y - 1, fill_color, boundary_color);
boundaryFill(x + 1, y+1, fill_color, boundary_color);
boundaryFill(x - 1, y+1, fill_color, boundary_color);
boundaryFill(x+1, y - 1, fill_color, boundary_color);
boundaryFill(x-1, y - 1, fill_color, boundary_color);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

rectangle(50, 50, 150, 150); // Draw a rectangle as a boundary

int fill_x = 100, fill_y = 100; // Point inside the rectangle


int fill_color = RED; // Fill color
int boundary_color = WHITE; // Boundary color

boundaryFill(fill_x, fill_y, fill_color, boundary_color);

getch();
closegraph();
return 0;
}

Output

Stack Based
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

// Stack data structure for boundary fill


typedef struct {
int x;
int y;
} Point;

// Function to push a point onto the stack


void push(Point stack[], int *top, int x, int y) {
(*top)++;
stack[*top].x = x;
stack[*top].y = y;
}

// Function to pop a point from the stack


Point pop(Point stack[], int *top) {
Point popped = stack[*top];
(*top)--;
return popped;
}

void boundaryFill(int x, int y, int fill_color, int boundary_color) {


Point stack[1000]; // Adjust the size based on your needs
int top = -1;

push(stack, &top, x, y);

while (top != -1) {


Point current = pop(stack, &top);

if (getpixel(current.x, current.y) != boundary_color &&


getpixel(current.x, current.y) != fill_color) {
putpixel(current.x, current.y, fill_color);

push(stack, &top, current.x + 1, current.y);


push(stack, &top, current.x - 1, current.y);
push(stack, &top, current.x, current.y + 1);
push(stack, &top, current.x, current.y - 1);
}
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

rectangle(50, 50, 150, 150); // Draw a rectangle as a boundary

int fill_x = 100, fill_y = 100; // Point inside the rectangle


int fill_color = RED; // Fill color
int boundary_color = WHITE; // Boundary color

boundaryFill(fill_x, fill_y, fill_color, boundary_color);

getch();
closegraph();
return 0;
}
Flood Fill
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void floodFill(int x, int y, int fill_color, int old_color) {


if (getpixel(x, y) == old_color && getpixel(x, y) != fill_color) {
putpixel(x, y, fill_color);
floodFill(x + 1, y, fill_color, old_color);
floodFill(x - 1, y, fill_color, old_color);
floodFill(x, y + 1, fill_color, old_color);
floodFill(x, y - 1, fill_color, old_color);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

rectangle(50, 50, 150, 150); // Draw a rectangle

int fill_x = 100, fill_y = 100; // Point inside the rectangle


int fill_color = GREEN; // Fill color
int old_color = BLACK; // Old color

floodFill(fill_x, fill_y, fill_color, old_color);

getch();
closegraph();
return 0;
}

Output
Cohen Sutherland Clipping Algorithm

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

#define INSIDE 0 // 0000


#define LEFT 1 // 0001
#define RIGHT 2 // 0010
#define BOTTOM 4 // 0100
#define TOP 8 // 1000

int xmin, ymin, xmax, ymax;

int computeCode(int x, int y) {


int code = INSIDE;

if (x < xmin)
code |= LEFT;
else if (x > xmax)
code |= RIGHT;
if (y < ymin)
code |= BOTTOM;
else if (y > ymax)
code |= TOP;

return code;
}

void cohenSutherland(int x1, int y1, int x2, int y2) {


int code1, code2, code;
int accept = 0, done = 0;

code1 = computeCode(x1, y1);


code2 = computeCode(x2, y2);

do {
if (!(code1 | code2)) {
accept = 1;
done = 1;
} else if (code1 & code2) {
done = 1;
} else {
int x, y;

code = code1 ? code1 : code2;


if (code & TOP) {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if (code & BOTTOM) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if (code & RIGHT) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else if (code & LEFT) {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}

if (code == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
} while (!done);

if (accept) {
line(x1, y1, x2, y2);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

// Set window coordinates


xmin = 100;
ymin = 100;
xmax = 300;
ymax = 200;

// Draw the window


rectangle(xmin, ymin, xmax, ymax);

// Line coordinates
int x1 = 50, y1 = 150;
int x2 = 200, y2 = 250;
// Draw the original line
line(x1, y1, x2, y2);

// Clip the line using Cohen-Sutherland algorithm


cohenSutherland(x1, y1, x2, y2);

getch();
closegraph();
return 0;
}

Output

Mid-point Sub-division Algorithm

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

void drawLine(int x1, int y1, int x2, int y2) {


line(x1, y1, x2, y2);
}

void midpointSubdivision(int x1, int y1, int x2, int y2, int depth) {
if (depth == 0) {
drawLine(x1, y1, x2, y2);
} else {
int xm = (x1 + x2) / 2;
int ym = (y1 + y2) / 2;

int xr = (x1 + xm) / 2;


int yr = (y1 + ym) / 2;

int xl = (xm + x2) / 2;


int yl = (ym + y2) / 2;

int xp = (xr + xl) / 2;


int yp = (yr + yl) / 2;

midpointSubdivision(x1, y1, xr, yr, depth - 1);


midpointSubdivision(xr, yr, xp, yp, depth - 1);
midpointSubdivision(xp, yp, xl, yl, depth - 1);
midpointSubdivision(xl, yl, x2, y2, depth - 1);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");

int x1, y1, x2, y2, depth;

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


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

printf("Enter the ending coordinates (x2 y2): ");


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

printf("Enter the depth for subdivision: ");


scanf("%d", &depth);

midpointSubdivision(x1, y1, x2, y2, depth);

getch();
closegraph();
return 0;
}

Output
Cyrus Beck

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

typedef struct {
float x, y;
} Point;

typedef struct {
float dx, dy;
} Vector;

typedef struct {
Point p1, p2;
} Line;

int computeRegionCode(Point p, int xmin, int ymin, int xmax, int ymax);
float dotProduct(Vector v1, Vector v2);
int clipTest(float p, float q, float *t1, float *t2);
void cyrusBeckClipping(Line line, int xmin, int ymin, int xmax, int ymax);

void drawLine(int x1, int y1, int x2, int y2) {


line(x1, y1, x2, y2);
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

int xmin = 100, ymin = 100, xmax = 400, ymax = 300;

rectangle(xmin, ymin, xmax, ymax); // Draw the clipping window

Line inputLine = {{50, 150}, {200, 250}};

// Draw the original line


drawLine((int)inputLine.p1.x, (int)inputLine.p1.y, (int)inputLine.p2.x,
(int)inputLine.p2.y);

// Clip the line using Cyrus-Beck algorithm


cyrusBeckClipping(inputLine, xmin, ymin, xmax, ymax);

getch();
closegraph();
return 0;
}

int computeRegionCode(Point p, int xmin, int ymin, int xmax, int ymax) {
int code = 0;

if (p.x < xmin)


code |= 1; // left
else if (p.x > xmax)
code |= 2; // right

if (p.y < ymin)


code |= 4; // bottom
else if (p.y > ymax)
code |= 8; // top

return code;
}

float dotProduct(Vector v1, Vector v2) {


return v1.dx * v2.dx + v1.dy * v2.dy;
}
int clipTest(float p, float q, float *t1, float *t2) {
float r;

if (p < 0.0) {
r = q / p;
if (r > *t2)
return 0;
if (r > *t1)
*t1 = r;
} else if (p > 0.0) {
r = q / p;
if (r < *t1)
return 0;
if (r < *t2)
*t2 = r;
} else if (q < 0.0)
return 0;

return 1;
}

void cyrusBeckClipping(Line line, int xmin, int ymin, int xmax, int ymax) {
Vector D, W;
Point P1, P2;

D.dx = line.p2.x - line.p1.x;


D.dy = line.p2.y - line.p1.y;

P1 = line.p1;
P2 = line.p2;

float t1 = 0.0, t2 = 1.0;

W.dx = -(xmax - xmin);


W.dy = 0;
if (clipTest(-W.dx, P1.x - xmin, &t1, &t2))
if (clipTest(W.dx, xmax - P1.x, &t1, &t2))
if (clipTest(-W.dy, P1.y - ymin, &t1, &t2))
if (clipTest(W.dy, ymax - P1.y, &t1, &t2)) {
if (t2 < 1.0) {
P2.x = P1.x + t2 * D.dx;
P2.y = P1.y + t2 * D.dy;
}

if (t1 > 0.0) {


P1.x += t1 * D.dx;
P1.y += t1 * D.dy;
}
// Draw the clipped line
drawLine((int)P1.x, (int)P1.y, (int)P2.x, (int)P2.y);
}
}

Output

Liang Barsky

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

typedef struct {
float x, y;
} Point;

typedef struct {
float dx, dy;
} Vector;

typedef struct {
Point p1, p2;
} Line;

int clipLineLiangBarsky(Line line, float xmin, float ymin, float xmax, float ymax, float *t1,
float *t2);
void drawLine(float x1, float y1, float x2, float y2);

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
float xmin = 100, ymin = 100, xmax = 400, ymax = 300;

rectangle(xmin, ymin, xmax, ymax); // Draw the clipping window

Line inputLine = {{50, 150}, {200, 250}};

// Draw the original line


drawLine(inputLine.p1.x, inputLine.p1.y, inputLine.p2.x, inputLine.p2.y);

float t1, t2;

// Clip the line using Liang-Barsky algorithm


if (clipLineLiangBarsky(inputLine, xmin, ymin, xmax, ymax, &t1, &t2)) {
float x1 = inputLine.p1.x + t1 * (inputLine.p2.x - inputLine.p1.x);
float y1 = inputLine.p1.y + t1 * (inputLine.p2.y - inputLine.p1.y);
float x2 = inputLine.p1.x + t2 * (inputLine.p2.x - inputLine.p1.x);
float y2 = inputLine.p1.y + t2 * (inputLine.p2.y - inputLine.p1.y);

// Draw the clipped line


drawLine(x1, y1, x2, y2);
}

getch();
closegraph();
return 0;
}

int clipLineLiangBarsky(Line line, float xmin, float ymin, float xmax, float ymax, float *t1,
float *t2) {
float dx = line.p2.x - line.p1.x;
float dy = line.p2.y - line.p1.y;

float p[4] = {-dx, dx, -dy, dy};


float q[4] = {line.p1.x - xmin, xmax - line.p1.x, line.p1.y - ymin, ymax - line.p1.y};

*t1 = 0.0;
*t2 = 1.0;

for (int i = 0; i < 4; i++) {


if (p[i] == 0) {
if (q[i] < 0)
return 0;
} else {
float r = q[i] / p[i];
if (p[i] < 0 && r > *t1)
*t1 = r;
else if (p[i] > 0 && r < *t2)
*t2 = r;
if (*t1 > *t2)
return 0;
}
}

return 1;
}

void drawLine(float x1, float y1, float x2, float y2) {


line((int)x1, (int)y1, (int)x2, (int)y2);
}

Output

You might also like