Loading...
Searching...
No Matches
Get centre and radius of the smallest circle that circumscribes given set of points. More...
#include <cmath>
#include <iostream>
#include <vector>
Go to the source code of this file.
Get centre and radius of the smallest circle that circumscribes given set of points.
Definition in file smallest_circle.cpp.
◆ circle() double circle ( const std::vector< Point > & P )Find the centre and radius of a circle enclosing a set of points.
The function returns the radius of the circle and prints the coordinated of the centre of the circle.
Definition at line 87 of file smallest_circle.cpp.
87 {
88 double minR = INFINITY;
89 double R;
92
93
94
95 for (size_t i = 0; i < P.size() - 2; i++)
96
97 for (size_t j = i + 1; j < P.size(); j++)
98
99 for(
size_tk = j + 1;
k< P.size();
k++) {
100
101
102
103 C.x = -0.5 * ((P[i].y * (P[j].x * P[j].x + P[j].y * P[j].y -
104P[
k].x * P[
k].x - P[
k].y * P[
k].y) +
105P[j].y * (P[
k].x * P[
k].x + P[
k].y * P[
k].y -
106 P[i].x * P[i].x - P[i].y * P[i].y) +
107P[
k].y * (P[i].x * P[i].x + P[i].y * P[i].y -
108 P[j].x * P[j].x - P[j].y * P[j].y)) /
109(P[i].x * (P[j].y - P[
k].y) +
110P[j].x * (P[
k].y - P[i].y) +
111P[
k].x * (P[i].y - P[j].y)));
112C.
y= 0.5 * ((P[i].x * (P[j].x * P[j].x + P[j].y * P[j].y -
113P[
k].x * P[
k].x - P[
k].y * P[
k].y) +
114P[j].x * (P[
k].x * P[
k].x + P[
k].y * P[
k].y -
115 P[i].x * P[i].x - P[i].y * P[i].y) +
116P[
k].x * (P[i].x * P[i].x + P[i].y * P[i].y -
117 P[j].x * P[j].x - P[j].y * P[j].y)) /
118(P[i].x * (P[j].y - P[
k].y) +
119P[j].x * (P[
k].y - P[i].y) +
120P[
k].x * (P[i].y - P[j].y)));
125 continue;
126 }
127 if (R <= minR) {
128 minR = R;
129 minC = C;
130 }
131 }
132
133
134 for (size_t i = 0; i < P.size() - 1; i++)
135
136 for (size_t j = i + 1; j < P.size(); j++) {
137
138 C.x = (P[i].x + P[j].x) / 2;
139C.
y= (P[i].y + P[j].y) / 2;
142 continue;
143 }
144 if (R <= minR) {
145 minR = R;
146 minC = C;
147 }
148 }
149std::cout << minC.x <<
" "<< minC.
y<< std::endl;
150 return minR;
151}
double k(double x)
Another test function.
double LenghtLine(const Point &A, const Point &B)
double TriangleArea(const Point &A, const Point &B, const Point &C)
bool PointInCircle(const std::vector< Point > &P, const Point &Center, double R)
int y
Point respect to x coordinate.
◆ LenghtLine() double LenghtLine ( const Point & A, const Point & B )Compute the Euclidian distance between two points \(A\equiv(x_1,y_1)\) and \(B\equiv(x_2,y_2)\) using the formula:
\[d=\sqrt{\left(x_1-x_2\right)^2+\left(y_1-y_2\right)^2}\]
Definition at line 37 of file smallest_circle.cpp.
37 {
38 double dx = B.x - A.x;
39 doubledy = B.
y- A.
y;
40 return std::sqrt((dx * dx) + (dy * dy));
41}
◆ main()Main program
Definition at line 198 of file smallest_circle.cpp.
198 {
200 std::cout << std::endl;
202 std::cout << std::endl;
204 return 0;
205}
◆ PointInCircle() bool PointInCircle ( const std::vector< Point > & P, const Point & Center, double R )Check if a set of points lie within given circle. This is true if the distance of all the points from the centre of the circle is less than the radius of the circle
Definition at line 72 of file smallest_circle.cpp.
72 {
73 for (size_t i = 0; i < P.size(); i++) {
75 return false;
76 }
77 return true;
78}
◆ test()Test case: result should be:
Circle with
radius 3.318493136080724
centre at (3.0454545454545454, 1.3181818181818181)
Definition at line 158 of file smallest_circle.cpp.
158 {
159 std::vector<Point> Pv;
160Pv.push_back(
Point(0, 0));
161Pv.push_back(
Point(5, 4));
162Pv.push_back(
Point(1, 3));
163Pv.push_back(
Point(4, 1));
164Pv.push_back(
Point(3, -2));
165std::cout <<
circle(Pv) << std::endl;
166}
double circle(const std::vector< Point > &P)
◆ test2()Test case: result should be:
Circle with
radius 1.4142135623730951
centre at (1.0, 1.0)
Definition at line 173 of file smallest_circle.cpp.
173 {
174 std::vector<Point> Pv;
175Pv.push_back(
Point(0, 0));
176Pv.push_back(
Point(0, 2));
177Pv.push_back(
Point(2, 2));
178Pv.push_back(
Point(2, 0));
179std::cout <<
circle(Pv) << std::endl;
180}
◆ test3()Test case: result should be:
Circle with
radius 1.821078397711709
centre at (2.142857142857143, 1.7857142857142856)
Definition at line 188 of file smallest_circle.cpp.
188 {
189 std::vector<Point> Pv;
190Pv.push_back(
Point(0.5, 1));
191Pv.push_back(
Point(3.5, 3));
192Pv.push_back(
Point(2.5, 0));
193Pv.push_back(
Point(2, 1.5));
194std::cout <<
circle(Pv) << std::endl;
195}
◆ TriangleArea()Compute the area of triangle formed by three points using Heron's formula. If the lengths of the sides of the triangle are \(a,\,b,\,c\) and \(s=\displaystyle\frac{a+b+c}{2}\) is the semi-perimeter then the area is given by
\[A=\sqrt{s(s-a)(s-b)(s-c)}\]
Definition at line 54 of file smallest_circle.cpp.
54 {
58 double p = (a + b + c) / 2;
59 return std::sqrt(p * (p - a) * (p - b) * (p - c));
60}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4