Merge pull request #318 from mono/rect-adds
add overloads to have more control over the ceiling/floor operations
This commit is contained in:
Коммит
3cda52c2e1
|
@ -1134,14 +1134,16 @@ namespace SkiaSharp
|
|||
|
||||
public SKRectI AspectFill (SKSizeI size) => Truncate (((SKRect)this).AspectFill (size));
|
||||
|
||||
public static SKRectI Ceiling (SKRect value)
|
||||
public static SKRectI Ceiling (SKRect value) => Ceiling (value, false);
|
||||
|
||||
public static SKRectI Ceiling (SKRect value, bool outwards)
|
||||
{
|
||||
int x, y, r, b;
|
||||
checked {
|
||||
x = (int) Math.Ceiling (value.Left);
|
||||
y = (int) Math.Ceiling (value.Top);
|
||||
r = (int) Math.Ceiling (value.Right);
|
||||
b = (int) Math.Ceiling (value.Bottom);
|
||||
x = (int) (outwards && value.Width > 0 ? Math.Floor (value.Left) : Math.Ceiling (value.Left));
|
||||
y = (int) (outwards && value.Height > 0 ? Math.Floor (value.Top) : Math.Ceiling (value.Top));
|
||||
r = (int) (outwards && value.Width < 0 ? Math.Floor (value.Right) : Math.Ceiling (value.Right));
|
||||
b = (int) (outwards && value.Height < 0 ? Math.Floor (value.Bottom) : Math.Ceiling (value.Bottom));
|
||||
}
|
||||
|
||||
return new SKRectI (x, y, r, b);
|
||||
|
@ -1197,6 +1199,21 @@ namespace SkiaSharp
|
|||
return new SKRectI (x, y, r, b);
|
||||
}
|
||||
|
||||
public static SKRectI Floor (SKRect value) => Floor (value, false);
|
||||
|
||||
public static SKRectI Floor (SKRect value, bool inwards)
|
||||
{
|
||||
int x, y, r, b;
|
||||
checked {
|
||||
x = (int) (inwards && value.Width > 0 ? Math.Ceiling (value.Left) : Math.Floor (value.Left));
|
||||
y = (int) (inwards && value.Height > 0 ? Math.Ceiling (value.Top) : Math.Floor (value.Top));
|
||||
r = (int) (inwards && value.Width < 0 ? Math.Ceiling (value.Right) : Math.Floor (value.Right));
|
||||
b = (int) (inwards && value.Height < 0 ? Math.Ceiling (value.Bottom) : Math.Floor (value.Bottom));
|
||||
}
|
||||
|
||||
return new SKRectI (x, y, r, b);
|
||||
}
|
||||
|
||||
public static SKRectI Truncate (SKRect value)
|
||||
{
|
||||
int x, y, r, b;
|
||||
|
|
|
@ -124,5 +124,35 @@ namespace SkiaSharp.Tests
|
|||
Assert.AreEqual(40, fitWide.Width);
|
||||
Assert.AreEqual(20, fitWide.Height);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SKRectICeilingWorksAsExpected()
|
||||
{
|
||||
Assert.AreEqual(new SKRectI(6, 6, 21, 21), SKRectI.Ceiling(new SKRect(5.5f, 5.5f, 20.5f, 20.5f)));
|
||||
Assert.AreEqual(new SKRectI(5, 5, 21, 21), SKRectI.Ceiling(new SKRect(5.5f, 5.5f, 20.5f, 20.5f), true));
|
||||
Assert.AreEqual(new SKRectI(6, 6, 21, 21), SKRectI.Ceiling(new SKRect(5.4f, 5.6f, 20.4f, 20.6f)));
|
||||
Assert.AreEqual(new SKRectI(5, 5, 21, 21), SKRectI.Ceiling(new SKRect(5.4f, 5.6f, 20.4f, 20.6f), true));
|
||||
Assert.AreEqual(new SKRectI(21, 21, 6, 6), SKRectI.Ceiling(new SKRect(20.4f, 20.6f, 5.4f, 5.6f)));
|
||||
Assert.AreEqual(new SKRectI(21, 21, 5, 5), SKRectI.Ceiling(new SKRect(20.4f, 20.6f, 5.4f, 5.6f), true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SKRectIFloorWorksAsExpected()
|
||||
{
|
||||
Assert.AreEqual(new SKRectI(5, 5, 20, 20), SKRectI.Floor(new SKRect(5.5f, 5.5f, 20.5f, 20.5f)));
|
||||
Assert.AreEqual(new SKRectI(6, 6, 20, 20), SKRectI.Floor(new SKRect(5.5f, 5.5f, 20.5f, 20.5f), true));
|
||||
Assert.AreEqual(new SKRectI(5, 5, 20, 20), SKRectI.Floor(new SKRect(5.4f, 5.6f, 20.4f, 20.6f)));
|
||||
Assert.AreEqual(new SKRectI(6, 6, 20, 20), SKRectI.Floor(new SKRect(5.4f, 5.6f, 20.4f, 20.6f), true));
|
||||
Assert.AreEqual(new SKRectI(20, 20, 5, 5), SKRectI.Floor(new SKRect(20.4f, 20.6f, 5.4f, 5.6f)));
|
||||
Assert.AreEqual(new SKRectI(20, 20, 6, 6), SKRectI.Floor(new SKRect(20.4f, 20.6f, 5.4f, 5.6f), true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SKRectIRoundWorksAsExpected()
|
||||
{
|
||||
Assert.AreEqual(new SKRectI(6, 6, 21, 21), SKRectI.Round(new SKRect(5.51f, 5.51f, 20.51f, 20.51f)));
|
||||
Assert.AreEqual(new SKRectI(5, 6, 20, 21), SKRectI.Round(new SKRect(5.41f, 5.61f, 20.41f, 20.61f)));
|
||||
Assert.AreEqual(new SKRectI(20, 21, 5, 6), SKRectI.Round(new SKRect(20.41f, 20.61f, 5.41f, 5.61f)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче