ladybird icon indicating copy to clipboard operation
ladybird copied to clipboard

LibGfx: Implement even-odd method for Quad::contains() and add tests

Open a4v2d4 opened this issue 1 year ago • 4 comments

First PR :)

LibGfx: Addressed simple FIXME comment to improve implementation of LibGfx::Quad::contains():

  • Now uses even-odd method to determine if point is inside/outside
  • Added TestQuad.cpp for basic verification of current functions

Docs: Added homebrew package shellcheck to build instructions documentation because I needed that to build on macOS

a4v2d4 avatar Aug 03 '24 02:08 a4v2d4

This has inconsistent behavior at edges (not like code you're replacing had it but), i. e. the following test passes:

TEST_CASE(foo)
{
    Gfx::Point<int> p1 { 0, 0 };
    Gfx::Point<int> p2 { 2, 0 };
    Gfx::Point<int> p3 { 2, 2 };
    Gfx::Point<int> p4 { 0, 2 };
    Gfx::Quad<int> quad { p1, p2, p3, p4 };

    EXPECT(quad.contains({ 0, 0 }));
    EXPECT(quad.contains({ 1, 0 }));
    EXPECT(quad.contains({ 0, 1 }));

    EXPECT(!quad.contains({ 2, 0 }));
    EXPECT(!quad.contains({ 2, 1 }));
    EXPECT(!quad.contains({ 2, 2 }));
    EXPECT(!quad.contains({ 1, 2 }));
    EXPECT(!quad.contains({ 0, 2 }));
}

DanShaders avatar Aug 07 '24 18:08 DanShaders

@DanShaders I appreciate your review, ty.

Turns out this behavior is consistent with LibGfx::Rect, i.e. the following test case also passes:

TEST_CASE(foo)
{
    Gfx::Point<int> p1 { 0, 0 };
    Gfx::Point<int> p2 { 2, 0 };
    Gfx::Point<int> p3 { 2, 2 };
    Gfx::Point<int> p4 { 0, 2 };
    Gfx::Quad<int> quad { p1, p2, p3, p4 };

    EXPECT(quad.contains({ 0, 0 }));
    EXPECT(quad.contains({ 1, 0 }));
    EXPECT(quad.contains({ 0, 1 }));

    EXPECT(!quad.contains({ 2, 0 }));
    EXPECT(!quad.contains({ 2, 1 }));
    EXPECT(!quad.contains({ 2, 2 }));
    EXPECT(!quad.contains({ 1, 2 }));
    EXPECT(!quad.contains({ 0, 2 }));

    Gfx::Rect<int> bounding_rect = quad.bounding_rect();
    EXPECT(bounding_rect.contains({ 0, 0 }));
    EXPECT(bounding_rect.contains({ 1, 0 }));
    EXPECT(bounding_rect.contains({ 0, 1 }));

    EXPECT(!bounding_rect.contains({ 2, 0 }));
    EXPECT(!bounding_rect.contains({ 2, 1 }));
    EXPECT(!bounding_rect.contains({ 2, 2 }));
    EXPECT(!bounding_rect.contains({ 1, 2 }));
    EXPECT(!bounding_rect.contains({ 0, 2 }));
}

Looks like LibGfx::Rect::contains() treats points at x() and y() inclusively and x() + width() and y() + height() exclusively.

Just added new test case to verify Quad and Rect contains() implementations are consistent.

a4v2d4 avatar Aug 08 '24 00:08 a4v2d4

Hello!

One or more of the commit messages in this PR do not match the Ladybird code submission policy, please check the lint_commits CI job for more details on which commits were flagged and why. Please do not close this PR and open another, instead modify your commit message(s) with git commit --amend and force push those changes to update this PR.

ladybird-bot avatar Aug 12 '24 23:08 ladybird-bot

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions!

github-actions[bot] avatar Dec 07 '24 21:12 github-actions[bot]

This pull request has been closed because it has not had recent activity. Feel free to re-open if you wish to still contribute these changes. Thank you for your contributions!

github-actions[bot] avatar Dec 15 '24 02:12 github-actions[bot]