﻿ c – 找出一个点是否在voronoi单元格内 - 代码日志

#### c – 找出一个点是否在voronoi单元格内

``````using namespace boost::polygon;

point_data<int> p1(0, 0);
point_data<int> p2(-10, 10);
point_data<int> p3(-10, -10);
point_data<int> p4(10, -10);
point_data<int> p5(10, 10);

std::vector<point_data<int>> pts = { p1, p2, p3, p4, p5 };
construct_voronoi(pts.begin(), pts.end(), vd);
``````

``````//using namespace boost::polygon;
using namespace std;
#include <iostream>
#include <vector>
#include <limits>

template <typename T>
using point_data = std::pair<T,T>;
point_data<int> p1(0, 0);
point_data<int> p2(-10, 10);
point_data<int> p3(-10, -10);
point_data<int> p4(10, -10);
point_data<int> p5(10, 10);

std::vector<point_data<int>> pts = { p1, p2, p3, p4, p5 };
//construct_voronoi(pts.begin(), pts.end(), vd);

double dist2(point_data<int> pt1,point_data<int> pt2) {
return (pt1.first-pt2.first)*(pt1.first-pt2.first) + (pt1.first-pt2.second)* (pt1.first-pt2.second);
}

bool isInCell(point_data<int> point) {
double d = numeric_limits<double>::max();

point_data<int> ptClose;
for (auto& pt:pts) {
if (dist2(pt,point) < d)
ptClose = pt;
}
return ptClose == point;
}

int main() {
cout << isInCell(make_pair(5,5)) << endl;
}
``````