Shoelace formula for polygonal area: Difference between revisions
Shoelace formula for polygonal area (view source)
Revision as of 15:23, 29 October 2018
, 5 years ago→{{header|C#|C sharp}}
SqrtNegInf (talk | contribs) (Added Perl example) |
|||
Line 115:
The polygon area is 30.000000 square units.
</pre>
=={{header|C++}}==
{{trans|D}}
<lang cpp>#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
double shoelace(vector<pair<double, double>> points) {
double leftSum = 0.0;
double rightSum = 0.0;
for (int i = 0; i < points.size(); ++i) {
int j = (i + 1) % points.size();
leftSum += points[i].first * points[j].second;
rightSum += points[j].first * points[i].second;
}
return 0.5 * abs(leftSum - rightSum);
}
void main() {
vector<pair<double, double>> points = {
make_pair( 3, 4),
make_pair( 5, 11),
make_pair(12, 8),
make_pair( 9, 5),
make_pair( 5, 6),
};
auto ans = shoelace(points);
cout << ans << endl;
}</lang>
{{out}}
<pre>30</pre>
=={{header|C#|C sharp}}==
|