Vertically center triangle

This commit is contained in:
2026-08-07 00:29:53 -04:00
parent 2458680ebe
commit 9c81f1a640
+10 -11
View File
@@ -28,24 +28,23 @@ static void triangle_layer_update_proc(Layer *layer, GContext *ctx) {
left_angle = tri->angle_A; // at vertex A
}
// draw the base, centered horizontally
// TODO center whole triangle vertically
uint16_t padded_base = (base_side % 2 == 0) ? base_side : base_side + 1;
uint16_t margin = (bounds.size.w / 2) - (padded_base / 2);
uint16_t start_x = margin + 1;
uint16_t end_x = margin + base_side;
uint16_t base_y = bounds.size.h - 5; // small bottom margin
graphics_draw_line(ctx, GPoint(start_x, base_y), GPoint(end_x, base_y));
// determine height and apex coordinates
// calculate height and horizontal apex offset first
int32_t sin_left = sin_lookup(((int32_t)left_angle * TRIG_MAX_ANGLE) / 360);
int32_t cos_left = cos_lookup(((int32_t)left_angle * TRIG_MAX_ANGLE) / 360);
uint16_t height = (uint16_t)(((uint32_t)left_side * (uint32_t)sin_left) / TRIG_MAX_RATIO);
uint16_t apex_offset = (uint16_t)(((uint32_t)left_side * (uint32_t)cos_left) / TRIG_MAX_RATIO);
// center the triangle both horizontally and vertically
uint16_t padded_base = (base_side % 2 == 0) ? base_side : base_side + 1;
uint16_t margin = (bounds.size.w / 2) - (padded_base / 2);
uint16_t start_x = margin + 1;
uint16_t end_x = margin + base_side;
uint16_t base_y = (bounds.size.h + height) / 2;
uint16_t apex_x = start_x + apex_offset;
uint16_t apex_y = base_y - height;
// draw the remaining sides
// draw the three sides
graphics_draw_line(ctx, GPoint(start_x, base_y), GPoint(end_x, base_y));
graphics_draw_line(ctx, GPoint(start_x, base_y), GPoint(apex_x, apex_y));
graphics_draw_line(ctx, GPoint(end_x, base_y), GPoint(apex_x, apex_y));
}