-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circle.cpp
108 lines (97 loc) · 2.35 KB
/
Circle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "Circle.h"
Circle::Circle(double centerX, double centerY, double radius, const char* color, unsigned int ID) :Shapes(centerX, centerY, color, ID)
{
if (radius < 0)
{
radius = abs(radius);
}
this->radius = radius;
}
double Circle::GetRadius()
{
return radius;
}
void Circle::SetRadius(double radius)
{
this->radius = radius;
}
void Circle::Print(ostream& strm)
{
strm << " <circle ";
start.PrintCirc(strm);
strm << "r=" << '"' << radius << '"' << " ";
strm << "fill=" << '"' << color << '"' << " " << "/>";
strm << endl;
}
void Circle::Print()
{
cout << ID << ". circle, ";
start.Print();
cout << ", " << radius << ", ";
cout << color;
cout << endl;
}
void Circle::Translate(double horizontal, double vertical)
{
start.SetX(start.GetX() + horizontal);
start.SetY(start.GetY() + horizontal);
}
bool Circle::IsWithinRectangle(double startX, double startY, double width, double height)
{
Point rectangleCenter = Point((startX + width / 2), (startY + height / 2));
double w = width / 2;
double h = height / 2;
double dx = abs(start.GetX() - rectangleCenter.GetX());
double dy = abs(start.GetY() - rectangleCenter.GetY());
if (dx > (radius + w) || dy > (radius + h)) return false;
Point circleDistance = Point((abs(start.GetX() - startX - w)), (abs(start.GetY() - startY - h)));
if (circleDistance.GetX() <= (w))
{
return true;
}
if (circleDistance.GetY() <= (h))
{
return true;
}
double cornerDistanceSq = pow(circleDistance.GetX() - w, 2) + pow(circleDistance.GetY() - h, 2);
return (cornerDistanceSq <= (pow(radius, 2)));
}
bool Circle::WithinRectangle(double startX, double startY, double width, double height)
{
if (this->IsWithinRectangle(startX, startY, width, height))
{
this->Print(cout);
return true;
}
return false;
}
bool Circle::WithinCircle(double startX, double startY, double radius)
{
Point point(startX, startY);
double dist = start.Dist(point);
if (this->GetRadius() < radius)
{
if (dist + radius < this->GetRadius())
{
this->Print(cout);
return true;
}
}
else if (dist + this->GetRadius() < radius)
{
this->Print(cout);
return true;
}
else
{
return false;
}
}
double Area(double width, double height=0)
{
return 3.14*width*width;
}
Shapes* Circle::clone()
{
return new Circle(*this);
}