Use double.NaN instead of double.MinValue

Only compute t, cos and sin once
This commit is contained in:
BobLd
2020-01-29 12:54:15 +00:00
committed by Eliot Jones
parent b585fe9581
commit 6dce4b1f8d

View File

@@ -39,11 +39,7 @@
{
get
{
var t = GetT();
var cosT = Math.Cos(t);
var sinT = Math.Sin(t);
(double cosT, double sinT) = GetCosSin();
var cx = Math.Min(BottomRight.X, Math.Min(TopLeft.X, Math.Min(BottomLeft.X, TopRight.X)))
+ (Width * cosT + Height * sinT) / 2.0;
var cy = Math.Min(BottomRight.Y, Math.Min(TopLeft.Y, Math.Min(BottomLeft.Y, TopRight.Y)))
@@ -61,11 +57,10 @@
{
get
{
if (width == double.MinValue)
if (double.IsNaN(width))
{
GetWidthHeight();
}
return width;
}
}
@@ -78,11 +73,10 @@
{
get
{
if (height == double.MinValue)
if (double.IsNaN(height))
{
GetWidthHeight();
}
return height;
}
}
@@ -94,9 +88,7 @@
{
get
{
var rotation = GetT() * 180 / Math.PI;
return rotation;
return GetT() * 180 / Math.PI;
}
}
@@ -167,8 +159,11 @@
BottomLeft = bottomLeft;
BottomRight = bottomRight;
width = double.MinValue;
height = double.MinValue;
width = double.NaN;
height = double.NaN;
t = double.NaN;
cosT = double.NaN;
sinT = double.NaN;
}
/// <summary>
@@ -183,19 +178,21 @@
BottomLeft.Translate(dx, dy), BottomRight.Translate(dx, dy));
}
private double t;
private double GetT()
{
double t;
if (!BottomRight.Equals(BottomLeft))
if (double.IsNaN(t))
{
t = Math.Atan2(BottomRight.Y - BottomLeft.Y, BottomRight.X - BottomLeft.X);
if (!BottomRight.Equals(BottomLeft))
{
t = Math.Atan2(BottomRight.Y - BottomLeft.Y, BottomRight.X - BottomLeft.X);
}
else
{
// handle the case where both bottom points are identical
t = Math.Atan2(TopLeft.Y - BottomLeft.Y, TopLeft.X - BottomLeft.X) - Math.PI / 2;
}
}
else
{
// handle the case where both bottom points are identical
t = Math.Atan2(TopLeft.Y - BottomLeft.Y, TopLeft.X - BottomLeft.X) - Math.PI / 2;
}
return t;
}
@@ -204,16 +201,26 @@
var bx = Math.Max(Math.Abs(BottomRight.X - TopLeft.X), Math.Abs(BottomLeft.X - TopRight.X));
var by = Math.Max(Math.Abs(TopRight.Y - BottomLeft.Y), Math.Abs(TopLeft.Y - BottomRight.Y));
var t = GetT();
var cosT = Math.Cos(t);
var sinT = Math.Sin(t);
(double cosT, double sinT) = GetCosSin();
var cosSqSinSqInv = 1.0 / (cosT * cosT - sinT * sinT);
width = cosSqSinSqInv * (bx * cosT - by * sinT);
height = cosSqSinSqInv * (-bx * sinT + by * cosT);
}
private double cosT;
private double sinT;
private (double, double) GetCosSin()
{
if (double.IsNaN(cosT) || double.IsNaN(sinT))
{
var t = GetT();
cosT = Math.Cos(t);
sinT = Math.Sin(t);
}
return (cosT, sinT);
}
/// <inheritdoc />
public override string ToString()
{