I want to use this function to draw triangle fan polygons like hexagons, octogons,... But it seems its buggy. The filled shapes are messed up. Can you fix this ? _polygon(float x, float y, float rx, float ry, int segments, bool filled) {
sgp_push_transform();
sgp_translate(x, y);
float step = (2.0fPI)/(float) segments;
float hw = 0;
float hh = 0;
unsigned int count = 0;
static sgp_vec2 points_buffer[64];
for (float theta = 0.0f; theta <= 2.0fPI + step0.5f; theta+=step) {
sgp_vec2 v = {hw + rxcosf(theta), hh - ry*sinf(theta)};
points_buffer[count++] = v;
if (filled && (count == 2)) {
sgp_vec2 u = {hw, hh};
points_buffer[count++] = u;
}
}
if (filled) sgp_draw_filled_triangles_strip(points_buffer, count);
else sgp_draw_lines_strip(points_buffer, count);
sgp_pop_transform();
}
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You're on the right track with your _polygon function for drawing regular polygons using a triangle fan approach. However, there are a few issues in your code that are causing the filled shapes to appear "messed up":
🔍 Issues:
Incorrect triangle strip logic: You're using sgp_draw_filled_triangles_strip, but it's not suitable for a triangle fan pattern.
Point generation logic is off for triangle fans:
For a triangle fan, you need to specify the center point once, and then alternate between two perimeter points each time.
Loop condition: The loop condition theta <= 2.0f*PI + step*0.5f can cause an extra point to be added unnecessarily.
Misuse of hw and hh: They’re always zero and seem redundant unless you're planning to add more features later.
✅ Fix: Triangle Fan Logic
To draw a polygon like a hexagon or octagon as a filled shape, we should use a triangle fan:
Start at the center (0, 0)
Then go around the circle, placing vertices on the perimeter
Each pair of adjacent perimeter vertices forms a triangle with the center
✅ Fixed Version of Your Function:
c
_polygon(float x, float y, float rx, float ry, int segments, bool filled) {
sgp_push_transform();
sgp_translate(x, y);
float step = (2.0f * PI) / (float)segments;
unsigned int count = 0;
static sgp_vec2 points_buffer[64];
// Always start with the center point if filled (triangle fan)
if (filled) {
points_buffer[count++] = (sgp_vec2){0, 0};
}
// Generate points around the circle
for (int i = 0; i <= segments; ++i) {
float theta = i * step;
float px = rx * cosf(theta);
float py = ry * sinf(theta); // Use positive since Y-axis might be flipped
points_buffer[count++] = (sgp_vec2){px, -py}; // flip Y if necessary
}
if (filled) {
// Draw as triangle fan: center + perimeter points
sgp_draw_filled_triangles_fan(points_buffer, count);
} else {
// Draw as line strip (close the shape manually if needed)
sgp_draw_lines_strip(points_buffer, count);
}
sgp_pop_transform();
}
📝 Notes:
Make sure you have a function sgp_draw_filled_triangles_fan(sgp_vec2* points, int count) implemented — this is ideal for drawing convex polygons from a center point.
If you don't have a triangle fan function, you can simulate it by sending triangles manually in a loop.
The Y-coordinate is negated (-py) assuming that the coordinate system has Y increasing downward (common in many graphics APIs).
points_buffer is limited to 64 elements — make sure you don’t exceed that for large segment counts.
🧪 Example Usage:
c
// Draw a hexagon at (100, 100), radius 40, 6 sides, filled
_polygon(100, 100, 40, 40, 6, true);
// Draw an octagon outline at (200, 200), radius 50, 8 sides
_polygon(200, 200, 50, 50, 8, false);
Let me know if you need help implementing sgp_draw_filled_triangles_fan() or converting this to use indexed triangles!