0% found this document useful (0 votes)
13 views

HUT.CPP

The document contains a C program that uses graphics to draw shapes including lines and circles using the DDA algorithm and a circle drawing algorithm. It initializes a graphics window, draws various shapes, fills them with colors, and displays them on the screen. The program also includes functions to draw lines and circles, as well as to plot points of the circle in all octants.

Uploaded by

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

HUT.CPP

The document contains a C program that uses graphics to draw shapes including lines and circles using the DDA algorithm and a circle drawing algorithm. It initializes a graphics window, draws various shapes, fills them with colors, and displays them on the screen. The program also includes functions to draw lines and circles, as well as to plot points of the circle in all octants.

Uploaded by

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

#include<stdio.

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

// Function to draw a line using DDA algorithm


void drawline(int x1, int y1, int x2, int y2);
void plotCirclePoints(int xc, int yc, int x, int y);

void drawCircle(int xc, int yc, int r);

void main() {
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

drawline(120, 70, 530, 70);


drawline(530, 70, 530, 400);
drawline(530, 400, 70, 400);
drawline(70, 400, 70, 180);
drawline(70, 180, 530, 180);
drawline(70, 180, 120, 70);
drawline(120, 70, 180, 180);
drawline(180, 180, 180, 400);
drawline(350, 220, 350, 290);
drawline(270, 255, 430, 255);

drawCircle(123, 145, 25);

drawline(270, 220, 430, 220);


drawline(430, 220, 430, 290);
drawline(430, 290, 270, 290);
drawline(270, 290, 270, 220);

drawline(90, 270, 160, 270);


drawline(160, 270, 160, 400);
drawline(160, 400, 90, 400);
drawline(90, 400, 90, 270);

drawline(350, 220, 350, 290);


drawline(270, 255, 430, 255);

// Fill colors into shapes


setfillstyle(SOLID_FILL, BROWN);
floodfill(123, 145, WHITE); // Circle

setfillstyle(SOLID_FILL, YELLOW);
floodfill(91, 271, WHITE); // Rectangle 1

setfillstyle(SOLID_FILL, LIGHTGREEN);
floodfill(271, 221, WHITE); // Rectangle 2
floodfill(429, 289, WHITE);
setfillstyle(SOLID_FILL, CYAN);
floodfill(271, 289, WHITE); // Rectangle 2 inner
floodfill(429, 221, WHITE);

setfillstyle(SOLID_FILL,7);
floodfill(431,221,WHITE);
setfillstyle(SOLID_FILL,13);
floodfill(71,181,WHITE);
setfillstyle(SOLID_FILL, LIGHTRED);
floodfill(71, 179, WHITE); // Triangle connection

setfillstyle(SOLID_FILL, GREEN);
floodfill(529, 71, WHITE); // Side boundary

getch();
closegraph();
}

// Function to draw a line using DDA algorithm


void drawline(int x1, int y1, int x2, int y2) {
float x, y, xinc, yinc, dx, dy;
int steps, i;

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

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


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

xinc = dx / steps;
yinc = dy / steps;

x = x1;
y = y1;

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


putpixel(x, y, WHITE); // Use WHITE for boundaries
x += xinc;
y += yinc;
}
}
void drawCircle(int xc, int yc, int r) {
int x = 0;
int y = r;
int p = 1 - r; // Initial decision parameter

// Plot the initial points


plotCirclePoints(xc, yc, x, y);

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

// Plot the points for the current x and y


plotCirclePoints(xc, yc, x, y);
}
}

// Function to plot points of the circle in all octants


void plotCirclePoints(int xc, int yc, int x, int y) {
putpixel(xc + x, yc + y, WHITE); // Octant 1
putpixel(xc - x, yc + y, WHITE); // Octant 2
putpixel(xc + x, yc - y, WHITE); // Octant 3
putpixel(xc - x, yc - y, WHITE); // Octant 4
putpixel(xc + y, yc + x, WHITE); // Octant 5
putpixel(xc - y, yc + x, WHITE); // Octant 6
putpixel(xc + y, yc - x, WHITE); // Octant 7
putpixel(xc - y, yc - x, WHITE); // Octant 8
}

You might also like