4

我查看了名为Boost Geometry的 grate 库 ,我查看了它,但没有看到任何关于处理任何东西的教程,至少有点图形化。所以我想知道是否有人可以帮助提供一个简单的教程来创建一些N随机 poligons(颜色大小和形式随机)并将 tham 保存为像SVG这样的矢量图像?

4

2 回答 2

11

所以......解决了它:在谷歌上你可以找到这个旧代码。它不会与最新的 boost 1.47.0 一起编译。因此,您将尝试修复它,例如,您会在一些过时的文档上到达这里……长话短说,您应该做些什么来使它工作:

下载 3 个代码文件boost/geometry/extensions/io/svg/,它们只是标题,所以不用担心。

现在您可以为当前的 boost 代码编译固定、更新的代码:

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

#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/io/svg/write_svg.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/algorithms/envelope.hpp>

#include <boost/geometry/io/svg/write_svg.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()
{
    using namespace boost::assign;


    boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > ring;
    ring +=
        boost::geometry::model::d2::point_xy<double>(4.0, -0.5), boost::geometry::model::d2::point_xy<double>(3.5, 1.0),
        boost::geometry::model::d2::point_xy<double>(2.0, 1.5), boost::geometry::model::d2::point_xy<double>(3.5, 2.0),
        boost::geometry::model::d2::point_xy<double>(4.0, 3.5), boost::geometry::model::d2::point_xy<double>(4.5, 2.0),
        boost::geometry::model::d2::point_xy<double>(6.0, 1.5), boost::geometry::model::d2::point_xy<double>(4.5, 1.0),
        boost::geometry::model::d2::point_xy<double>(4.0, -0.5);


     boost::geometry::model::box<boost::geometry::model::d2::point_xy<double> > box;
     boost::geometry::envelope(ring, box); 
    std::cout
        << "make_envelope:"
        << boost::geometry::dsv(box)
        << std::endl;

    create_svg("make_envelope.svg", ring, box);
}

这将绘制:

在此处输入图像描述

(不是图像,而是可在谷歌浏览器中打开的矢量 svg 文件=))所以这就是如何在 C++ 中从矢量创建 SVG 文件=)

于 2011-11-08T08:36:35.253 回答
0

对于boost >= 1.54我们现在官方支持的版本,来自boost::geometry::io::svg下的库。

#include <string>
#include <fstream>

#include <boost/geometry.hpp>


namespace bg = boost::geometry;
namespace bgm = boost::geometry::model;

using Point2D = bgm::d2::point_xy<double>;
using Polygon2D = bgm::polygon<Point2D>;


template <typename Geometry>
void create_svg(std::string const& file_path, Geometry const& geometry, std::string const& style) {
    using PointType = typename bg::point_type<Geometry>::type;
    std::ofstream svg_file(file_path.c_str());
    bg::svg_mapper<PointType> mapper(svg_file, 400, 400);
    mapper.add(geometry);
    mapper.map(geometry, style);
}

Polygon2D create_polygon(Point2D const& p1, Point2D const& p2, Point2D const& p3, Point2D const& p4) {
    // NOTE: For closed poly first point equal to the last
    return {{p1, p2, p3, p4, p1}};
}

int main() {
    auto const& polygon = create_polygon({0., 0.}, {0., 4.}, {7., 4.}, {7., 0.});
    std::string style{"fill-rule:nonzero;fill-opacity:0.5;fill:yellow;stroke:black;stroke-width:2;"};
    create_svg("image.svg", polygon, style);
    return 0;
}

这给了我们

在此处输入图像描述

完整的播放代码可在此处获得。

于 2021-01-07T15:08:36.297 回答