使用boost geometry将boost段分成N个相等的段
divide boost segment into N equal segments using boost geometry
我使用 boost 几何模型创建了一条线段。我想知道是否有一种方法可以在 boost 中将线段划分为 N 个相等的线段。
请不要将此问题视为 this 的重复版本。
(编辑)
因为已经过时了,我已经尝试过为这个问题提供的答案。
如果需要将AB段分成N等份,那么第i段的起点坐标(和前一段的终点)可以用参数线方程计算。
伪代码:
for i = 0.. N-1
t = i / N
Start[i].X = A.X * (1 - t) + B.X * t
Start[i].Y = A.Y * (1 - t) + B.Y * t
想到的最简单的事情:
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/arithmetic/arithmetic.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <iostream>
template <typename Segment, typename Point = typename boost::geometry::point_type<Segment>::type>
auto split_into(unsigned N, Segment const& s) {
using namespace boost::geometry;
assert(N>1);
auto step = s.second;
subtract_point(step, s.first);
divide_value(step, N-1);
std::vector<Point> segments;
std::generate_n(back_inserter(segments), N, [accu=s.first, step] () mutable { Point p = accu; return add_point(accu, step), p; });
return model::linestring<Point> {segments.begin(), segments.end()};
}
using namespace boost::geometry;
using Point = model::d2::point_xy<double>;
int main() {
model::segment<Point> const line { {1,3}, {11,33} };
std::cout << wkt(line) << "\n";
std::cout << wkt(split_into(5, line)) << "\n";
}
版画
LINESTRING(1 3,11 33)
LINESTRING(1 3,3.5 10.5,6 18,8.5 25.5,11 33)
花式范围版本
对于大 N 可能更好地扩展的一个:
#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>
template <typename Segment, typename Point = typename boost::geometry::point_type<Segment>::type>
auto split_into(unsigned N, Segment const& s) {
using namespace boost::geometry;
using namespace boost::adaptors;
assert(N>1);
auto step = s.second;
subtract_point(step, s.first);
divide_value(step, N-1);
auto gen = boost::irange(0u, N) | transformed([=](int i) { auto p = step; multiply_value(p, i); add_point(p, s.first); return p; });
return model::linestring<Point> { boost::begin(gen), boost::end(gen) };
}
版画
LINESTRING(1 3,11 33)
LINESTRING(1 3,3.5 10.5,6 18,8.5 25.5,11 33)
我使用 boost 几何模型创建了一条线段。我想知道是否有一种方法可以在 boost 中将线段划分为 N 个相等的线段。
请不要将此问题视为 this 的重复版本。 (编辑) 因为已经过时了,我已经尝试过为这个问题提供的答案。
如果需要将AB段分成N等份,那么第i段的起点坐标(和前一段的终点)可以用参数线方程计算。
伪代码:
for i = 0.. N-1
t = i / N
Start[i].X = A.X * (1 - t) + B.X * t
Start[i].Y = A.Y * (1 - t) + B.Y * t
想到的最简单的事情:
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/arithmetic/arithmetic.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <iostream>
template <typename Segment, typename Point = typename boost::geometry::point_type<Segment>::type>
auto split_into(unsigned N, Segment const& s) {
using namespace boost::geometry;
assert(N>1);
auto step = s.second;
subtract_point(step, s.first);
divide_value(step, N-1);
std::vector<Point> segments;
std::generate_n(back_inserter(segments), N, [accu=s.first, step] () mutable { Point p = accu; return add_point(accu, step), p; });
return model::linestring<Point> {segments.begin(), segments.end()};
}
using namespace boost::geometry;
using Point = model::d2::point_xy<double>;
int main() {
model::segment<Point> const line { {1,3}, {11,33} };
std::cout << wkt(line) << "\n";
std::cout << wkt(split_into(5, line)) << "\n";
}
版画
LINESTRING(1 3,11 33)
LINESTRING(1 3,3.5 10.5,6 18,8.5 25.5,11 33)
花式范围版本
对于大 N 可能更好地扩展的一个:
#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>
template <typename Segment, typename Point = typename boost::geometry::point_type<Segment>::type>
auto split_into(unsigned N, Segment const& s) {
using namespace boost::geometry;
using namespace boost::adaptors;
assert(N>1);
auto step = s.second;
subtract_point(step, s.first);
divide_value(step, N-1);
auto gen = boost::irange(0u, N) | transformed([=](int i) { auto p = step; multiply_value(p, i); add_point(p, s.first); return p; });
return model::linestring<Point> { boost::begin(gen), boost::end(gen) };
}
版画
LINESTRING(1 3,11 33)
LINESTRING(1 3,3.5 10.5,6 18,8.5 25.5,11 33)