Just starting out? Need help? Post your questions and find answers here.
-
ehowington
- Enthusiast
- Posts: 114
- Joined: Sat Sep 12, 2009 3:06 pm
Post
by ehowington »
Wondering if someone might provide some thoughts on converting this bit of code in c++ and the next and prev in statement kinda fubars me a bit
Code: Select all
#include <iostream>
using namespace std;
struct Point2D
{
double x;
double y;
};
Point2D compute2DPolygonCentroid(const Point2D* vertices, int vertexCount)
{
Point2D centroid = {0, 0};
double signedArea = 0.0;
double x0 = 0.0; // Current vertex X
double y0 = 0.0; // Current vertex Y
double x1 = 0.0; // Next vertex X
double y1 = 0.0; // Next vertex Y
double a = 0.0; // Partial signed area
int lastdex = vertexCount-1;
const Point2D* prev = &(vertices[lastdex]);
const Point2D* next;
// For all vertices in a loop
for (int i=0; i<vertexCount; ++i)
{
next = &(vertices[i]);
x0 = prev->x;
y0 = prev->y;
x1 = next->x;
y1 = next->y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
prev = next;
}
signedArea *= 0.5;
centroid.x /= (6.0*signedArea);
centroid.y /= (6.0*signedArea);
return centroid;
}
int main()
{
Point2D polygon[] = {{0.0,0.0}, {0.0,10.0}, {10.0,10.0}, {10.0,0.0}};
size_t vertexCount = sizeof(polygon) / sizeof(polygon[0]);
Point2D centroid = compute2DPolygonCentroid(polygon, vertexCount);
std::cout << "Centroid is (" << centroid.x << ", " << centroid.y << ")\n";
}
-
ehowington
- Enthusiast
- Posts: 114
- Joined: Sat Sep 12, 2009 3:06 pm
Post
by ehowington »
Please note this is based off of Emile Cormier's algorithm
-
ehowington
- Enthusiast
- Posts: 114
- Joined: Sat Sep 12, 2009 3:06 pm
Post
by ehowington »
Far as I have gotten with this bit of code
Code: Select all
Structure Point2D
x.d;
y.d;
EndStructure
Point2D compute2DPolygonCentroid(const Point2D* vertices,vertexCount.i)
Point2D = 0
centroid = 0
signedArea.d = 0.0;
x0.d = 0.0; // Current vertex X
y0.d = 0.0; // Current vertex Y
x1.d = 0.0; // Next vertex X
y1.d = 0.0; // Next vertex Y
a.d = 0.0; // Partial signed area
lastdex.i = vertexCount -1;
;const Point2D*
prev = &(vertices[lastdex]);
;const Point2D* Next;
// For all vertices in a loop
For i.i=0 To vertexCount Step 1
x0 = prev->x;
y0 = prev->y;
x1 = Next->x;
y1 = Next->y;
a = x0*y1 - x1*y0;
signedArea = a + a;
centroid.x = (x0 + x1)*a + (x0 + x1)*a ;
centroid.y = (y0 + y1)*a + (x0 + x1)*a;
;prev = Next;
Next
signedArea = 0.5 * 0.5;
centroid.x = (6.0*signedArea)/(6.0*signedArea);
centroid.y = (6.0*signedArea)/(6.0*signedArea);
Return centroid;
}
int main()
{
Point2D polygon[] = {{0.0,0.0}, {0.0,10.0}, {10.0,10.0}, {10.0,0.0}};
size_t vertexCount = SizeOf(polygon) / SizeOf(polygon[0]);
Point2D centroid = compute2DPolygonCentroid(polygon, vertexCount);
std::cout << "Centroid is (" << centroid.x << ", " << centroid.y << ")\n";
}
-
wilbert
- PureBasic Expert
- Posts: 3870
- Joined: Sun Aug 08, 2004 5:21 am
- Location: Netherlands
Post
by wilbert »
Something like this ?
Code: Select all
Structure Point2D
x.d
y.d
EndStructure
Procedure compute2DPolygonCentroid(Array vertices.Point2D(1), *centroid.Point2D)
Protected.Point2D centroid, *prev, *next
Protected.d signedArea, x0, y0, x1, y1, a
Protected.i i, lastdex
lastdex = ArraySize(vertices())
*prev = @vertices(lastdex)
For i = 0 To lastdex
*next = @vertices(i)
x0 = *prev\x
y0 = *prev\y
x1 = *next\x
y1 = *next\y
a = x0*y1 - x1*y0
signedArea + a
centroid\x + (x0 + x1)*a
centroid\y + (y0 + y1)*a
*prev = *next
Next
signedArea * 0.5
*centroid\x = centroid\x / (6.0*signedArea)
*centroid\y = centroid\y / (6.0*signedArea)
EndProcedure
Dim polygon.Point2D(3)
polygon(0)\x = 0
polygon(0)\y = 0
polygon(1)\x = 0
polygon(1)\y = 10
polygon(2)\x = 10
polygon(2)\y = 10
polygon(3)\x = 10
polygon(3)\y = 0
compute2DPolygonCentroid(polygon(), @centroid.Point2D)
Debug "Centroid is (" + centroid\x + ", " + centroid\y + ")"
Windows (x64)
Raspberry Pi OS (Arm64)
-
ehowington
- Enthusiast
- Posts: 114
- Joined: Sat Sep 12, 2009 3:06 pm
Post
by ehowington »
hmmm not exactly
Code: Select all
Structure Point2D
x.d
y.d
EndStructure
Procedure compute2DPolygonCentroid(Array vertices.Point2D(1), *centroid.Point2D)
Protected.Point2D centroid, *prev, *next
Protected.d signedArea, x0, y0, x1, y1, a
Protected.i i, lastdex
lastdex = ArraySize(vertices())
*prev = @vertices(lastdex)
For i = 0 To lastdex
*next = @vertices(i)
x0 = *prev\x
y0 = *prev\y
x1 = *next\x
y1 = *next\y
a = x0*y1 - x1*y0
signedArea + a
centroid\x + (x0 + x1)*a
centroid\y + (y0 + y1)*a
*prev = *next
Next
signedArea * 0.5
*centroid\x = centroid\x / (6.0*signedArea)
*centroid\y = centroid\y / (6.0*signedArea)
EndProcedure
Dim polygon.Point2D(3)
polygon(0)\x = 0
polygon(0)\y = 0
polygon(1)\x = 20
polygon(1)\y = 0
polygon(2)\x = 20
polygon(2)\y = 20
polygon(3)\x = 10
polygon(3)\y = 30
polygon(4)\x = 0
polygon(4)\y = 20
compute2DPolygonCentroid(polygon(), @centroid.Point2D)
Debug "Centroid is (" + centroid\x + ", " + centroid\y + ")"
-
ehowington
- Enthusiast
- Posts: 114
- Joined: Sat Sep 12, 2009 3:06 pm
Post
by ehowington »
also even closing of polygon
Code: Select all
Structure Point2D
x.d
y.d
EndStructure
Procedure compute2DPolygonCentroid(Array vertices.Point2D(1), *centroid.Point2D)
Protected.Point2D centroid, *prev, *next
Protected.d signedArea, x0, y0, x1, y1, a
Protected.i i, lastdex
lastdex = ArraySize(vertices())
*prev = @vertices(lastdex)
For i = 0 To lastdex
*next = @vertices(i)
x0 = *prev\x
y0 = *prev\y
x1 = *next\x
y1 = *next\y
a = x0*y1 - x1*y0
signedArea + a
centroid\x + (x0 + x1)*a
centroid\y + (y0 + y1)*a
*prev = *next
Next
signedArea * 0.5
*centroid\x = centroid\x / (6.0*signedArea)
*centroid\y = centroid\y / (6.0*signedArea)
EndProcedure
Dim polygon.Point2D(3)
polygon(0)\x = 0
polygon(0)\y = 0
polygon(1)\x = 20
polygon(1)\y = 0
polygon(2)\x = 20
polygon(2)\y = 20
polygon(3)\x = 0
polygon(3)\y = 20
polygon(4)\x = 0
polygon(4)\y = 0
compute2DPolygonCentroid(polygon(), @centroid.Point2D)
Debug "Centroid is (" + centroid\x + ", " + centroid\y + ")"
-
wilbert
- PureBasic Expert
- Posts: 3870
- Joined: Sun Aug 08, 2004 5:21 am
- Location: Netherlands
Post
by wilbert »
You added a new point to the array but didn't change the dim statement to
Dim polygon.Point2D(4)
Windows (x64)
Raspberry Pi OS (Arm64)
-
ehowington
- Enthusiast
- Posts: 114
- Joined: Sat Sep 12, 2009 3:06 pm
Post
by ehowington »
ah woops lol I see it now
-
ehowington
- Enthusiast
- Posts: 114
- Joined: Sat Sep 12, 2009 3:06 pm
Post
by ehowington »
Many thanks
Code: Select all
Structure Point2D
x.d
y.d
EndStructure
Procedure compute2DPolygonCentroid(Array vertices.Point2D(1), *centroid.Point2D)
Protected.Point2D centroid, *prev, *next
Protected.d signedArea, x0, y0, x1, y1, a
Protected.i i, lastdex
lastdex = ArraySize(vertices())
*prev = @vertices(lastdex)
For i = 0 To lastdex
*next = @vertices(i)
x0 = *prev\x
y0 = *prev\y
x1 = *next\x
y1 = *next\y
a = x0*y1 - x1*y0
signedArea + a
centroid\x + (x0 + x1)*a
centroid\y + (y0 + y1)*a
*prev = *next
Next
signedArea * 0.5
*centroid\x = centroid\x / (6.0*signedArea)
*centroid\y = centroid\y / (6.0*signedArea)
EndProcedure
Dim polygon.Point2D(5)
polygon(0)\x = 0
polygon(0)\y = 0
polygon(1)\x = 20
polygon(1)\y = 0
polygon(2)\x = 20
polygon(2)\y = 20
polygon(3)\x = 10
polygon(3)\y = 100
polygon(4)\x = 0
polygon(4)\y = 20
polygon(5)\x = 0
polygon(5)\y = 0
compute2DPolygonCentroid(polygon(), @centroid.Point2D)
Debug "Centroid is (" + centroid\x + ", " + centroid\y + ")"