• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>centroid</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="../centroid.html" title="centroid">
9<link rel="prev" href="centroid_3_with_strategy.html" title="centroid (with strategy)">
10<link rel="next" href="return_centroid_1.html" title="return_centroid">
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="centroid_3_with_strategy.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../centroid.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="return_centroid_1.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.centroid.centroid_2"></a><a class="link" href="centroid_2.html" title="centroid">centroid</a>
28</h5></div></div></div>
29<p>
30            <a class="indexterm" name="idm45635705649744"></a>
31Calculates the centroid of a geometry.
32          </p>
33<h6>
34<a name="geometry.reference.algorithms.centroid.centroid_2.h0"></a>
35            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.description"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.description">Description</a>
36          </h6>
37<p>
38            The free function centroid calculates the geometric center (or: center
39            of mass) of a geometry. It uses the default strategy, based on the coordinate
40            system of the geometry.
41          </p>
42<h6>
43<a name="geometry.reference.algorithms.centroid.centroid_2.h1"></a>
44            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.synopsis"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.synopsis">Synopsis</a>
45          </h6>
46<p>
47</p>
48<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Geometry</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Point</span><span class="special">&gt;</span>
49<span class="keyword">void</span> <span class="identifier">centroid</span><span class="special">(</span><span class="identifier">Geometry</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">geometry</span><span class="special">,</span> <span class="identifier">Point</span> <span class="special">&amp;</span> <span class="identifier">c</span><span class="special">)</span></pre>
50<p>
51          </p>
52<h6>
53<a name="geometry.reference.algorithms.centroid.centroid_2.h2"></a>
54            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.parameters"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.parameters">Parameters</a>
55          </h6>
56<div class="informaltable"><table class="table">
57<colgroup>
58<col>
59<col>
60<col>
61<col>
62</colgroup>
63<thead><tr>
64<th>
65                    <p>
66                      Type
67                    </p>
68                  </th>
69<th>
70                    <p>
71                      Concept
72                    </p>
73                  </th>
74<th>
75                    <p>
76                      Name
77                    </p>
78                  </th>
79<th>
80                    <p>
81                      Description
82                    </p>
83                  </th>
84</tr></thead>
85<tbody>
86<tr>
87<td>
88                    <p>
89                      Geometry const &amp;
90                    </p>
91                  </td>
92<td>
93                    <p>
94                      Any type fulfilling a Geometry Concept
95                    </p>
96                  </td>
97<td>
98                    <p>
99                      geometry
100                    </p>
101                  </td>
102<td>
103                    <p>
104                      A model of the specified concept
105                    </p>
106                  </td>
107</tr>
108<tr>
109<td>
110                    <p>
111                      Point &amp;
112                    </p>
113                  </td>
114<td>
115                    <p>
116                      Any type fulfilling a Point Concept
117                    </p>
118                  </td>
119<td>
120                    <p>
121                      c
122                    </p>
123                  </td>
124<td>
125                    <p>
126                      The calculated centroid will be assigned to this point reference
127                    </p>
128                  </td>
129</tr>
130</tbody>
131</table></div>
132<h6>
133<a name="geometry.reference.algorithms.centroid.centroid_2.h3"></a>
134            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.header"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.header">Header</a>
135          </h6>
136<p>
137            Either
138          </p>
139<p>
140            <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span></code>
141          </p>
142<p>
143            Or
144          </p>
145<p>
146            <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</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">centroid</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
147          </p>
148<h6>
149<a name="geometry.reference.algorithms.centroid.centroid_2.h4"></a>
150            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.conformance"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.conformance">Conformance</a>
151          </h6>
152<p>
153            The function centroid implements function Centroid from the <a href="http://www.opengeospatial.org/standards/sfa" target="_top">OGC
154            Simple Feature Specification</a>.
155          </p>
156<h6>
157<a name="geometry.reference.algorithms.centroid.centroid_2.h5"></a>
158            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.behavior"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.behavior">Behavior</a>
159          </h6>
160<div class="informaltable"><table class="table">
161<colgroup>
162<col>
163<col>
164</colgroup>
165<thead><tr>
166<th>
167                    <p>
168                      Case
169                    </p>
170                  </th>
171<th>
172                    <p>
173                      Behavior
174                    </p>
175                  </th>
176</tr></thead>
177<tbody>
178<tr>
179<td>
180                    <p>
181                      Point
182                    </p>
183                  </td>
184<td>
185                    <p>
186                      Returns the point itself as the centroid
187                    </p>
188                  </td>
189</tr>
190<tr>
191<td>
192                    <p>
193                      Multi Point
194                    </p>
195                  </td>
196<td>
197                    <p>
198                      Calculates centroid (based on average)
199                    </p>
200                  </td>
201</tr>
202<tr>
203<td>
204                    <p>
205                      linear (e.g. linestring)
206                    </p>
207                  </td>
208<td>
209                    <p>
210                      Calculates centroid (based on weighted length)
211                    </p>
212                  </td>
213</tr>
214<tr>
215<td>
216                    <p>
217                      areal (e.g. polygon)
218                    </p>
219                  </td>
220<td>
221                    <p>
222                      Calculates centroid
223                    </p>
224                  </td>
225</tr>
226<tr>
227<td>
228                    <p>
229                      Empty (e.g. polygon without points)
230                    </p>
231                  </td>
232<td>
233                    <p>
234                      Throws a <a class="link" href="../../exceptions/centroid_exception.html" title="centroid_exception">centroid_exception</a>
235                    </p>
236                  </td>
237</tr>
238<tr>
239<td>
240                    <p>
241                      Cartesian
242                    </p>
243                  </td>
244<td>
245                    <p>
246                      Implemented
247                    </p>
248                  </td>
249</tr>
250<tr>
251<td>
252                    <p>
253                      Spherical
254                    </p>
255                  </td>
256<td>
257                    <p>
258                      Calculates the centroid as if based on Cartesian coordinates
259                    </p>
260                  </td>
261</tr>
262</tbody>
263</table></div>
264<h6>
265<a name="geometry.reference.algorithms.centroid.centroid_2.h6"></a>
266            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.supported_geometries"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.supported_geometries">Supported
267            geometries</a>
268          </h6>
269<div class="informaltable"><table class="table">
270<colgroup>
271<col>
272<col>
273<col>
274</colgroup>
275<thead><tr>
276<th>
277                  </th>
278<th>
279                    <p>
280                      2D
281                    </p>
282                  </th>
283<th>
284                    <p>
285                      3D
286                    </p>
287                  </th>
288</tr></thead>
289<tbody>
290<tr>
291<td>
292                    <p>
293                      Point
294                    </p>
295                  </td>
296<td>
297                    <p>
298                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
299                    </p>
300                  </td>
301<td>
302                    <p>
303                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
304                    </p>
305                  </td>
306</tr>
307<tr>
308<td>
309                    <p>
310                      Segment
311                    </p>
312                  </td>
313<td>
314                    <p>
315                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
316                    </p>
317                  </td>
318<td>
319                    <p>
320                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
321                    </p>
322                  </td>
323</tr>
324<tr>
325<td>
326                    <p>
327                      Box
328                    </p>
329                  </td>
330<td>
331                    <p>
332                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
333                    </p>
334                  </td>
335<td>
336                    <p>
337                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
338                    </p>
339                  </td>
340</tr>
341<tr>
342<td>
343                    <p>
344                      Linestring
345                    </p>
346                  </td>
347<td>
348                    <p>
349                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
350                    </p>
351                  </td>
352<td>
353                    <p>
354                      <span class="inlinemediaobject"><img src="../../../../img/nyi.png" alt="nyi"></span>
355                    </p>
356                  </td>
357</tr>
358<tr>
359<td>
360                    <p>
361                      Ring
362                    </p>
363                  </td>
364<td>
365                    <p>
366                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
367                    </p>
368                  </td>
369<td>
370                    <p>
371                      <span class="inlinemediaobject"><img src="../../../../img/nyi.png" alt="nyi"></span>
372                    </p>
373                  </td>
374</tr>
375<tr>
376<td>
377                    <p>
378                      Polygon
379                    </p>
380                  </td>
381<td>
382                    <p>
383                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
384                    </p>
385                  </td>
386<td>
387                    <p>
388                      <span class="inlinemediaobject"><img src="../../../../img/nyi.png" alt="nyi"></span>
389                    </p>
390                  </td>
391</tr>
392<tr>
393<td>
394                    <p>
395                      MultiPoint
396                    </p>
397                  </td>
398<td>
399                    <p>
400                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
401                    </p>
402                  </td>
403<td>
404                    <p>
405                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
406                    </p>
407                  </td>
408</tr>
409<tr>
410<td>
411                    <p>
412                      MultiLinestring
413                    </p>
414                  </td>
415<td>
416                    <p>
417                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
418                    </p>
419                  </td>
420<td>
421                    <p>
422                      <span class="inlinemediaobject"><img src="../../../../img/nyi.png" alt="nyi"></span>
423                    </p>
424                  </td>
425</tr>
426<tr>
427<td>
428                    <p>
429                      MultiPolygon
430                    </p>
431                  </td>
432<td>
433                    <p>
434                      <span class="inlinemediaobject"><img src="../../../../img/ok.png" alt="ok"></span>
435                    </p>
436                  </td>
437<td>
438                    <p>
439                      <span class="inlinemediaobject"><img src="../../../../img/nyi.png" alt="nyi"></span>
440                    </p>
441                  </td>
442</tr>
443</tbody>
444</table></div>
445<h6>
446<a name="geometry.reference.algorithms.centroid.centroid_2.h7"></a>
447            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.complexity"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.complexity">Complexity</a>
448          </h6>
449<p>
450            Linear
451          </p>
452<h6>
453<a name="geometry.reference.algorithms.centroid.centroid_2.h8"></a>
454            <span class="phrase"><a name="geometry.reference.algorithms.centroid.centroid_2.example"></a></span><a class="link" href="centroid_2.html#geometry.reference.algorithms.centroid.centroid_2.example">Example</a>
455          </h6>
456<p>
457            Shows calculation of a centroid of a polygon
458          </p>
459<p>
460</p>
461<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
462<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">list</span><span class="special">&gt;</span>
463
464<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
465<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
466<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
467
468
469<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
470<span class="special">{</span>
471    <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">d2</span><span class="special">::</span><span class="identifier">point_xy</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">point_type</span><span class="special">;</span>
472    <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">&lt;</span><span class="identifier">point_type</span><span class="special">&gt;</span> <span class="identifier">polygon_type</span><span class="special">;</span>
473
474    <span class="identifier">polygon_type</span> <span class="identifier">poly</span><span class="special">;</span>
475    <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>
476        <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>
477            <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">poly</span><span class="special">);</span>
478
479    <span class="identifier">point_type</span> <span class="identifier">p</span><span class="special">;</span>
480    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">centroid</span><span class="special">(</span><span class="identifier">poly</span><span class="special">,</span> <span class="identifier">p</span><span class="special">);</span>
481
482    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"centroid: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">dsv</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
483
484    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
485<span class="special">}</span>
486</pre>
487<p>
488          </p>
489<p>
490            Output:
491          </p>
492<pre class="programlisting">centroid: (4.04663, 1.6349)
493<img src="../../../../img/algorithms/centroid.png" alt="centroid">
494</pre>
495<p>
496            Note that the centroid might be located in a hole or outside a polygon,
497            easily.
498          </p>
499</div>
500<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
501<td align="left"></td>
502<td align="right"><div class="copyright-footer">Copyright © 2009-2019 Barend Gehrels, Bruno Lalande, Mateusz Loskot, Adam
503      Wulkiewicz, Oracle and/or its affiliates<p>
504        Distributed under the Boost Software License, Version 1.0. (See accompanying
505        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>)
506      </p>
507</div></td>
508</tr></table>
509<hr>
510<div class="spirit-nav">
511<a accesskey="p" href="centroid_3_with_strategy.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../centroid.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="return_centroid_1.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
512</div>
513</body>
514</html>
515