Go to the documentation of this file.00001
00038 #include <bwi_mapper/circle_provider.h>
00039
00040 #include <iostream>
00041
00042 namespace bwi_mapper {
00043
00047 CircleProvider& CircleProvider::getInstance() {
00048 static CircleProvider instance;
00049 return instance;
00050 }
00051
00056 void CircleProvider::getCircle(int radius, std::vector<bool>& points,
00057 bool connect4) {
00058
00059 points.resize((2*radius + 1)*(2*radius + 1));
00060 int error = -radius;
00061 int x = radius;
00062 int y =0;
00063 while (x >= y) {
00064 plot8Points(x, y, points, radius);
00065 error += y;
00066 ++y;
00067 error += y;
00068 if (error >= 0) {
00069 error -= x;
00070 if (connect4) {
00071 plot8Points(x, y, points, radius);
00072 }
00073 --x;
00074 error -= x;
00075 }
00076 }
00077 }
00078
00082 void CircleProvider::printCircle(const std::vector<bool>& points,
00083 int radius) {
00084
00085 for (size_t i = 0; i < points.size(); i++) {
00086 if (i % (2*radius + 1) == 0 && i != 0) {
00087 std::cout << std::endl;
00088 }
00089 if (points[i]) {
00090 std::cout << "%";
00091 } else {
00092 std::cout << " ";
00093 }
00094 }
00095 std::cout << std::endl;
00096 }
00097
00103 void CircleProvider::plot8Points(int x, int y, std::vector<bool>& points,
00104 int radius) {
00105 plot4Points(x, y, points, radius);
00106 if (x != y) {
00107 plot4Points(y, x, points, radius);
00108 }
00109 }
00110
00115 void CircleProvider::plot4Points(int x, int y, std::vector<bool>& points,
00116 int radius) {
00117 CIRCLE_REF(points, radius, x, y) = true;
00118 CIRCLE_REF(points, radius, x, -y) = true;
00119 CIRCLE_REF(points, radius, -x, -y) = true;
00120 CIRCLE_REF(points, radius, -x, y) = true;
00121 }
00122
00123 }