Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method for calculating the centroid of a polygon #30222

Closed
wants to merge 1 commit into from
Closed

Add a method for calculating the centroid of a polygon #30222

wants to merge 1 commit into from

Conversation

Xrayez
Copy link
Contributor

@Xrayez Xrayez commented Jul 1, 2019

Required for #29867 to work for calculating the exact center of mass of polygon shapes.

polygon_centroid

C++:

Vector2 centroid = Geometry::get_polygon_centroid(polygon)

GDScript:

var centroid = Geometry.get_polygon_centroid(polygon)

@raphael10241024 please take a look at this. 🙂

@Xrayez Xrayez requested a review from a team as a code owner July 1, 2019 13:48
@akien-mga akien-mga added this to the 3.2 milestone Jul 1, 2019
@Raphael2048
Copy link
Contributor

That's great, how about adding another function for calculating polygon area?

@Xrayez
Copy link
Contributor Author

Xrayez commented Jul 2, 2019

@raphael10241024 there are a few places where area can be computed for polygons within engine:

This one can already be used within the engine now, but not from script (currently used by CSG module):

real_t Triangulate::get_area(const Vector<Vector2> &contour) {
int n = contour.size();
const Vector2 *c = &contour[0];
real_t A = 0.0;
for (int p = n - 1, q = 0; q < n; p = q++) {
A += c[p].cross(c[q]);
}
return A * 0.5;
}

Usage:

#include "core/math/triangulate.h"
/* ... */
real_t area = Triangulate::get_area(polygon)

This one needs to compute polygon's area and decide whether it's clockwise or not based on computed signed area.

godot/core/math/geometry.h

Lines 891 to 904 in 24e9a88

static bool is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
int c = p_polygon.size();
if (c < 3)
return false;
const Vector2 *p = p_polygon.ptr();
real_t sum = 0;
for (int i = 0; i < c; i++) {
const Vector2 &v1 = p[i];
const Vector2 &v2 = p[(i + 1) % c];
sum += (v2.x - v1.x) * (v2.y + v1.y);
}
return sum > 0.0f;
}

So now I wonder that it might be a good idea to make a dedicated function for it in Geometry as the functionality is already present but just used indirectly.

@akien-mga akien-mga modified the milestones: 3.2, 4.0 Nov 7, 2019
@Xrayez
Copy link
Contributor Author

Xrayez commented May 27, 2020

This is still needed by #29867, but given that #29867 already uses the implementation from this PR, and given there's no particular reason why this should be part of the core, I'll close this for now unless people explicitly express their interest/provide a real-life use case for this.

The method is in fact already implemented in GeometryTools module. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants