mirror of
https://github.com/boostorg/geometry.git
synced 2026-02-13 12:32:09 +00:00
197 lines
9.1 KiB
HTML
197 lines
9.1 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>Boost.Geometry (aka GGL, Generic Geometry Library)</title>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css">
|
|
<link href="tabs.css" rel="stylesheet" type="text/css">
|
|
</head>
|
|
|
|
<table cellpadding="2" width="100%">
|
|
<tbody>
|
|
<tr>
|
|
<td valign="top">
|
|
<img alt="Boost.Geometry" src="images/ggl-logo-big.png" height="80" width="200">
|
|
|
|
</td>
|
|
<td valign="top" align="right">
|
|
<a href="http://www.boost.org">
|
|
<img alt="Boost C++ Libraries" src="images/accepted_by_boost.png" height="80" width="230" border="0">
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<!-- Generated by Doxygen 1.5.9 -->
|
|
<div class="navigation" id="top">
|
|
<div class="tabs">
|
|
<ul>
|
|
<li><a href="index.html"><span>Main Page</span></a></li>
|
|
<li><a href="pages.html"><span>Related Pages</span></a></li>
|
|
<li><a href="modules.html"><span>Modules</span></a></li>
|
|
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
|
|
<li><a href="annotated.html"><span>Classes</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
<li><a href="examples.html"><span>Examples</span></a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="contents">
|
|
<h1>c06_custom_polygon_example.cpp</h1>Showing a custom polygon (asked on the list during Formal Review)<p>
|
|
<div class="fragment"><pre class="fragment"><span class="comment">// Boost.Geometry (aka GGL, Generic Geometry Library)</span>
|
|
<span class="comment">//</span>
|
|
<span class="comment">// Copyright Barend Gehrels 2007-2009, Geodan, Amsterdam, the Netherlands</span>
|
|
<span class="comment">// Use, modification and distribution is subject to the Boost Software License,</span>
|
|
<span class="comment">// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at</span>
|
|
<span class="comment">// http://www.boost.org/LICENSE_1_0.txt)</span>
|
|
<span class="comment">//</span>
|
|
<span class="comment">// Custom Polygon Example</span>
|
|
<span class="preprocessor">#include <iostream></span>
|
|
|
|
<span class="preprocessor">#include <boost/geometry/geometry.hpp></span>
|
|
|
|
<span class="preprocessor">#include <<a class="code" href="register_2point_8hpp.html">boost/geometry/geometries/register/point.hpp</a>></span>
|
|
<span class="preprocessor">#include <<a class="code" href="ring_8hpp.html">boost/geometry/geometries/register/ring.hpp</a>></span>
|
|
|
|
|
|
<span class="keyword">struct </span>my_point
|
|
{
|
|
my_point(<span class="keywordtype">double</span> an_x = 0, <span class="keywordtype">double</span> an_y = 0)
|
|
: x(an_x)
|
|
, y(an_y)
|
|
{}
|
|
|
|
<span class="keywordtype">double</span> x, y;
|
|
};
|
|
|
|
<span class="keyword">struct </span>my_ring : std::deque<my_point>
|
|
{};
|
|
|
|
<span class="comment">// Define a struct of a polygon, having always two holes</span>
|
|
<span class="comment">// (of course this can be implemented differently, usually</span>
|
|
<span class="comment">// with a vector or deque, but it is just an exampe)</span>
|
|
<span class="keyword">struct </span>my_polygon
|
|
{
|
|
<span class="comment">// required for a polygon: an outer ring...</span>
|
|
my_ring boundary;
|
|
<span class="comment">// ... and a Boost.Range compatible inner ring collection</span>
|
|
boost::array<my_ring, 2> holes;
|
|
|
|
<span class="comment">// just for the sample</span>
|
|
std::string name;
|
|
|
|
my_polygon(std::string <span class="keyword">const</span>& n = <span class="stringliteral">""</span>) : name(n) {}
|
|
};
|
|
|
|
|
|
<span class="comment">// We can conveniently use macro's to register point and ring</span>
|
|
<a name="a0"></a><a class="code" href="register_2point_8hpp.html#040a8280aaf06372fc637e1a436019d8">BOOST_GEOMETRY_REGISTER_POINT_2D</a>(my_point, <span class="keywordtype">double</span>, cs::cartesian, x, y)
|
|
<a name="a1"></a><a class="code" href="ring_8hpp.html#742491bcf6ece3b0c9d21d5f6a991ac1">BOOST_GEOMETRY_REGISTER_RING</a>(my_ring)
|
|
|
|
|
|
|
|
<span class="comment">// There is currently no registration macro for polygons</span>
|
|
<span class="comment">// and besides that a boost::array<T,N> in a macro would</span>
|
|
<span class="comment">// be very specific, so we show it "by hand":</span>
|
|
namespace boost { <span class="keyword">namespace </span>geometry { <span class="keyword">namespace </span>traits
|
|
{
|
|
|
|
<span class="keyword">template</span><> <span class="keyword">struct </span>tag<my_polygon> { <span class="keyword">typedef</span> polygon_tag type; };
|
|
<span class="keyword">template</span><> <span class="keyword">struct </span>ring_type<my_polygon> { <span class="keyword">typedef</span> my_ring type; };
|
|
|
|
<span class="keyword">template</span><> <span class="keyword">struct </span>interior_type<my_polygon>
|
|
{
|
|
<span class="keyword">typedef</span> boost::array<my_ring, 2> type;
|
|
};
|
|
|
|
|
|
<span class="keyword">template</span><> <span class="keyword">struct </span><a name="a2"></a><a class="code" href="group__access.html#gd96ba07cd3c6c3216bd5ab69adc9ccb5" title="Function to get the exterior_ring ring of a polygon.">exterior_ring</a><my_polygon>
|
|
{
|
|
<span class="keyword">static</span> my_ring& <span class="keyword">get</span>(my_polygon& p)
|
|
{
|
|
<span class="keywordflow">return</span> p.boundary;
|
|
}
|
|
|
|
<span class="keyword">static</span> my_ring <span class="keyword">const</span>& <span class="keyword">get</span>(my_polygon <span class="keyword">const</span>& p)
|
|
{
|
|
<span class="keywordflow">return</span> p.boundary;
|
|
}
|
|
};
|
|
|
|
<span class="keyword">template</span><> <span class="keyword">struct </span><a name="a3"></a><a class="code" href="group__access.html#g5af585b056bb80bf3dfe7c07ad30d84a" title="Function to get the interior rings of a polygon (non const version).">interior_rings</a><my_polygon>
|
|
{
|
|
<span class="keyword">typedef</span> boost::array<my_ring, 2> holes_type;
|
|
|
|
<span class="keyword">static</span> holes_type& <span class="keyword">get</span>(my_polygon& p)
|
|
{
|
|
<span class="keywordflow">return</span> p.holes;
|
|
}
|
|
|
|
<span class="keyword">static</span> holes_type <span class="keyword">const</span>& <span class="keyword">get</span>(my_polygon <span class="keyword">const</span>& p)
|
|
{
|
|
<span class="keywordflow">return</span> p.holes;
|
|
}
|
|
};
|
|
|
|
}}} <span class="comment">// namespace boost::geometry::traits</span>
|
|
|
|
|
|
|
|
<span class="keywordtype">int</span> main()
|
|
{
|
|
my_polygon p1(<span class="stringliteral">"my polygon"</span>);
|
|
|
|
<span class="comment">// Fill it the my-way, triangle</span>
|
|
p1.boundary.push_back(my_point(2, 0));
|
|
p1.boundary.push_back(my_point(1, 5));
|
|
p1.boundary.push_back(my_point(5, 5));
|
|
p1.boundary.push_back(my_point(2, 0));
|
|
|
|
<span class="comment">// Triangle</span>
|
|
p1.holes[0].push_back(my_point(2, 1));
|
|
p1.holes[0].push_back(my_point(1.9, 2));
|
|
p1.holes[0].push_back(my_point(2.4, 2));
|
|
p1.holes[0].push_back(my_point(2, 1));
|
|
|
|
<span class="comment">// Box</span>
|
|
p1.holes[1].push_back(my_point(3, 3));
|
|
p1.holes[1].push_back(my_point(3, 4));
|
|
p1.holes[1].push_back(my_point(4, 4));
|
|
p1.holes[1].push_back(my_point(4, 3));
|
|
p1.holes[1].push_back(my_point(3, 3));
|
|
|
|
std::cout << <span class="stringliteral">"Representation of "</span> << p1.name << <span class="stringliteral">": "</span>
|
|
<< <a name="a4"></a><a class="code" href="group__utility.html#g62cc5db4d3bb1147591298b3500f8f1a" title="Main DSV-streaming function.">boost::geometry::dsv</a>(p1) << std::endl;
|
|
std::cout << <span class="stringliteral">"Area of "</span> << p1.name << <span class="stringliteral">": "</span>
|
|
<< <a name="a5"></a><a class="code" href="group__area.html#gd0e9e99685a9d45895162bd1fd96a136" title="Calculate area of a geometry.">boost::geometry::area</a>(p1) << std::endl;
|
|
std::cout << <span class="stringliteral">"Perimeter of "</span> << p1.name << <span class="stringliteral">": "</span>
|
|
<< <a name="a6"></a><a class="code" href="group__perimeter.html#gf9bf2e5d5002baec58778669cefb564b" title="Calculate perimeter of a geometry.">boost::geometry::perimeter</a>(p1) << std::endl;
|
|
std::cout << <span class="stringliteral">"Centroid of "</span> << p1.name << <span class="stringliteral">": "</span>
|
|
<< <a class="code" href="group__utility.html#g62cc5db4d3bb1147591298b3500f8f1a" title="Main DSV-streaming function.">boost::geometry::dsv</a>(boost::geometry::make_centroid<my_point>(p1)) << std::endl;
|
|
|
|
<span class="keywordflow">return</span> 0;
|
|
}
|
|
</pre></div> </div>
|
|
<hr size="1">
|
|
<table width="100%">
|
|
<tbody>
|
|
<tr>
|
|
<td align="left"><small>
|
|
<p>December 1, 2009</p>
|
|
</small></td>
|
|
<td align="right">
|
|
<small>Copyright © 1995-2009 Barend Gehrels, Geodan, Amsterdam<br>
|
|
Copyright © 2008-2009 Bruno Lalande, Paris<br>
|
|
Copyright © 2009 Mateusz Loskot, Cadcorp, London<br>
|
|
</small>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<address style="text-align: right;"><small>
|
|
Documentation is generated by <a href="http://www.doxygen.org/index.html">Doxygen</a>
|
|
</small></address>
|
|
</body>
|
|
</html>
|