feat: added shape_get_min_extent and shape_get_max_extent
This commit is contained in:
parent
b2f6f9c176
commit
7e9dc4003b
|
@ -11,6 +11,7 @@ struct Shape {
|
|||
|
||||
Vector mean;
|
||||
int is_convex;
|
||||
Vector min, max;
|
||||
};
|
||||
|
||||
static
|
||||
|
@ -40,7 +41,7 @@ int _shape_calculate_is_convex(Shape* self) {
|
|||
// point relative to mean
|
||||
Vector relative;
|
||||
list_foreach(Vector*, point, &self->points) {
|
||||
relative = vsubf(*point, self->mean);
|
||||
relative = vnormalizedf(vsubf(*point, self->mean));
|
||||
if(point != _shape_get_furthest_in_direction(self, relative)) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -183,3 +184,29 @@ void shape_draw(Shape* self, Transform transform) {
|
|||
SDL_RenderDrawLineF(g_renderer, lhs.x, lhs.y, rhs.x, rhs.y);
|
||||
}
|
||||
}
|
||||
|
||||
Vector shape_get_min_extent(Shape* self, Transform* transform) {
|
||||
if(self->points.len == 0)
|
||||
return ZeroVector;
|
||||
Vector min = *list_at_as(Vector, &self->points, 0);
|
||||
Vector point;
|
||||
for(size_t i = 0; i < shape_get_points_count(self); ++i) {
|
||||
point = shape_get_point_transformed(self, i, *transform);
|
||||
min.x = fminf(min.x, point.x);
|
||||
min.y = fminf(min.y, point.y);
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
Vector shape_get_max_extent(Shape* self, Transform* transform) {
|
||||
if(self->points.len == 0)
|
||||
return ZeroVector;
|
||||
Vector max = *list_at_as(Vector, &self->points, 0);
|
||||
Vector point;
|
||||
for(size_t i = 0; i < shape_get_points_count(self); ++i) {
|
||||
point = shape_get_point_transformed(self, i, *transform);
|
||||
max.x = fmaxf(max.x, point.x);
|
||||
max.y = fmaxf(max.y, point.y);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
|
|
@ -29,4 +29,7 @@ extern Vector shape_get_median_point(Shape* self);
|
|||
extern int shape_is_convex(Shape* self);
|
||||
extern void shape_draw(Shape* self, Transform transform);
|
||||
|
||||
extern Vector shape_get_min_extent(Shape* self, Transform* transform);
|
||||
extern Vector shape_get_max_extent(Shape* self, Transform* transform);
|
||||
|
||||
#endif // !_fencer_shape_h
|
||||
|
|
Loading…
Reference in a new issue