Skip to content

Instantly share code, notes, and snippets.

@thai-ng
Last active March 5, 2017 02:58
Show Gist options
  • Save thai-ng/7b1840a92c82fd9eadf512fad1e5ba04 to your computer and use it in GitHub Desktop.
Save thai-ng/7b1840a92c82fd9eadf512fad1e5ba04 to your computer and use it in GitHub Desktop.
count number of viewable points
#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