Last active
March 5, 2017 02:58
-
-
Save thai-ng/7b1840a92c82fd9eadf512fad1e5ba04 to your computer and use it in GitHub Desktop.
count number of viewable points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <cmath> | |
#include <algorithm> | |
struct Point | |
{ | |
int x; | |
int y; | |
}; | |
auto getAngle(const Point& point) | |
{ | |
static constexpr double PI = 3.14159265; | |
auto angle = std::atan2(point.y, point.x) * (180.0 / PI); | |
if (angle < 0) | |
{ | |
angle += 360; | |
} | |
return angle; | |
} | |
int main() | |
{ | |
std::vector<Point> points{ { 1, 1 },{ 3,1 },{ 3,2 },{ 3, 3 },{ 1, 3 },{ 2, 5 },{ 1, 5 },{ -1, -1 },{ -1, -2 },{ -2, -3 },{ -4, -4 } }; | |
std::vector<int> angles; | |
angles.resize(points.size()); | |
std::transform(points.begin(), points.end(), angles.begin(), [](auto point) {return getAngle(point); }); | |
std::sort(angles.begin(), angles.end()); | |
std::vector<int> count; | |
count.reserve(angles.size()); | |
for (auto startAngleIt = angles.begin(); startAngleIt != angles.end(); ++startAngleIt) | |
{ | |
auto endAngle = std::round(*startAngleIt + 45.0); | |
auto endAngleIt = std::upper_bound(angles.begin(), angles.end(), endAngle); | |
count.push_back(std::distance(startAngleIt, endAngleIt)); | |
} | |
auto result = *std::max_element(count.begin(), count.end()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment