Made nested Bounds structure to encapsulate work with bounds
Looks a bit nicer, probably increases padding but whatever.
This commit is contained in:
47
src/main.cpp
47
src/main.cpp
@@ -45,46 +45,57 @@ struct State
|
|||||||
NodeAllocator allocator;
|
NodeAllocator allocator;
|
||||||
std::queue<word_t> iteration_queue;
|
std::queue<word_t> iteration_queue;
|
||||||
word_t root;
|
word_t root;
|
||||||
|
|
||||||
|
struct Bounds
|
||||||
|
{
|
||||||
Node leftmost, rightmost;
|
Node leftmost, rightmost;
|
||||||
long double lower_bound, upper_bound;
|
long double lower, upper;
|
||||||
|
} bounds;
|
||||||
|
|
||||||
State(const Fraction start) : allocator{256}
|
State(const Fraction start) : allocator{256}
|
||||||
{
|
{
|
||||||
root = allocator.alloc(start);
|
root = allocator.alloc(start);
|
||||||
iteration_queue.push(root);
|
iteration_queue.push(root);
|
||||||
leftmost = allocator.getVal(root);
|
bounds.leftmost = allocator.getVal(root);
|
||||||
rightmost = allocator.getVal(root);
|
bounds.rightmost = allocator.getVal(root);
|
||||||
|
compute_bounds();
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_iteration(void)
|
||||||
|
{
|
||||||
|
iterate(iteration_queue, allocator);
|
||||||
|
compute_bound_nodes();
|
||||||
compute_bounds();
|
compute_bounds();
|
||||||
}
|
}
|
||||||
|
|
||||||
void compute_bounds()
|
void compute_bounds()
|
||||||
{
|
{
|
||||||
lower_bound = std::floorl(leftmost.value.norm);
|
bounds.lower = std::floorl(bounds.leftmost.value.norm);
|
||||||
upper_bound = std::ceill(rightmost.value.norm);
|
bounds.upper = std::ceill(bounds.rightmost.value.norm);
|
||||||
}
|
}
|
||||||
|
|
||||||
void compute_bound_nodes()
|
void compute_bound_nodes()
|
||||||
{
|
{
|
||||||
leftmost = allocator.getVal(0);
|
bounds.leftmost = allocator.getVal(0);
|
||||||
while (leftmost.left.has_value())
|
while (bounds.leftmost.left.has_value())
|
||||||
leftmost = allocator.getVal(leftmost.left.value());
|
bounds.leftmost = allocator.getVal(bounds.leftmost.left.value());
|
||||||
|
|
||||||
rightmost = allocator.getVal(0);
|
bounds.rightmost = allocator.getVal(0);
|
||||||
while (rightmost.right.has_value())
|
while (bounds.rightmost.right.has_value())
|
||||||
rightmost = allocator.getVal(rightmost.right.value());
|
bounds.rightmost = allocator.getVal(bounds.rightmost.right.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr word_t clamp_to_width(long double 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()
|
void draw_bounds()
|
||||||
{
|
{
|
||||||
word_t lower_x = clamp_to_width(leftmost.value.norm);
|
word_t lower_x = clamp_to_width(bounds.leftmost.value.norm);
|
||||||
word_t upper_x = clamp_to_width(rightmost.value.norm);
|
word_t upper_x = clamp_to_width(bounds.rightmost.value.norm);
|
||||||
draw_fraction(leftmost.value, lower_x, 3 * HEIGHT / 8);
|
draw_fraction(bounds.leftmost.value, lower_x, 3 * HEIGHT / 8);
|
||||||
draw_fraction(rightmost.value, upper_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(lower_x, LINE_TOP, lower_x, LINE_BOTTOM, WHITE);
|
||||||
DrawLine(upper_x, LINE_TOP, upper_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))
|
if (IsKeyPressed(KEY_SPACE))
|
||||||
{
|
{
|
||||||
iterate(state.iteration_queue, state.allocator);
|
state.do_iteration();
|
||||||
state.compute_bound_nodes();
|
|
||||||
state.compute_bounds();
|
|
||||||
count += 2;
|
count += 2;
|
||||||
}
|
}
|
||||||
if (prev_count != count)
|
if (prev_count != count)
|
||||||
|
|||||||
Reference in New Issue
Block a user