Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/Painting/BorderRadiiData.h (C/C++)

Code before:
    CornerRadius top_left;
    CornerRadius top_right;
    CornerRadius bottom_right;
    CornerRadius bottom_left;

    inline bool has_any_radius() const
    {
        return top_left || top_right || bottom_right || bottom_left;
    }
};

struct BorderRadiiData {
    BorderRadiusData top_left;
    BorderRadiusData top_right;
    BorderRadiusData bottom_right;
    BorderRadiusData bottom_left;

    inline bool has_any_radius() const
    {
        return top_left || top_right || bottom_right || bottom_left;
    }

    bool contains(CSSPixelPoint point, CSSPixelRect const& rect) const
    {
        if (!has_any_radius())
            return rect.contains(point);

        if (!rect.contains(point))
            return false;

        auto const px = point.x();
        auto const py = point.y();

        auto outside_ellipse = [&](BorderRadiusData const& r, CSSPixels cx, CSSPixels cy) {
            auto dx = (px - cx).to_float() / r.horizontal_radius.to_float();
            auto dy = (py - cy).to_float() / r.vertical_radius.to_float();
            return dx * dx + dy * dy > 1.0f;
        };

        if (top_left) {
            auto cx = rect.left() + top_left.horizontal_radius;
            auto cy = rect.top() + top_left.vertical_radius;
            if (px < cx && py < cy && outside_ellipse(top_left, cx, cy))
                return false;
        }

        if (top_right) {
            auto cx = rect.right() - top_right.horizontal_radius;
            auto cy = rect.top() + top_right.vertical_radius;
            if (px > cx && py < cy && outside_ellipse(top_right, cx, cy))
                return false;

Review comment (refactor):
If you move this to the top, you can simplify it slightly:

```cpp
        if (!rect.contains(point))
            return false;

        if (!has_any_radius())
            return true;
```

Code after:
    CornerRadius top_left;
    CornerRadius top_right;
    CornerRadius bottom_right;
    CornerRadius bottom_left;

    inline bool has_any_radius() const
    {
        return top_left || top_right || bottom_right || bottom_left;
    }
};

struct BorderRadiiData {
    BorderRadiusData top_left;
    BorderRadiusData top_right;
    BorderRadiusData bottom_right;
    BorderRadiusData bottom_left;

    inline bool has_any_radius() const
    {
        return top_left || top_right || bottom_right || bottom_left;
    }

    bool contains(CSSPixelPoint point, CSSPixelRect const& rect) const
    {
        if (!rect.contains(point))
            return false;

        if (!has_any_radius())
            return true;

        auto const px = point.x();
        auto const py = point.y();

        auto outside_ellipse = [&](BorderRadiusData const& r, CSSPixels cx, CSSPixels cy) {
            auto dx = (px - cx).to_float() / r.horizontal_radius.to_float();
            auto dy = (py - cy).to_float() / r.vertical_radius.to_float();
            return dx * dx + dy * dy > 1.0f;
        };

        if (top_left) {
            auto cx = rect.left() + top_left.horizontal_radius;
            auto cy = rect.top() + top_left.vertical_radius;
            if (px < cx && py < cy && outside_ellipse(top_left, cx, cy))
                return false;
        }

        if (top_right) {
            auto cx = rect.right() - top_right.horizontal_radius;
            auto cy = rect.top() + top_right.vertical_radius;
            if (px > cx && py < cy && outside_ellipse(top_right, cx, cy))
                return false;