7
A
回答
5
這是一個相當陳舊的問題,但我發現它試圖解決相同問題的時候。分享我遇到的解決方案是公平的,所以下一個可憐的悶棍不必自己弄清楚。
我不知道這是否是做事情特別好的方式,我想,如果你開始使用弧形細胞會掙扎,但我的目的,效果還行。
最根本的問題是,你只有無限的邊緣一個頂點,所以你要自己計算的方向向量。使用方向垂直於由邊緣分隔的兩點之間的矢量。
#include <vector>
#include <boost/polygon/voronoi.hpp>
using boost::polygon::voronoi_builder;
using boost::polygon::voronoi_diagram;
typedef boost::polygon::point_data<int> point;
typedef voronoi_diagram<double>::cell_type cell_type;
typedef voronoi_diagram<double>::edge_type edge_type;
typedef voronoi_diagram<double>::vertex_type vertex_type;
int main(int argc, char *argv[])
{
std::vector<point> points;
// Populate with random points
for (int i = 0; i < 50; i++)
{
points.push_back(point(60 + rand() % 500, 60 + rand() % 500));
}
voronoi_diagram<double> vd;
construct_voronoi(points.begin(), points.end(), &vd);
// vd now contains the voronoi diagram. Let's visualise it
// pseudocode 'draw_line(x1, y1, x2, y2)'
for (voronoi_diagram<double>::const_cell_iterator it = vd.cells().begin();
it != vd.cells().end(); ++it)
{
const cell_type& cell = *it;
const edge_type* edge = cell.incident_edge();
do
{
if (edge->is_primary())
{
// Easy finite edge case
if (edge->is_finite())
{
// Without this check each edge would be drawn twice
// as they are really half-edges
if (edge->cell()->source_index() <
edge->twin()->cell()->source_index())
{
draw_line(edge->vertex0()->x(), edge->vertex0()->y(),
edge->vertex1()->x(), edge->vertex1()->y());
}
}
else
{
// This covers the infinite edge case in question.
const vertex_type* v0 = edge->vertex0();
// Again, only consider half the half-edges, ignore edge->vertex1()
// to avoid overdrawing the lines
if (v0)
{
// Direction of infinite edge if perpendicular to direction
// between the points owning the two half edges.
// Take the rotated right vector and multiply by a large
// enough number to reach your bounding box
point p1 = points[edge->cell()->source_index()];
point p2 = points[edge->twin()->cell()->source_index()];
int end_x = (p1.y() - p2.y()) * 640;
int end_y = (p1.x() - p2.x()) * -640;
draw_line(v0->x(), v0->y(),
end_x, end_y);
}
}
}
edge = edge->next();
} while (edge != cell.incident_edge());
}
}
2
我在這裏發現了這個代碼段:http://www.boost.org/doc/libs/1_55_0/libs/polygon/example/voronoi_visualizer.cpp
void clip_infinite_edge(
const edge_type& edge, std::vector<point_type>* clipped_edge) {
const cell_type& cell1 = *edge.cell();
const cell_type& cell2 = *edge.twin()->cell();
point_type origin, direction;
// Infinite edges could not be created by two segment sites.
if (cell1.contains_point() && cell2.contains_point()) {
point_type p1 = retrieve_point(cell1);
point_type p2 = retrieve_point(cell2);
origin.x((p1.x() + p2.x()) * 0.5);
origin.y((p1.y() + p2.y()) * 0.5);
direction.x(p1.y() - p2.y());
direction.y(p2.x() - p1.x());
} else {
origin = cell1.contains_segment() ?
retrieve_point(cell2) :
retrieve_point(cell1);
segment_type segment = cell1.contains_segment() ?
retrieve_segment(cell1) :
retrieve_segment(cell2);
coordinate_type dx = high(segment).x() - low(segment).x();
coordinate_type dy = high(segment).y() - low(segment).y();
if ((low(segment) == origin)^cell1.contains_point()) {
direction.x(dy);
direction.y(-dx);
} else {
direction.x(-dy);
direction.y(dx);
}
}
coordinate_type side = xh(brect_) - xl(brect_);
coordinate_type koef =
side/(std::max)(fabs(direction.x()), fabs(direction.y()));
if (edge.vertex0() == NULL) {
clipped_edge->push_back(point_type(
origin.x() - direction.x() * koef,
origin.y() - direction.y() * koef));
} else {
clipped_edge->push_back(
point_type(edge.vertex0()->x(), edge.vertex0()->y()));
}
if (edge.vertex1() == NULL) {
clipped_edge->push_back(point_type(
origin.x() + direction.x() * koef,
origin.y() + direction.y() * koef));
} else {
clipped_edge->push_back(
point_type(edge.vertex1()->x(), edge.vertex1()->y()));
}
}
也可能會丟失一些類變量或方法,但邏輯是這裏重要的。
相關問題
- 1. 如何讓單邊盒子陰影從邊緣到邊緣出現?
- 2. 邊緣到邊緣的HTML5視頻
- 3. 側邊欄與純邊緣的邊緣?
- 4. 使用邊緣容器提升astar_search
- 5. 帶索引的提升圖邊緣
- 6. 無邊界窗體邊緣
- 7. 邊緣擴展chrome.storage.local 1MB限制
- 8. 邊緣限制包含在一頁內?
- 9. 如何在幀邊緣接觸另一個邊緣時獲取該邊緣?
- 10. 如何從邊緣
- 11. Google Sketchup中的明顯邊緣邊緣
- 12. 帶有無限邊緣座標的重疊框
- 13. 限制UITableView到其父視圖的邊緣,無故事板
- 14. 如何限制平移到畫布邊緣?
- 15. 邊緣提取Suggections OpenCV的
- 16. UIBezierPath提供尖銳邊緣
- 17. 讓文字傳播到div的邊緣
- 18. networkx邊緣到節點節點到邊緣的表示
- 19. 如何測量邊緣檢測圖像邊緣的長度?
- 20. 如何將邊緣標籤放置在graphviz的邊緣
- 21. 如何設置邊緣長度與邊緣值成比例
- 22. 如何更改JUNG邊緣的邊緣標籤?
- 23. AForge.net邊緣檢測 - 如何獲得邊緣點?
- 24. SVG:邊緣到邊緣的黑色形狀之間的無意的白色邊緣
- 25. 邊緣疊加
- 26. 邊緣 - Cocos2d/Box2D
- 27. 邊緣塌陷
- 28. 銳化邊緣
- 29. 邊緣Cytoscape中
- 30. 由邊緣環
耶穌克里斯,爲什麼我們沒有這個落實到庫本身? o.O – PiotrK