Skip to content

Commit acb4f72

Browse files
committed
Improved aspect ratio calculation
1 parent 64a489b commit acb4f72

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed
+24-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "aspect_ratio.hpp"
22

3+
#include "units/display_domain.hpp"
4+
35
namespace fractal {
46

5-
display_coordinate calculate_rectangle_end_point(
7+
DisplayDomain calculate_rectangle_end_points(
68
display_coordinate start, display_coordinate current, float target_aspect_ratio
79
)
810
{
@@ -19,18 +21,30 @@ display_coordinate calculate_rectangle_end_point(
1921
height = width / target_aspect_ratio;
2022
}
2123

22-
auto new_x = static_cast<float>(std::min(current.x, start.x));
23-
auto new_y = static_cast<float>(std::min(current.y, start.y));
24+
auto x_pos1 = static_cast<float>(start.x);
25+
auto y_pos1 = static_cast<float>(start.y);
26+
auto x_pos2 = x_pos1 + width;
27+
auto y_pos2 = y_pos1 + height;
28+
29+
bool flipped_horizontal = start.x > current.x;
30+
bool flipped_vertical = start.y > current.y;
2431

25-
// Adjust the top-left corner based on new dimensions
26-
if (current.x < start.x) {
27-
new_x = static_cast<float>(start.x) - width;
32+
if (flipped_vertical) {
33+
y_pos1 -= height;
34+
y_pos2 -= height;
2835
}
29-
if (current.y < start.y) {
30-
new_y = static_cast<float>(start.y) - height;
36+
if (flipped_horizontal) {
37+
x_pos1 -= width;
38+
x_pos2 -= width;
3139
}
3240

33-
// Return the top-left and bottom-right corners as a pair of sf::Vector2f
34-
return {new_x + width, new_y + height};
41+
display_coordinate top_left{
42+
static_cast<uint16_t>(x_pos1), static_cast<uint16_t>(y_pos1)
43+
};
44+
display_coordinate bottom_right{
45+
static_cast<uint16_t>(x_pos2), static_cast<uint16_t>(y_pos2)
46+
};
47+
48+
return {top_left, bottom_right};
3549
}
3650
} // namespace fractal

source/graphics/aspect_ratio/aspect_ratio.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#include "config.hpp"
44
#include "units/coordinates.hpp"
5+
#include "units/display_domain.hpp"
56

67
namespace fractal {
7-
display_coordinate calculate_rectangle_end_point(
8+
DisplayDomain calculate_rectangle_end_points(
89
display_coordinate start, display_coordinate current,
910
float target_aspect_ratio = static_cast<float>(WINDOW_WIDTH) / WINDOW_HEIGHT
1011
);

source/graphics/selection_window/selection_window.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ std::optional<std::unique_ptr<sf::Drawable>> SelectionWindow::get_drawable()
3333
{
3434
if (!left_mouse_down_)
3535
return std::nullopt;
36-
display_coordinate end_point = calculate_rectangle_end_point(
36+
DisplayDomain new_selection = calculate_rectangle_end_points(
3737
{selection_start_x_, selection_start_y_}, {current_mouse_x_, current_mouse_y_}
3838
);
3939

4040
auto rectangle = std::make_unique<sf::RectangleShape>(sf::Vector2f{
41-
static_cast<float>(end_point.x) - selection_start_x_,
42-
static_cast<float>(end_point.y) - selection_start_y_
41+
static_cast<float>(new_selection.width()),
42+
static_cast<float>(new_selection.height())
4343
});
44-
rectangle->setPosition(selection_start_x_, selection_start_y_);
44+
auto [x, y] = new_selection.get_start_coordinate();
45+
rectangle->setPosition(x, y);
4546
rectangle->setFillColor(sf::Color(255, 0, 0, 127));
4647
return rectangle;
4748
}

source/mandelbrot/window.hpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "graphics/display_event_observer.hpp"
66
#include "mandelbrot/mandelbrot_window.hpp"
77
#include "units/coordinates.hpp"
8+
#include "units/display_domain.hpp"
89
#include "units/units.hpp"
910

1011
#include <SFML/Graphics/Image.hpp>
@@ -56,16 +57,10 @@ class Window : public DisplayEventObserver {
5657

5758
void on_mouse_button_released(const sf::Event::MouseButtonEvent& event) override
5859
{
59-
auto ends = calculate_rectangle_end_point(
60+
DisplayDomain ends = calculate_rectangle_end_points(
6061
{selection_start_x_, selection_start_y_}, {event.x, event.y}
6162
);
62-
auto res = mandelbrot_.calculate_(
63-
DISPLAY_DOMAIN,
64-
{
65-
{selection_start_x_, selection_start_y_},
66-
ends
67-
}
68-
);
63+
auto res = mandelbrot_.calculate_(DISPLAY_DOMAIN, ends);
6964
for (display_coordinate pos : DISPLAY_DOMAIN) {
7065
set_pixel_color(pos, res[pos.x][pos.y]);
7166
}

source/units/coordinates.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ uint32_t
55
decay_2d_coordinate(const display_coordinate& coordinate, uint32_t display_width)
66
{
77
return static_cast<uint32_t>(coordinate.x)
8-
+ static_cast<uint32_t>(coordinate.y) * display_width;
8+
+ static_cast<uint32_t>(coordinate.y) * (display_width - 1u);
99
}
1010

1111
display_coordinate generate_1d_coordinate(uint32_t coordinate, uint32_t display_width)
1212
{
1313
return {
14-
coordinate % (display_width + 1),
15-
(coordinate - (coordinate % (display_width + 1))) / display_width
14+
coordinate % display_width,
15+
(coordinate - (coordinate % display_width)) / display_width
1616
};
1717
}
1818
} // namespace fractal

source/units/display_domain.hpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "coordinates.hpp"
4+
#include "util.hpp"
45

56
#include <iterator>
67

@@ -13,7 +14,10 @@ class DisplayDomain {
1314
public:
1415
DisplayDomain(display_coordinate start, display_coordinate end) :
1516
START_COORDINATE{start}, END_COORDINATE{end}
16-
{}
17+
{
18+
assert_true(start.x <= end.x);
19+
assert_true(start.y <= end.y);
20+
}
1721

1822
const display_coordinate& get_start_coordinate() const { return START_COORDINATE; }
1923

@@ -31,7 +35,7 @@ class DisplayDomain {
3135
using reference = display_coordinate&;
3236

3337
explicit DisplayCoordinateIterator(const DisplayDomain& domain) :
34-
GRID_WIDTH{domain.get_end_coordinate().x},
38+
GRID_WIDTH{domain.get_end_coordinate().x + 1u},
3539
current_coordinate_{
3640
decay_2d_coordinate(domain.get_start_coordinate(), GRID_WIDTH)
3741
}
@@ -77,6 +81,10 @@ class DisplayDomain {
7781
bool operator==(const DisplayCoordinateIterator&) const = default;
7882
};
7983

84+
uint32_t width() const { return END_COORDINATE.x - START_COORDINATE.x; }
85+
86+
uint32_t height() const { return END_COORDINATE.y - START_COORDINATE.y; }
87+
8088
DisplayCoordinateIterator begin() const { return DisplayCoordinateIterator{*this}; }
8189

8290
DisplayCoordinateIterator end() const

0 commit comments

Comments
 (0)