1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>intersection</title> 5<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../../index.html" title="Chapter 1. Geometry"> 8<link rel="up" href="../intersection.html" title="intersection"> 9<link rel="prev" href="intersection_4_with_strategy.html" title="intersection (with strategy)"> 10<link rel="next" href="../intersects.html" title="intersects"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="intersection_4_with_strategy.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intersection.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../intersects.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h5 class="title"> 27<a name="geometry.reference.algorithms.intersection.intersection_3"></a><a class="link" href="intersection_3.html" title="intersection">intersection</a> 28</h5></div></div></div> 29<p> 30 <a class="indexterm" name="idm45635695715136"></a> 31Calculate the intersection of two geometries. 32 </p> 33<h6> 34<a name="geometry.reference.algorithms.intersection.intersection_3.h0"></a> 35 <span class="phrase"><a name="geometry.reference.algorithms.intersection.intersection_3.description"></a></span><a class="link" href="intersection_3.html#geometry.reference.algorithms.intersection.intersection_3.description">Description</a> 36 </h6> 37<p> 38 The free function intersection calculates the spatial set theoretic intersection 39 of two geometries. 40 </p> 41<h6> 42<a name="geometry.reference.algorithms.intersection.intersection_3.h1"></a> 43 <span class="phrase"><a name="geometry.reference.algorithms.intersection.intersection_3.synopsis"></a></span><a class="link" href="intersection_3.html#geometry.reference.algorithms.intersection.intersection_3.synopsis">Synopsis</a> 44 </h6> 45<p> 46</p> 47<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Geometry1</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Geometry2</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">GeometryOut</span><span class="special">></span> 48<span class="keyword">bool</span> <span class="identifier">intersection</span><span class="special">(</span><span class="identifier">Geometry1</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">geometry1</span><span class="special">,</span> <span class="identifier">Geometry2</span> <span class="keyword">const</span> <span class="special">&</span> <span class="identifier">geometry2</span><span class="special">,</span> <span class="identifier">GeometryOut</span> <span class="special">&</span> <span class="identifier">geometry_out</span><span class="special">)</span></pre> 49<p> 50 </p> 51<h6> 52<a name="geometry.reference.algorithms.intersection.intersection_3.h2"></a> 53 <span class="phrase"><a name="geometry.reference.algorithms.intersection.intersection_3.parameters"></a></span><a class="link" href="intersection_3.html#geometry.reference.algorithms.intersection.intersection_3.parameters">Parameters</a> 54 </h6> 55<div class="informaltable"><table class="table"> 56<colgroup> 57<col> 58<col> 59<col> 60<col> 61</colgroup> 62<thead><tr> 63<th> 64 <p> 65 Type 66 </p> 67 </th> 68<th> 69 <p> 70 Concept 71 </p> 72 </th> 73<th> 74 <p> 75 Name 76 </p> 77 </th> 78<th> 79 <p> 80 Description 81 </p> 82 </th> 83</tr></thead> 84<tbody> 85<tr> 86<td> 87 <p> 88 Geometry1 const & 89 </p> 90 </td> 91<td> 92 <p> 93 Any type fulfilling a Geometry Concept 94 </p> 95 </td> 96<td> 97 <p> 98 geometry1 99 </p> 100 </td> 101<td> 102 <p> 103 A model of the specified concept 104 </p> 105 </td> 106</tr> 107<tr> 108<td> 109 <p> 110 Geometry2 const & 111 </p> 112 </td> 113<td> 114 <p> 115 Any type fulfilling a Geometry Concept 116 </p> 117 </td> 118<td> 119 <p> 120 geometry2 121 </p> 122 </td> 123<td> 124 <p> 125 A model of the specified concept 126 </p> 127 </td> 128</tr> 129<tr> 130<td> 131 <p> 132 GeometryOut & 133 </p> 134 </td> 135<td> 136 <p> 137 Collection of geometries (e.g. std::vector, std::deque, boost::geometry::multi*) 138 of which the value_type fulfills a Point, LineString or Polygon 139 concept, or it is the output geometry (e.g. for a box) 140 </p> 141 </td> 142<td> 143 <p> 144 geometry_out 145 </p> 146 </td> 147<td> 148 <p> 149 The output geometry, either a multi_point, multi_polygon, multi_linestring, 150 or a box (for intersection of two boxes) 151 </p> 152 </td> 153</tr> 154</tbody> 155</table></div> 156<h6> 157<a name="geometry.reference.algorithms.intersection.intersection_3.h3"></a> 158 <span class="phrase"><a name="geometry.reference.algorithms.intersection.intersection_3.header"></a></span><a class="link" href="intersection_3.html#geometry.reference.algorithms.intersection.intersection_3.header">Header</a> 159 </h6> 160<p> 161 Either 162 </p> 163<p> 164 <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> 165 </p> 166<p> 167 Or 168 </p> 169<p> 170 <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">algorithms</span><span class="special">/</span><span class="identifier">intersection</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> 171 </p> 172<h6> 173<a name="geometry.reference.algorithms.intersection.intersection_3.h4"></a> 174 <span class="phrase"><a name="geometry.reference.algorithms.intersection.intersection_3.conformance"></a></span><a class="link" href="intersection_3.html#geometry.reference.algorithms.intersection.intersection_3.conformance">Conformance</a> 175 </h6> 176<p> 177 The function intersection implements function Intersection from the 178 <a href="http://www.opengeospatial.org/standards/sfa" target="_top">OGC Simple Feature 179 Specification</a>. 180 </p> 181<h6> 182<a name="geometry.reference.algorithms.intersection.intersection_3.h5"></a> 183 <span class="phrase"><a name="geometry.reference.algorithms.intersection.intersection_3.behavior"></a></span><a class="link" href="intersection_3.html#geometry.reference.algorithms.intersection.intersection_3.behavior">Behavior</a> 184 </h6> 185<div class="informaltable"><table class="table"> 186<colgroup> 187<col> 188<col> 189</colgroup> 190<thead><tr> 191<th> 192 <p> 193 Case 194 </p> 195 </th> 196<th> 197 <p> 198 Behavior 199 </p> 200 </th> 201</tr></thead> 202<tbody> 203<tr> 204<td> 205 <p> 206 GeometryOut is a Point 207 </p> 208 </td> 209<td> 210 <p> 211 Calculates intersection points of input geometries 212 </p> 213 </td> 214</tr> 215<tr> 216<td> 217 <p> 218 GeometryOut is a Linestring 219 </p> 220 </td> 221<td> 222 <p> 223 Calculates intersection linestrings of input (multi)linestrings 224 </p> 225 </td> 226</tr> 227<tr> 228<td> 229 <p> 230 GeometryOut is a Polygon 231 </p> 232 </td> 233<td> 234 <p> 235 Calculates intersection polygons of input (multi)polygons and/or 236 boxes 237 </p> 238 </td> 239</tr> 240</tbody> 241</table></div> 242<div class="note"><table border="0" summary="Note"> 243<tr> 244<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../../doc/src/images/note.png"></td> 245<th align="left">Note</th> 246</tr> 247<tr><td align="left" valign="top"><p> 248 Check the <a class="link" href="../../concepts/concept_polygon.html" title="Polygon Concept">Polygon 249 Concept</a> for the rules that polygon input for this algorithm 250 should fulfill 251 </p></td></tr> 252</table></div> 253<h6> 254<a name="geometry.reference.algorithms.intersection.intersection_3.h6"></a> 255 <span class="phrase"><a name="geometry.reference.algorithms.intersection.intersection_3.example"></a></span><a class="link" href="intersection_3.html#geometry.reference.algorithms.intersection.intersection_3.example">Example</a> 256 </h6> 257<p> 258 Shows the intersection of two polygons 259 </p> 260<p> 261</p> 262<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 263<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">deque</span><span class="special">></span> 264 265<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 266<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">point_xy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 267<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">polygon</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 268 269<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">foreach</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 270 271 272<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 273<span class="special">{</span> 274 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">polygon</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">d2</span><span class="special">::</span><span class="identifier">point_xy</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="special">></span> <span class="identifier">polygon</span><span class="special">;</span> 275 276 <span class="identifier">polygon</span> <span class="identifier">green</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">;</span> 277 278 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">read_wkt</span><span class="special">(</span> 279 <span class="string">"POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"</span> 280 <span class="string">"(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))"</span><span class="special">,</span> <span class="identifier">green</span><span class="special">);</span> 281 282 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">read_wkt</span><span class="special">(</span> 283 <span class="string">"POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))"</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">);</span> 284 285 <span class="identifier">std</span><span class="special">::</span><span class="identifier">deque</span><span class="special"><</span><span class="identifier">polygon</span><span class="special">></span> <span class="identifier">output</span><span class="special">;</span> 286 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">intersection</span><span class="special">(</span><span class="identifier">green</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">,</span> <span class="identifier">output</span><span class="special">);</span> 287 288 <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 289 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"green && blue:"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 290 <span class="identifier">BOOST_FOREACH</span><span class="special">(</span><span class="identifier">polygon</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">output</span><span class="special">)</span> 291 <span class="special">{</span> 292 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">i</span><span class="special">++</span> <span class="special"><<</span> <span class="string">": "</span> <span class="special"><<</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">area</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 293 <span class="special">}</span> 294 295 296 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 297<span class="special">}</span> 298</pre> 299<p> 300 </p> 301<p> 302 Output: 303 </p> 304<pre class="programlisting">green && blue: 3050: 2.50205 306 307<img src="../../../../img/algorithms/intersection.png" alt="intersection"> 308 309</pre> 310<h6> 311<a name="geometry.reference.algorithms.intersection.intersection_3.h7"></a> 312 <span class="phrase"><a name="geometry.reference.algorithms.intersection.intersection_3.see_also"></a></span><a class="link" href="intersection_3.html#geometry.reference.algorithms.intersection.intersection_3.see_also">See 313 also</a> 314 </h6> 315<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 316<li class="listitem"> 317 <a class="link" href="../union_.html" title="union_">union</a> 318 </li> 319<li class="listitem"> 320 <a class="link" href="../difference.html" title="difference">difference</a> 321 </li> 322<li class="listitem"> 323 <a class="link" href="../sym_difference.html" title="sym_difference">sym_difference 324 (symmetric difference)</a> 325 </li> 326</ul></div> 327</div> 328<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 329<td align="left"></td> 330<td align="right"><div class="copyright-footer">Copyright © 2009-2019 Barend Gehrels, Bruno Lalande, Mateusz Loskot, Adam 331 Wulkiewicz, Oracle and/or its affiliates<p> 332 Distributed under the Boost Software License, Version 1.0. (See accompanying 333 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 334 </p> 335</div></td> 336</tr></table> 337<hr> 338<div class="spirit-nav"> 339<a accesskey="p" href="intersection_4_with_strategy.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../intersection.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../intersects.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 340</div> 341</body> 342</html> 343