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

01. Write C++ program to draw the following pattern. Use DDA line and Bresenham‘s circle drawing algorithm. Apply the concept of encapsulation

The document contains C++ code implementing Bresenham's Circle Drawing Algorithm and DDA Line Drawing Algorithm. It defines two classes, Circle and Line, to draw circles and lines on a graphics window. The main function initializes the graphics, draws two concentric circles, and a triangle using the defined algorithms.

Uploaded by

divyaghadge5
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)
68 views

01. Write C++ program to draw the following pattern. Use DDA line and Bresenham‘s circle drawing algorithm. Apply the concept of encapsulation

The document contains C++ code implementing Bresenham's Circle Drawing Algorithm and DDA Line Drawing Algorithm. It defines two classes, Circle and Line, to draw circles and lines on a graphics window. The main function initializes the graphics, draws two concentric circles, and a triangle using the defined algorithms.

Uploaded by

divyaghadge5
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/ 2

#include <graphics.

h>
#include <iostream>
#include <cmath>
using namespace std;

// Class for Bresenham's Circle Drawing Algorithm


class Circle {
public:
void drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;

// Draw 8 symmetric points


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);

x++;
if (d < 0) {
d += 4 * x + 6;
} else {
y--;
d += 4 * (x - y) + 10;
}
}
}
};

// Class for DDA Line Drawing Algorithm


class Line {
public:
void drawLine(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 Xinc = dx / (float)steps;
float Yinc = dy / (float)steps;

float x = x1, y = y1;


for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE);
x += Xinc;
y += Yinc;
}
}
};

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

Circle c;
Line l;
// Outer circle
int centerX = 320, centerY = 240, outerRadius = 150, innerRadius = 50;
c.drawCircle(centerX, centerY, outerRadius);

// Inner circle
c.drawCircle(centerX, centerY, innerRadius);

// Triangle vertices
int x1 = centerX, y1 = centerY - outerRadius; // Top vertex
int x2 = centerX - (outerRadius * sin(3.14159 / 3)), y2 = centerY +
(outerRadius / 2); // Bottom left
int x3 = centerX + (outerRadius * sin(3.14159 / 3)), y3 = centerY +
(outerRadius / 2); // Bottom right

// Draw triangle using DDA


l.drawLine(x1, y1, x2, y2); // Top to bottom left
l.drawLine(x2, y2, x3, y3); // Bottom left to bottom right
l.drawLine(x3, y3, x1, y1); // Bottom right to top

// Hold the screen


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

You might also like