Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6842661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:07:39+00:00 2026-05-27T00:07:39+00:00

There is great library for geometry in Boost. It allows also to draw SVG

  • 0

There is great library for geometry in Boost. It allows also to draw SVG images. I want to use it in some project of mine but it works really strange for me (see image below).

So we have 3 pixel points represented as square poligons in 2d space

 1 1
 0 1

enter image description here
pic 1

we want to get from them a union and simplify it so that when we scale it we’d get a triangle like

1 1 1 1 1 1
1 1 1 1 1 1  
1 1 1 1 1 1
0 1 1 1 1 1 
0 0 1 1 1 1 
0 0 0 1 1 1

enter image description here
pic 2

but we get this:

enter image description here

where yellow doted line is union and green is simplification.

Sourcecode:

#include <iostream>
#include <fstream>
#include <boost/assign.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/algorithms/envelope.hpp>

#include <boost/geometry/extensions/io/svg/svg_mapper.hpp>

template <typename Geometry1, typename Geometry2>
void create_svg(std::string const& filename, Geometry1 const& a, Geometry2 const& b)
{
    typedef typename boost::geometry::point_type<Geometry1>::type point_type;
    std::ofstream svg(filename.c_str());

    boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);
    mapper.add(a);
    mapper.add(b);

    mapper.map(a, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
    mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
}

int main()
{

    // create points (each point == square poligon)
    boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > one, two, three;

    boost::geometry::read_wkt(
        "POLYGON((1 1, 1 0, 0 0, 0 1))", one);

    boost::geometry::read_wkt(
        "POLYGON((2 2, 2 1, 1 1, 1 2))", two);

    boost::geometry::read_wkt(
        "POLYGON((1 1, 1 2, 0 2, 0 1))", three);

    // create a container for joined points structure
    boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > output, simpl;

    // join points one by one (because one day we would have many=))
    boost::geometry::union_(one, two, output);
    boost::geometry::union_( output , three, output);

    // simplify joined structure
    boost::geometry::simplify(output, simpl, 0.5);

    // create an svg image
    create_svg("make_envelope.svg", simpl, output );
}

requires at least boost 1.47.0 and 3 files from boost/geometry/extensions/io/svg/

So how to make it simplify like I want meaning to get shape like pic 2?

Update

Created new code, works correctly, quite tested:

#include <iostream>
#include <fstream>
#include <boost/assign.hpp>

//Boost
#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

#include <boost/foreach.hpp>

//and this is why we use Boost Geometry from Boost trunk 
#include <boost/geometry/extensions/io/svg/svg_mapper.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

template <typename Geometry1, typename Geometry2>
void create_svg(std::string const& filename, Geometry1 const& a, Geometry2 const& b)
{
    typedef typename boost::geometry::point_type<Geometry1>::type point_type;
    std::ofstream svg(filename.c_str());

    boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);
    mapper.add(a);
    mapper.add(b);

    mapper.map(a, "fill-rule:nonzero;fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2;");
    mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
}


void make_point(int x, int y,  boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > & ring)
{
    using namespace boost::assign;

    boost::geometry::append(  ring,     boost::geometry::model::d2::point_xy<double>(x-1, y-1));
    boost::geometry::append(  ring,     boost::geometry::model::d2::point_xy<double>(x, y-1));
    boost::geometry::append(  ring,      boost::geometry::model::d2::point_xy<double>(x, y));
    boost::geometry::append(  ring,      boost::geometry::model::d2::point_xy<double>(x-1, y));
    boost::geometry::append(  ring,     boost::geometry::model::d2::point_xy<double>(x-1, y-1));
    boost::geometry::correct(ring);
}

void create_point(int x, int y, boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > & mp)
{
    boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > temp;
    boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > ring;
    make_point(x, y, ring);
    boost::geometry::union_(mp, ring, temp);
    boost::geometry::correct(temp);
    mp=temp;
}

int main()
{
    using namespace boost::assign;

    typedef boost::geometry::model::polygon
        <
        boost::geometry::model::d2::point_xy<double>
        > polygon;

    typedef boost::geometry::model::multi_polygon<polygon> mp;

    polygon ring;

    mp pol, simpl;
    polygon exring;

    create_point(1,1, pol);
    create_point(2, 1, pol);
    create_point(3, 1, pol);
    create_point(4,1, pol);
    create_point(5, 1, pol);

    create_point(1,2, pol);
    create_point(2, 2, pol);
    create_point(3, 2, pol);
    create_point(4,2, pol);
    create_point(5, 2, pol);

    create_point(2, 3, pol);
    create_point(3, 3, pol);
    create_point(5, 3, pol);

    create_point(3, 4, pol);

    create_point(5, 3, pol);

    create_point(5, 5, pol);

    //boost::geometry::dissolve(ring, pol); // Baad
    boost::geometry::simplify(pol, simpl, 0.5); // Good

    create_svg("make_envelope.svg",pol,  simpl );
}

And this code creates such image:

enter image description here

And for 3 points it returns images alike @J. Calleja answer:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:07:40+00:00Added an answer on May 27, 2026 at 12:07 am

    I think there are several problems with the code:

    • The polygons you are defining are:

    1 1
    1 0

    That is:

    three two
    one    -
    

    So the expected result is different from pic2.

    • Polygons should be closed, and directed clockwise.

    You are missing the closing point and the third polygon is not directed clockwise. Take a look at the correct method. On this example, you should call it for every polygon you define.

    • You cannot use the same argument for input and output when using _union.

    You should use a temporary variable:

      boost::geometry::union_(one, two, outputTmp);    
      boost::geometry::union_( outputTmp, three, output);  
    
    • Your expected result may not be the algorithm result.

    After executing the corrected code, the result is:

    simplify result

    This may be a valid simplifcation of your polygon. See the Ramer–Douglas–Peucker algorithm.

    After performing those modifications, below is the resulting main()

    int main() 
    {
      // create points (each point == square poligon)     
      boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > one, two, three;  
      boost::geometry::read_wkt(         "POLYGON((1 1, 1 0, 0 0, 0 1))", one);  
      boost::geometry::read_wkt(         "POLYGON((2 2, 2 1, 1 1, 1 2))", two); 
      boost::geometry::read_wkt(         "POLYGON((1 1, 1 2, 0 2, 0 1))", three);  
      boost::geometry::correct(one);
      boost::geometry::correct(two);
      boost::geometry::correct(three);
    
      // create a container for joined points structure  
      boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > outputTmp, output, simpl;      
      // join points one by one (because one day we would have many=))    
      boost::geometry::union_(one, two, outputTmp);    
      boost::geometry::union_( outputTmp, three, output);    
      // simplify joined structure  
      boost::geometry::simplify(output, simpl, 0.5);   
      // create an svg image   
      create_svg("make_envelope.svg", simpl, output ); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using opensource library called wxFreeChart to draw some XY charts. In example there
Is there some library that has something like log4j logger that would persist exception
Hi there i had been sucessfully using this great library PDF Sharp.now i wanted
There is a great library I found here . It's AeroWizard done in Windows
There's a great project called the Ruby Koans , it's a series of tasks
Sorry this is a total newb question but there is this really great C-library
Is there any library out there that I can use to create epub files
i know nothing about medical records but im sure there's great opportunity in it
WPF is great because there are many ways to achieve your goals. For example,
There are a number of great Javascript libraries\frameworks out there (jQuery, Prototype, MooTools, etc.),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.