2014-12-25 9 views
1

GEOS 라이브러리의 C++ API를 사용하여 사각형을 만드는 방법은 무엇입니까?GEOS에서 사각형을 만드는 방법은 무엇입니까?

+0

_ "StackOverflow, 도와 주실 수 있나요?"_ 아니요! 그리고 실제 담당자와 함께 잘 알아야합니다. 스택 오버플로에 대해 책, 도구, 소프트웨어 라이브러리, 자습서 또는 기타 오프 사이트 리소스를 추천하거나 찾도록 요청하는 질문은 논쟁의 여지가있는 대답을 유치하는 경향이 있습니다. 스팸. 대신, 문제를 설명하고 지금까지 해결 된 문제를 설명하십시오.'_ –

+0

저는 [tag : geos] 라이브러리에 대한 전문 지식이 없습니다. 하지만 2 분의 연구를 통해 [geos :: geom :: Polygon'] (http://geos.osgeo.org/doxygen/classgeos_1_1geom_1_1Polygon.html)으로 안내 할 수 있습니다. 좋은 출발점처럼 보입니다. (연구의 결여 또한 SO에 대한 결론적 인 이유이다). 이 문제와 관련하여 몇 가지 특별한 문제가 있으면 질문을 자세히 작성하십시오. –

+1

도와 주셔서 감사합니다. @ πάντα ῥεῖ. 나는 당신이 수행 한 검색이 충분했는지에 대한 질문을하지 않았을 것이고, 적절한 연구라고 느낀 후에야 게시를 할 것입니다. 내가 수선 한 실제 답변은 아마도 우리 중 어느 누구도 예상했던 것, 특히 널리 사용되는 라이브러리 인 것보다 복잡하게 뒤얽힌 것입니다. – Richard

답변

4

다음 구현은 GEOS에서 작업을 완료합니다. 다음과 같이

//Compile with: g++ code.cpp -lgeos 
#include <geos/geom/Polygon.h> 
#include <geos/geom/LinearRing.h> 
#include <geos/geom/CoordinateSequenceFactory.h> 
#include <geos/geom/GeometryFactory.h> 
#include <iostream> 

geos::geom::Polygon* MakeBox(double xmin, double ymin, double xmax, double ymax){ 
    geos::geom::GeometryFactory factory; 
    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; 
} 
참고로

, 당신은 또한 boost::polygon 라이브러리를 사용하여이 작업을 수행 할 수 있습니다.
//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는 부동 소수점 좌표를 사용할 수 없습니다.

#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; 
}