Skip to content

Commit

Permalink
fix: apply same scale for x and y in map widget projection, centering…
Browse files Browse the repository at this point in the history
… scaled x or y whichever is needed
  • Loading branch information
neri14 committed Sep 7, 2024
1 parent 6ad7c96 commit 6d89616
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/video/overlay/widget/map_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ void map_widget::prepare(const std::vector<std::shared_ptr<telemetry::datapoint>
}

if (min_x < max_x && min_y < max_y) {
scale_x = width_/(max_x-min_x);
scale_y = height_/(max_y-min_y);
double scale_x = width_/(max_x-min_x);
double scale_y = height_/(max_y-min_y);

if (scale_x < scale_y) {
scale = scale_x;
offset_y = (height_ - ((max_y - min_y) * scale_x))/2;
} else {
scale = scale_y;
offset_x = (width_ - ((max_x - min_x) * scale_y))/2;
}

valid = true;
log.debug("Prepared data for generating widget");
Expand Down Expand Up @@ -111,8 +119,8 @@ void map_widget::draw_dynamic_impl(cairo_t* cr, std::shared_ptr<telemetry::datap

std::pair<int, int> map_widget::translate_xy(double x, double y)
{
return std::make_pair(static_cast<int>(std::round(((x-min_x)*scale_x) + x_)),
static_cast<int>(std::round(((y-min_y)*scale_y) + y_)));
return std::make_pair(static_cast<int>(std::round(((x-min_x)*scale) + x_ + offset_x)),
static_cast<int>(std::round(((y-min_y)*scale) + y_ + offset_y)));
}

std::pair<double, double> map_widget::project(double x, double y)
Expand Down
5 changes: 3 additions & 2 deletions src/video/overlay/widget/map_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ class map_widget: public widget {
double min_y = DBL_MAX;
double max_y = DBL_MIN;

double scale_x = 0;
double scale_y = 0;
double scale = 0;
double offset_x = 0;
double offset_y = 0;
bool valid = false;
};

Expand Down

0 comments on commit 6d89616

Please sign in to comment.