Added general polygon generation WITH CORRECT INERTIA YUHUU

This commit is contained in:
Karma Riuk 2023-05-06 14:04:58 +02:00
parent ad841588ad
commit c0d4343b77
3 changed files with 52 additions and 7 deletions

View File

@ -66,14 +66,55 @@ polygon poly_generate::regular(double radius, uint n_sides, double mass) {
return polygon{{0, 0}, 0, points, inertia, mass}; return polygon{{0, 0}, 0, points, inertia, mass};
} }
static double intertia_of_polygon_subtriangle( static double area_of_triangle(vec2d& a, vec2d& b, vec2d& c) {
vec2d& centroid, vec2d& p1, vec2d& p2) { return std::abs(vec2d::cross(c - a, b - a)) / 2;
double base, height;
if (vec2d::norm(p1 - centroid) > vec2d::norm(p2 - centroid))
base = vec2d::norm(p1 - centroid);
} }
polygon poly_generate::general(std::vector<vec2d>& points, double mass) { static double area_of_poly(std::vector<vec2d>& points, vec2d& centroid) {
double area = 0;
for (int i = 0; i < points.size(); ++i)
area += area_of_triangle(
centroid, points[i], points[(i + 1) % points.size()]);
return area;
}
static double intertia_of_polygon_subtriangle(double total_mass,
double total_area,
vec2d& centroid,
vec2d& p1,
vec2d& p2) {
double partial_area = area_of_triangle(centroid, p1, p2);
std::cout << "partial area: " << partial_area << std::endl;
double partial_mass = total_mass * partial_area / total_area;
vec2d CA = p1 - centroid;
vec2d AB = p2 - p1;
return partial_mass / 2 *
(vec2d::norm2(AB) / 3 + vec2d::dot(AB, CA) + vec2d::norm2(CA));
}
static vec2d centroid(std::vector<vec2d>& points) {
double x = 0, y = 0;
for (auto& p : points)
x += p.x, y += p.y;
return vec2d{x, y} / points.size();
}
polygon poly_generate::general(std::vector<vec2d> points, double mass) {
double intertia = 0; double intertia = 0;
vec2d c = centroid(points);
double area = area_of_poly(points, c);
std::cout << "area: " << area << std::endl;
std::cout << "centroid: " << c << std::endl;
for (int i = 0; i < points.size(); ++i)
intertia += intertia_of_polygon_subtriangle(
mass, area, c, points[i], points[(i + 1) % points.size()]);
for (auto& p : points) // set the center of the polygon to it's centroid
p -= c;
return polygon{{0, 0}, 0, points, intertia, mass}; return polygon{{0, 0}, 0, points, intertia, mass};
} }

View File

@ -18,7 +18,7 @@ namespace poly_generate {
polygon regular(double radius, uint n_sides, double mass = 1); polygon regular(double radius, uint n_sides, double mass = 1);
polygon general(std::vector<vec2d>& points, double mass = 1); polygon general(std::vector<vec2d> points, double mass = 1);
}; // namespace poly_generate }; // namespace poly_generate
#endif #endif

View File

@ -116,6 +116,10 @@ class vec2d {
return {-omega * v.y, omega * v.x}; return {-omega * v.y, omega * v.x};
} }
static double norm2(const vec2d& v) {
return vec2d::dot(v, v);
}
static double norm(const vec2d& v) { static double norm(const vec2d& v) {
return std::sqrt(dot(v, v)); return std::sqrt(dot(v, v));
} }