1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5 <title>Boost.Geometry (aka GGL, Generic Geometry Library)</title>
6 <link href="doxygen.css" rel="stylesheet" type="text/css">
7 <link href="tabs.css" rel="stylesheet" type="text/css">
9 <table cellpadding="2" width="100%">
13 <img alt="Boost.Geometry" src="images/ggl-logo-big.png" height="80" width="200">
16 <td valign="top" align="right">
17 <a href="http://www.boost.org">
18 <img alt="Boost C++ Libraries" src="images/accepted_by_boost.png" height="80" width="230" border="0">
24 <!-- Generated by Doxygen 1.8.6 -->
25 <div id="navrow1" class="tabs">
27 <li><a href="index.html"><span>Main Page</span></a></li>
28 <li><a href="pages.html"><span>Related Pages</span></a></li>
29 <li><a href="modules.html"><span>Modules</span></a></li>
30 <li><a href="namespaces.html"><span>Namespaces</span></a></li>
31 <li><a href="annotated.html"><span>Classes</span></a></li>
32 <li><a href="files.html"><span>Files</span></a></li>
33 <li><a href="examples.html"><span>Examples</span></a></li>
38 <div class="headertitle">
39 <div class="title">c04_b_custom_triangle_example.cpp</div> </div>
41 <div class="contents">
42 <p>This second custom triangle example shows an alternative implementation for a custom shape, showing a partial specialization for the area calculation.</p>
43 <div class="fragment"><div class="line"><span class="comment">// Boost.Geometry (aka GGL, Generic Geometry Library)</span></div>
44 <div class="line"></div>
45 <div class="line"><span class="comment">// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.</span></div>
46 <div class="line"><span class="comment">// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.</span></div>
47 <div class="line"><span class="comment">// Copyright (c) 2009-2012 Mateusz Loskot, London, UK.</span></div>
48 <div class="line"></div>
49 <div class="line"><span class="comment">// Use, modification and distribution is subject to the Boost Software License,</span></div>
50 <div class="line"><span class="comment">// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at</span></div>
51 <div class="line"><span class="comment">// http://www.boost.org/LICENSE_1_0.txt)</span></div>
52 <div class="line"><span class="comment">//</span></div>
53 <div class="line"><span class="comment">// Custom triangle template Example</span></div>
54 <div class="line"></div>
55 <div class="line"><span class="preprocessor">#include <iostream></span></div>
56 <div class="line"></div>
57 <div class="line"><span class="preprocessor">#include <boost/array.hpp></span></div>
58 <div class="line"><span class="preprocessor">#include <boost/tuple/tuple.hpp></span></div>
59 <div class="line"></div>
60 <div class="line"><span class="preprocessor">#include <<a class="code" href="algorithms_2area_8hpp.html">boost/geometry/algorithms/area.hpp</a>></span></div>
61 <div class="line"><span class="preprocessor">#include <<a class="code" href="algorithms_2centroid_8hpp.html">boost/geometry/algorithms/centroid.hpp</a>></span></div>
62 <div class="line"><span class="preprocessor">#include <<a class="code" href="boost__tuple_8hpp.html">boost/geometry/geometries/adapted/boost_tuple.hpp</a>></span></div>
63 <div class="line"><span class="preprocessor">#include <<a class="code" href="geometries_2register_2ring_8hpp.html">boost/geometry/geometries/register/ring.hpp</a>></span></div>
64 <div class="line"><span class="preprocessor">#include <<a class="code" href="strategies_8hpp.html">boost/geometry/strategies/strategies.hpp</a>></span></div>
65 <div class="line"><span class="preprocessor">#include <boost/geometry/io/dsv/write.hpp></span></div>
66 <div class="line"></div>
67 <div class="line"><a name="a0"></a><a class="code" href="boost__tuple_8hpp.html#ad912721c6dfdc38ab2cd6551c2436f97">BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS</a>(cs::cartesian)</div>
68 <div class="line"></div>
69 <div class="line"></div>
70 <div class="line">template <typename P></div>
71 <div class="line">struct triangle : public boost::array<P, 3></div>
72 <div class="line">{</div>
73 <div class="line">};</div>
74 <div class="line"></div>
75 <div class="line"></div>
76 <div class="line"><span class="comment">// Register triangle<P> as a ring</span></div>
77 <div class="line"><a name="a1"></a><a class="code" href="group__register.html#ga8a9071c3e9e9e0175250677a50e3fcc8">BOOST_GEOMETRY_REGISTER_RING_TEMPLATED</a>(triangle)</div>
78 <div class="line"></div>
79 <div class="line"></div>
80 <div class="line">namespace boost { <span class="keyword">namespace </span>geometry { <span class="keyword">namespace </span>dispatch {</div>
81 <div class="line"></div>
82 <div class="line"><span class="comment">// Specializations of area dispatch structure, implement algorithm</span></div>
83 <div class="line"><span class="keyword">template</span><<span class="keyword">typename</span> Po<span class="keywordtype">int</span>></div>
84 <div class="line"><span class="keyword">struct </span><a name="a2"></a><a class="code" href="group__area.html#gaf7a1c34467f74f290d0b090adb27db62">area</a><triangle<Point>, ring_tag></div>
85 <div class="line">{</div>
86 <div class="line"> <span class="keyword">template</span> <<span class="keyword">typename</span> Strategy></div>
87 <div class="line"> <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">double</span> apply(triangle<Point> <span class="keyword">const</span>& t, Strategy <span class="keyword">const</span>&)</div>
88 <div class="line"> {</div>
89 <div class="line"> <span class="keywordflow">return</span> 0.5 * ((get<0>(t[1]) - get<0>(t[0])) * (get<1>(t[2]) - get<1>(t[0]))</div>
90 <div class="line"> - (get<0>(t[2]) - get<0>(t[0])) * (get<1>(t[1]) - get<1>(t[0])));</div>
91 <div class="line"> }</div>
92 <div class="line">};</div>
93 <div class="line"></div>
94 <div class="line">}}} <span class="comment">// namespace boost::geometry::dispatch</span></div>
95 <div class="line"></div>
96 <div class="line"></div>
97 <div class="line"><span class="keywordtype">int</span> main()</div>
98 <div class="line">{</div>
99 <div class="line"> <span class="comment">//triangle<boost::geometry::point_xy<double> > t;</span></div>
100 <div class="line"> triangle<boost::tuple<double, double> > t;</div>
101 <div class="line"> t[0] = boost::make_tuple(0, 0);</div>
102 <div class="line"> t[1] = boost::make_tuple(5, 0);</div>
103 <div class="line"> t[2] = boost::make_tuple(2.5, 2.5);</div>
104 <div class="line"></div>
105 <div class="line"> std::cout << <span class="stringliteral">"Triangle: "</span> << boost::geometry::dsv(t) << std::endl;</div>
106 <div class="line"> std::cout << <span class="stringliteral">"Area: "</span> << <a class="code" href="group__area.html#gaf7a1c34467f74f290d0b090adb27db62">boost::geometry::area</a>(t) << std::endl;</div>
107 <div class="line"></div>
108 <div class="line"> <span class="comment">//boost::geometry::point_xy<double> c;</span></div>
109 <div class="line"> boost::tuple<double, double> c;</div>
110 <div class="line"> <a name="a3"></a><a class="code" href="group__centroid.html#ga3ec2c5e245c42ffaf28f1c88ea564c8c">boost::geometry::centroid</a>(t, c);</div>
111 <div class="line"> std::cout << <span class="stringliteral">"Centroid: "</span> << boost::geometry::dsv(c) << std::endl;</div>
112 <div class="line"></div>
113 <div class="line"> <span class="keywordflow">return</span> 0;</div>
114 <div class="line">}</div>
115 </div><!-- fragment --> </div><!-- contents -->
120 <td align="left"><small>
125 Copyright © 2007-2011 Barend Gehrels, Amsterdam, the Netherlands<br>
126 Copyright © 2008-2011 Bruno Lalande, Paris, France<br>
127 Copyright © 2009-2010 Mateusz Loskot, London, UK<br>
133 <address style="text-align: right;"><small>
134 Documentation is generated by <a href="http://www.doxygen.org/index.html">Doxygen</a>