1

I am trying to split polygons into bunch of rectangles. For that I am using boost polygon library. When i try to take input from user (in vector for e.g. here) the program gives unintended result but same values when hard-coded (commented in the code) gives right result.

#include <iostream>
#include <vector>
#include <boost/polygon/polygon.hpp>

namespace gtl = boost::polygon;
using namespace boost::polygon::operators;

int main() {

    typedef gtl::polygon_90_with_holes_data<int> Polygon;
    typedef gtl::polygon_traits<Polygon>::point_type Point;
    Point pts[7];
    std::vector <double> vec  {0.0,0.0, 0.0, 235.0, 170.0, 235.0, 170.0, 305.0, 310.0, 305.0, 310.0, 0.0, 0.0, 0.0};
  
    for (int i  = 0 ; i< 7 ; i++) {
        pts[i] = gtl::construct<Point>(vec[i],vec[i+1]);
    }

    /*
    pts[0] = gtl::construct<Point>(0.0 , 0.0) ;
    pts[1] = gtl::construct<Point>(0.0 , 235.0) ;
    pts[2] = gtl::construct<Point>(170.0 , 235.0) ;
    pts[3] = gtl::construct<Point>(170.0 , 305.0) ;
    pts[4] = gtl::construct<Point>(310.0 , 305.0) ;
    pts[5] = gtl::construct<Point>(310.0 , 0.0) ;
    pts[6] = gtl::construct<Point>(0.0 , 0.0) ;
    */

    Polygon poly;
    gtl::set_points(poly, pts, pts+7);

    std::vector< gtl::rectangle_data<int> > rects;
    get_rectangles(rects, poly);

    std::cout << rects.size() << " rectangle: \n";

    for(std::vector<gtl::rectangle_data<int> >::iterator it = rects.begin(); it !=
        rects.end(); ++it) {
            // Print out the corner coordinates
            std::cout << "x1: "<< gtl::xl(*it) << ", x2: " << gtl::xh(*it)
            << ", y1: "<< gtl::yl(*it) << ", y2: " << gtl::yh(*it) << std::endl;
    }
    return 0;
}

output:

with vector as input

0 rectangle:

with hardcode value

2 rectangle: 
1: 0, x2: 170, y1: 0, y2: 235
x1: 170, x2: 310, y1: 0, y2: 305
sehe
  • 374,641
  • 47
  • 450
  • 633
kil47
  • 53
  • 1
  • 9
  • 1
    voting to close as typo. In the loop `for (int i = 0 ; i< 7 ; i++) {` you increment `i` by 1 only hence `vec[i]` is the same as `vec[i+1]` in the previous iteration – 463035818_is_not_an_ai Mar 02 '22 at 08:25
  • Ah.. yes ! Thanks for the scrutiny. it should be `pts[i] = gtl::construct(vec[i*2],vec[i*2+1])` – kil47 Mar 02 '22 at 08:37

1 Answers1

0

I'd suggest avoiding problems and making the code much safer:

Live On Coliru

#include <boost/polygon/polygon.hpp>
#include <iostream>
#include <vector>

namespace gtl = boost::polygon;
//using namespace boost::polygon::operators;

int main() {
    using Polygon = gtl::polygon_90_with_holes_data<int>;
    using Point   = gtl::polygon_traits<Polygon>::point_type;

    std::vector const vec{0,   0,   0,   235, 170, 235, 170,
                          305, 310, 305, 310, 0,   0,   0};
    assert(vec.size() % 2 == 0);

    std::vector<Point> pts;
    for (size_t i = 0; i + 1 < vec.size(); i += 2) {
        pts.emplace_back(vec[i], vec[i + 1]);
    }

    Polygon poly;
    gtl::set_points(poly, pts.begin(), pts.end());

    std::vector<gtl::rectangle_data<int>> rects;
    get_rectangles(rects, poly);

    std::cout << rects.size() << " rectangle: \n";

    for (auto& r : rects) {
        // Print out the corner coordinates
        std::cout << "x1: " << gtl::xl(r) << ", x2: " << gtl::xh(r)
                  << ", y1: " << gtl::yl(r) << ", y2: " << gtl::yh(r)
                  << std::endl;
    }
}

Prints

2 rectangle: 
x1: 0, x2: 170, y1: 0, y2: 235
x1: 170, x2: 310, y1: 0, y2: 305
sehe
  • 374,641
  • 47
  • 450
  • 633
  • (actually reading data from std::cin: http://coliru.stacked-crooked.com/a/d62eabd3242cd17d) – sehe Mar 06 '22 at 01:00