From 9c81f1a6402b0e02cd531ef1637768873e4bd048 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 7 Aug 2026 00:29:53 -0400 Subject: [PATCH] Vertically center triangle --- src/c/triangles.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/c/triangles.c b/src/c/triangles.c index bb14da4..8322e03 100644 --- a/src/c/triangles.c +++ b/src/c/triangles.c @@ -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)); }