Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Contains method for inverted rects #4597

Merged
merged 3 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 10,7 @@ _Not yet on NuGet..._
* Generate: Fixed causing `AddNoiseInPlace()` to offset the mean of the underlying signal (#4564) @nightroman
* Text: Fixed `DragTo()` to use the correct axis dimension (#4587) @claire0821
* Rendering: Improved handling of the `RenderPack` inside the render loop to avoid Skia exceptions (#4592) @vadimart92
* Axes: Improved support for `CoordinateRect` hit detection on plots with inverted axes (#4596) @SprinterDave

## ScottPlot 5.0.47
_Published on [NuGet](https://www.nuget.org/profiles/ScottPlot) on 2024-11-24_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 38,20 @@ public void Test_CoordinateRect_Expanded()
CoordinateRect cRect2 = cRect.Expanded(coord);
cRect2.Contains(coord).Should().BeTrue();
}

[TestCase(0, 0, 1, 1, 0.5, 0.5, true, TestName = "Test Contains with no inverted")]
[TestCase(0, 0, 1, -1, 0.5, -0.5, true, TestName = "Test Contains with inverted X")]
[TestCase(0, 0, -1, 1, -0.5, 0.5, true, TestName = "Test Contains with inverted Y")]
[TestCase(0, 0, -1, -1, -0.5, -0.5, true, TestName = "Test Contains with inverted X and Y")]
swharden marked this conversation as resolved.
Show resolved Hide resolved
public void Test_CoordinateRect_Contains(double x1,
double y1,
double x2,
double y2,
double x,
double y,
bool expectedContains)
{
CoordinateRect coordinateRect = new CoordinateRect(x1, x2, y1, y2);
Assert.That(coordinateRect.Contains(x, y), Is.True);
}
}
6 changes: 3 additions & 3 deletions src/ScottPlot5/ScottPlot5/Primitives/CoordinateRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 76,17 @@ public CoordinateRect(Coordinates point, CoordinateSize size)

public bool Contains(double x, double y)
{
return x >= Left && x <= Right && y >= Bottom && y <= Top;
return ContainsX(x) && ContainsY(y);
}

public bool ContainsX(double x)
{
return x >= Left && x <= Right;
return IsInvertedX ? x >= Right && x <= Left : x >= Left && x <= Right;
}

public bool ContainsY(double y)
{
return y >= Bottom && y <= Top;
return IsInvertedY ? y >= Top && y <= Bottom : y >= Bottom && y <= Top;
}

public CoordinateRect Expanded(Coordinates point)
Expand Down
Loading