本文介绍了如何在GEOS中制作矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用GEOS库的C ++ API制作矩形?

How do you make a rectangle using the GEOS library's C++ API?

推荐答案

以下实现在中完成工作 GEOS .

The following implementation gets the job done in GEOS.

//Compile with: g++ code.cpp -lgeos
//Updated: 2019-03-31
#include <geos/geom/PrecisionModel.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/LinearRing.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryFactory.h>
#include <iostream>
#include <memory>

geos::geom::Polygon* MakeBox(double xmin, double ymin, double xmax, double ymax){
  std::unique_ptr<geos::geom::PrecisionModel> pm(new geos::geom::PrecisionModel());

  geos::geom::GeometryFactory::unique_ptr factory = geos::geom::GeometryFactory::create(pm.get(), -1);
  geos::geom::CoordinateSequence *temp = factory->getCoordinateSequenceFactory()->create((std::size_t) 0, 0);

  temp->add(geos::geom::Coordinate(xmin, ymin));
  temp->add(geos::geom::Coordinate(xmin, ymax));
  temp->add(geos::geom::Coordinate(xmax, ymax));
  temp->add(geos::geom::Coordinate(xmax, ymin));
  //Must close the linear ring or we will get an error:
  //"Points of LinearRing do not form a closed linestring"
  temp->add(geos::geom::Coordinate(xmin, ymin));

  geos::geom::LinearRing *shell=factory->createLinearRing(temp);

  //NULL in this case could instead be a collection of one or more holes
  //in the interior of the polygon
  return factory->createPolygon(shell,NULL);
}

int main(){
  geos::geom::Polygon* box = MakeBox(0,0,10,10);
  std::cout<<box->getArea()<<std::endl;
  delete box; //Important to avoid memory leaks
}


作为参考,您也可以使用 boost :: polygon 库,如下所示.


For reference, you can also accomplish this using the boost::polygon library, as follows.

//Compile with: g++ code.cpp
#include <boost/polygon/polygon.hpp>
#include <iostream>
namespace gtl = boost::polygon;

typedef gtl::polygon_data<float> Polygon;

Polygon MakeBox(float xmin, float ymin, float xmax, float ymax){
  typedef gtl::polygon_traits<Polygon>::point_type Point;
  Point pts[] = {
    gtl::construct<Point>(xmin, ymin),
    gtl::construct<Point>(xmin, ymax),
    gtl::construct<Point>(xmax, ymax),
    gtl::construct<Point>(xmax, ymin)
  };
  Polygon poly;
  gtl::set_points(poly, pts, pts+4);

  return poly;
}

int main(){
  Polygon box = MakeBox(0,0,10,10);
  std::cout<<gtl::area(box)<<std::endl;
}


以及 Clipper .请注意,Clipper不能使用浮点坐标.


And also with Clipper. Note that Clipper cannot use floating-point coordinates.

#include "clipper_cpp/clipper.hpp"
#include <iostream>

ClipperLib::Paths MakeBox(int xmin, int ymin, int xmax, int ymax){
  ClipperLib::Paths box(1);
  //Note that we run the path in the reverse direction to previous examples in
  //order to maintain positive area
  box[0] << ClipperLib::IntPoint(xmin,ymin) << ClipperLib::IntPoint(xmax,ymin)
         << ClipperLib::IntPoint(xmax,ymax) << ClipperLib::IntPoint(xmin,ymax);
  return box;
}

int main(){
  ClipperLib::Paths box = MakeBox(0,0,10,10);
  std::cout<<ClipperLib::Area(box[0])<<std::endl;
}

这篇关于如何在GEOS中制作矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 21:54