0% found this document useful (0 votes)
11 views2 pages

CG Lab1

The document contains a C++ program that utilizes the graphics library to draw shapes. It defines a class 'ShapeDrawer' with methods to draw lines, circles, and a specific pattern combining these shapes. The main function initializes the graphics, prompts the user for circle parameters, and calls the drawing methods to create the visual output.

Uploaded by

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

CG Lab1

The document contains a C++ program that utilizes the graphics library to draw shapes. It defines a class 'ShapeDrawer' with methods to draw lines, circles, and a specific pattern combining these shapes. The main function initializes the graphics, prompts the user for circle parameters, and calls the drawing methods to create the visual output.

Uploaded by

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

#include <graphics.

h>
#include <iostream>
#include <math.h>
using namespace std;

class ShapeDrawer {
public:
void drawLine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int steps = std::max(abs(dx), abs(dy));

float xInc = dx / (float)steps;


float yInc = dy / (float)steps;

float x = x1;
float y = y1;

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


putpixel(round(x), round(y), WHITE);
x += xInc;
y += yInc;
}
}

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


int x = 0, y = r;
int d = 3 - 2 * r;

while (y >= x) {
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) {
y--;
d = d + 4 * (x - y) + 10;
} else {
d = d + 4 * x + 6;
}
}
}
void drawPattern(int xc, int yc, int r) {
drawCircle(xc, yc, r);

int x1 = xc, y1 = yc - r;
int x2 = xc - r * sqrt(3) / 2, y2 = yc + r / 2;
int x3 = xc + r * sqrt(3) / 2, y3 = yc + r / 2;

drawLine(x1, y1, x2, y2);


drawLine(x2, y2, x3, y3);
drawLine(x3, y3, x1, y1);

drawCircle(xc, yc, r / 2);


}
};

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
ShapeDrawer drawer;
int xc,yc,r;
cout<<"Name: Binay Tilokchandani";
cout<<"\nenter x coordinate of center: ";
cin>>xc;
cout<<"enter y coordinate of center: ";
cin>>yc;
cout<<"enter radius: ";
cin>>r;
drawer.drawPattern(xc, yc, r);
getch();
closegraph();
return 0;
}

You might also like