Skip to content

Commit 066d391

Browse files
committed
Move CairoExtensions.PointsToRectangle into RectangleD
1 parent 89b8320 commit 066d391

File tree

6 files changed

+21
-25
lines changed

6 files changed

+21
-25
lines changed

Pinta.Core/Classes/DocumentWorkspace.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void InvalidateWindowRect (RectangleI windowRect)
170170
PointD canvasTopLeft = ViewPointToCanvas (windowTopLeft);
171171
PointD canvasBtmRight = ViewPointToCanvas (windowBtmRight);
172172

173-
RectangleI canvasRect = CairoExtensions.PointsToRectangle (canvasTopLeft, canvasBtmRight).ToInt ();
173+
RectangleI canvasRect = RectangleD.FromPoints (canvasTopLeft, canvasBtmRight).ToInt ();
174174
OnCanvasInvalidated (new CanvasInvalidatedEventArgs (canvasRect));
175175
}
176176

Pinta.Core/Classes/Rectangle.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ double bottom
5050
right - left + 1,
5151
bottom - top + 1);
5252

53+
/// <summary>
54+
/// Creates a rectangle with positive width & height from the provided points.
55+
/// Note that the second point will be the bottom right corner of the rectangle,
56+
/// and the pixel is not inside the rectangle itself.
57+
/// </summary>
58+
public static RectangleD FromPoints (in PointD a, in PointD b)
59+
{
60+
double x1 = Math.Min (a.X, b.X);
61+
double y1 = Math.Min (a.Y, b.Y);
62+
double x2 = Math.Max (a.X, b.X);
63+
double y2 = Math.Max (a.Y, b.Y);
64+
return new (x1, y1, x2 - x1, y2 - y1);
65+
}
66+
5367
public static RectangleD Zero { get; } = new (0d, 0d, 0d, 0d);
5468

5569
public readonly RectangleI ToInt ()

Pinta.Core/Extensions/Cairo/CairoExtensions.Geometry.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,6 @@ public static RectangleD StrokeExtents (this Context g)
5555
y2 - y1);
5656
}
5757

58-
/// <summary>
59-
/// Create a rectangle with a positive width / height from the provided points.
60-
/// </summary>
61-
public static RectangleD PointsToRectangle (
62-
PointD p1,
63-
PointD p2)
64-
{
65-
double y1 = Math.Min (p1.Y, p2.Y);
66-
double y2 = Math.Max (p1.Y, p2.Y);
67-
double x1 = Math.Min (p1.X, p2.X);
68-
double x2 = Math.Max (p1.X, p2.X);
69-
70-
return new (
71-
x1,
72-
y1,
73-
x2 - x1,
74-
y2 - y1);
75-
}
76-
7758
public static Region CreateRegion (in RectangleI rect)
7859
{
7960
CairoRectangleInt cairo_rect = new () {

Pinta.Tools/Tools/SelectTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ private RectangleI ReDraw (Document document)
349349

350350
ShowHandles (true);
351351

352-
RectangleD rect = CairoExtensions.PointsToRectangle (shape_origin, shape_end);
352+
RectangleD rect = RectangleD.FromPoints (shape_origin, shape_end);
353353

354354
DrawShape (document, rect, document.Layers.SelectionLayer);
355355

Pinta.Tools/Tools/ZoomTool.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ protected override void OnMouseUp (Document document, ToolMouseEventArgs e)
126126

127127
if (mouse_down == MouseButton.Left || mouse_down == MouseButton.Right) {
128128
if (e.MouseButton == MouseButton.Left) {
129-
var shape_origin_window = document.Workspace.CanvasPointToView (shape_origin);
130-
if (shape_origin_window.Distance (e.WindowPoint) <= tolerance) {
129+
PointD shapeOriginWindow = document.Workspace.CanvasPointToView (shape_origin);
130+
if (shapeOriginWindow.Distance (e.WindowPoint) <= tolerance) {
131131
document.Workspace.ZoomInAroundCanvasPoint (e.PointDouble);
132132
} else {
133-
document.Workspace.ZoomToCanvasRectangle (CairoExtensions.PointsToRectangle (shape_origin, e.PointDouble));
133+
document.Workspace.ZoomToCanvasRectangle (RectangleD.FromPoints (shape_origin, e.PointDouble));
134134
}
135135
} else {
136136
document.Workspace.ZoomOutAroundCanvasPoint (e.PointDouble);
@@ -149,7 +149,7 @@ private void UpdateRectangle (Document document, PointD point)
149149
if (!is_drawing)
150150
return;
151151

152-
var r = CairoExtensions.PointsToRectangle (shape_origin.Rounded (), point.Rounded ());
152+
RectangleD r = RectangleD.FromPoints (shape_origin.Rounded (), point.Rounded ());
153153

154154
document.Layers.ToolLayer.Clear ();
155155
document.Layers.ToolLayer.Hidden = false;

tests/Pinta.Core.Tests/RectangleTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public void ConsistentLTRBFactory (int l, int t, int r, int b)
3838
public void CorrectFromPoints (PointI a, PointI b, RectangleI expected)
3939
{
4040
Assert.That (RectangleI.FromPoints (a, b), Is.EqualTo (expected));
41+
Assert.That (RectangleD.FromPoints (a.ToDouble (), b.ToDouble ()), Is.EqualTo (expected.ToDouble ()));
4142
}
4243

4344
[TestCaseSource (nameof (not_equal_cases))]

0 commit comments

Comments
 (0)