Skip to content

Commit

Permalink
Avoid gdouble compare in SelectionRectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
qarkai committed Mar 1, 2025
1 parent 639db88 commit ccd223f
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,24 @@ struct SelectionRectangle
gint width = 0;
gint x = 0;
gint y = 0;
gdouble aspect_ratio = 0.0;
RectangleDrawAspectRatio rectangle_draw_aspect_ratio = RECTANGLE_DRAW_ASPECT_RATIO_NONE;

SelectionRectangle(gint origin_x = 0, gint origin_y = 0, int rectangle_draw_aspect_ratio = RECTANGLE_DRAW_ASPECT_RATIO_NONE);
SelectionRectangle(gint origin_x = 0, gint origin_y = 0,
RectangleDrawAspectRatio rectangle_draw_aspect_ratio = RECTANGLE_DRAW_ASPECT_RATIO_NONE);

gdouble aspect_ratio() const;
void set_cursor(gint x, gint y);
};

SelectionRectangle::SelectionRectangle(gint origin_x, gint origin_y, int rectangle_draw_aspect_ratio)
SelectionRectangle::SelectionRectangle(gint origin_x, gint origin_y, RectangleDrawAspectRatio rectangle_draw_aspect_ratio)
: origin_x(origin_x)
, origin_y(origin_y)
, rectangle_draw_aspect_ratio(rectangle_draw_aspect_ratio)
{}

gdouble SelectionRectangle::aspect_ratio() const
{
this->aspect_ratio = aspect_ratios[rectangle_draw_aspect_ratio];
return aspect_ratios[static_cast<gint>(rectangle_draw_aspect_ratio)];
}

void SelectionRectangle::set_cursor(gint x, gint y)
Expand All @@ -91,19 +98,20 @@ void SelectionRectangle::set_cursor(gint x, gint y)
this->width = std::abs(x - origin_x);
this->height = std::abs(y - origin_y);

if (aspect_ratio != 0.0)
{
if (width < height)
this->width = height / aspect_ratio;
else
this->height = width / aspect_ratio;
// left side of the origin: move x to respect that origin
if (cursor_x < origin_x)
this->x = origin_x - width;
// above the origin: move y
if (cursor_y < origin_y)
this->y = origin_y - height;
}
if (rectangle_draw_aspect_ratio == RECTANGLE_DRAW_ASPECT_RATIO_NONE) return;

if (width < height)
this->width = height / aspect_ratio();
else
this->height = width / aspect_ratio();

// left side of the origin: move x to respect that origin
if (cursor_x < origin_x)
this->x = origin_x - width;

// above the origin: move y
if (cursor_y < origin_y)
this->y = origin_y - height;
}

// For draw rectangle function
Expand Down Expand Up @@ -290,13 +298,14 @@ static void image_drag_cb(PixbufRenderer *pr, GdkEventMotion *event, gpointer da

if (options->rectangle_draw_aspect_ratio != RECTANGLE_DRAW_ASPECT_RATIO_NONE)
{
if (gdouble(image_x_pixel - image_start_x) / (image_y_pixel - image_start_y) < selection_rectangle.aspect_ratio)
const gdouble aspect_ratio = selection_rectangle.aspect_ratio();
if (gdouble(image_x_pixel - image_start_x) / (image_y_pixel - image_start_y) < aspect_ratio)
{
image_x_pixel = image_start_x + ((image_y_pixel - image_start_y) * selection_rectangle.aspect_ratio);
image_x_pixel = image_start_x + ((image_y_pixel - image_start_y) * aspect_ratio);
}
else
{
image_y_pixel = image_start_y + ((image_x_pixel - image_start_x) / selection_rectangle.aspect_ratio);
image_y_pixel = image_start_y + ((image_x_pixel - image_start_x) / aspect_ratio);
}
}

Expand Down

0 comments on commit ccd223f

Please sign in to comment.