From 01468f793fb0566caab9daf0a6a39532f7808b8c Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 26 Jul 2024 20:46:39 +0100 Subject: recursive function to draw tree in a number line Draws the current node by converting its norm value (which is in (lower, upper)) to a screen value, drawing a line there. It then recurs on the children of the node. --- src/main.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index aae5b72..cec3b5e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,6 +63,19 @@ void draw_fraction(Fraction f, word_t x, word_t y) DrawText(s.c_str(), x - width / 2, y - FONT_SIZE, FONT_SIZE, WHITE); } +void draw_node_number_line(index_t index, const NodeAllocator &allocator, + long double lower, long double upper) +{ + if (index.has_value()) + { + Node n = allocator.getVal(index.value()); + word_t x = clamp_to_width(n.value.norm, lower, upper); + DrawLine(x, LINE_TOP, x, LINE_BOTTOM, index.value() == 0 ? GREEN : RED); + draw_node_number_line(n.left, allocator, lower, upper); + draw_node_number_line(n.right, allocator, lower, upper); + } +} + int main(void) { // NodeAllocator allocator{256}; -- cgit v1.2.3-13-gbd6f