diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-07-26 21:58:53 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-07-26 21:58:53 +0100 |
commit | 32314708d564c5e7373f6f5f543c4768f856931c (patch) | |
tree | 1eb0858ffba4f48e99f2aa788a1234115bad5bb1 | |
parent | bb6ded706f87abe5d5f92404b0145ac95de76a11 (diff) | |
download | cw_tree-32314708d564c5e7373f6f5f543c4768f856931c.tar.gz cw_tree-32314708d564c5e7373f6f5f543c4768f856931c.tar.bz2 cw_tree-32314708d564c5e7373f6f5f543c4768f856931c.zip |
Made nested Bounds structure to encapsulate work with bounds
Looks a bit nicer, probably increases padding but whatever.
-rw-r--r-- | src/main.cpp | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/src/main.cpp b/src/main.cpp index 1718090..83dd289 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,46 +45,57 @@ struct State NodeAllocator allocator; std::queue<word_t> iteration_queue; word_t root; - Node leftmost, rightmost; - long double lower_bound, upper_bound; + + struct Bounds + { + Node leftmost, rightmost; + long double lower, upper; + } bounds; State(const Fraction start) : allocator{256} { root = allocator.alloc(start); iteration_queue.push(root); - leftmost = allocator.getVal(root); - rightmost = allocator.getVal(root); + bounds.leftmost = allocator.getVal(root); + bounds.rightmost = allocator.getVal(root); + compute_bounds(); + } + + void do_iteration(void) + { + iterate(iteration_queue, allocator); + compute_bound_nodes(); compute_bounds(); } void compute_bounds() { - lower_bound = std::floorl(leftmost.value.norm); - upper_bound = std::ceill(rightmost.value.norm); + bounds.lower = std::floorl(bounds.leftmost.value.norm); + bounds.upper = std::ceill(bounds.rightmost.value.norm); } void compute_bound_nodes() { - leftmost = allocator.getVal(0); - while (leftmost.left.has_value()) - leftmost = allocator.getVal(leftmost.left.value()); + bounds.leftmost = allocator.getVal(0); + while (bounds.leftmost.left.has_value()) + bounds.leftmost = allocator.getVal(bounds.leftmost.left.value()); - rightmost = allocator.getVal(0); - while (rightmost.right.has_value()) - rightmost = allocator.getVal(rightmost.right.value()); + bounds.rightmost = allocator.getVal(0); + while (bounds.rightmost.right.has_value()) + bounds.rightmost = allocator.getVal(bounds.rightmost.right.value()); } constexpr word_t clamp_to_width(long double value) { - return WIDTH / (upper_bound - lower_bound) * (value - lower_bound); + return WIDTH / (bounds.upper - bounds.lower) * (value - bounds.lower); } void draw_bounds() { - word_t lower_x = clamp_to_width(leftmost.value.norm); - word_t upper_x = clamp_to_width(rightmost.value.norm); - draw_fraction(leftmost.value, lower_x, 3 * HEIGHT / 8); - draw_fraction(rightmost.value, upper_x, 3 * HEIGHT / 8); + word_t lower_x = clamp_to_width(bounds.leftmost.value.norm); + word_t upper_x = clamp_to_width(bounds.rightmost.value.norm); + draw_fraction(bounds.leftmost.value, lower_x, 3 * HEIGHT / 8); + draw_fraction(bounds.rightmost.value, upper_x, 3 * HEIGHT / 8); DrawLine(lower_x, LINE_TOP, lower_x, LINE_BOTTOM, WHITE); DrawLine(upper_x, LINE_TOP, upper_x, LINE_BOTTOM, WHITE); } @@ -123,9 +134,7 @@ int main(void) { if (IsKeyPressed(KEY_SPACE)) { - iterate(state.iteration_queue, state.allocator); - state.compute_bound_nodes(); - state.compute_bounds(); + state.do_iteration(); count += 2; } if (prev_count != count) |