简体   繁体   English

增强几何:多边形定义

[英]Boost-geometry : Polygon definition

Could you please tell me why this polygon definition don't work? 您能否告诉我为什么这个多边形定义不起作用?

namespace bg = boost::geometry;

int main()
{
typedef bg::model::point<double, 2, bg::cs::cartesian> point_type;
typedef bg::model::polygon<point_type> polygon_type;

polygon_type P;

int xi[] = {0,100,100,0,0};
int yi[] = {0,0,100,100,0};

bg::append(P, bg::make<point_type>(*xi, *yi));

double area = bg::area(P);
std::cout << "Area:" << area << std::endl;

Return 0; }

This shows Area : 0 这显示面积:0

Thank you 谢谢

Learn to use the library to diagnose your issues: 学习使用库来诊断您的问题:

std::string reason;
bool ok = bg::is_valid(P, reason);
std::cout << "Polygon " << (ok?"valid":"invalid") << " (" << reason << ")\n";

Prints: Live On Coliru 版画: Coliru直播

Polygon invalid (Geometry has too few points)

You can also print the geometry in WKT/DSV (or even SVG): Live On Coliru 您也可以在WKT / DSV(甚至SVG)中打印几何图形: Live On Coliru

POLYGON((0 0)) invalid (Geometry has too few points)
Area:0

This makes the problem even more evident. 这使问题更加明显。


You don't want to add a single point, but a ring. 您不想添加单个点,而是添加一个环。 The simplest way to achieve this is to explicitly make it a ring: 实现此目的的最简单方法是显式使其成为环:

ring_type points {
    {   0,   0 },
    { 100,   0 },
    { 100, 100 },
    {   0, 100 },
    {   0,   0 },
};

bg::append(P, points);

Still not ok, but better: Live On Coliru 仍然不行,但更好: Live on Coliru

POLYGON((0 0,100 0,100 100,0 100,0 0)) invalid (Geometry has wrong orientation)
Area:-10000

As you can see we got the orientation wrong (should be clockwise): 如您所见,我们弄错了方向(应该是顺时针方向):

ring_type points {
    {   0,   0 },
    {   0, 100 },
    { 100, 100 },
    { 100,   0 },
    {   0,   0 },
};

Prints: Live On Coliru 版画: Coliru直播

POLYGON((0 0,0 100,100 100,100 0,0 0)) valid (Geometry is valid)
Area:10000

Note You can use bg::correct to correct validity issues in straightforward cases, like this. 注意您可以使用bg::correct来纠正在简单情况下的有效性问题,例如这样。

Style And Types 样式和类型

In fact the ring-type is the outer ring type of the polygon type: 实际上,环形是多边形类型的外环类型:

static_assert(std::is_same<ring_type, polygon_type::ring_type>{}, "synonyms");
static_assert(bg::point_order<ring_type>::value == bg::order_selector::clockwise, "orientation");

Therefore, you can construct directly from the outer ring initializer: 因此,您可以直接从外环初始化程序构造:

polygon_type P { {
    {   0,   0 },
    {   0, 100 },
    { 100, 100 },
    { 100,   0 },
    {   0,   0 },
} };

Condensing the whole program to a single line: Live On Coliru 将整个程序压缩为一行: Live On Coliru

int main() {
    typedef bgm::polygon<bgm::d2::point_xy<double> > polygon_type;
    std::cout << "Area:" << bg::area(polygon_type { { {0, 0}, {0, 100}, {100, 100}, {100, 0}, {0, 0} } }) << std::endl;
}

Or you can, indeed read it from WKT/DSV: Live On Coliru 或者,您也可以从WKT / DSV上阅读它: Live On Coliru

int main() {
    bgm::polygon<bgm::d2::point_xy<double> > P;
    bg::read_wkt("POLYGON((0 0,0 100,100 100,100 0,0 0))", P);
    std::cout << "Area:" << bg::area(P) << std::endl;
}

Still printing 仍在打印

Area:10000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM