Skip to content

Commit

Permalink
Add forgotten fuzzy func
Browse files Browse the repository at this point in the history
  • Loading branch information
dzibma committed Jul 13, 2015
1 parent 35c8d3d commit 4ff8670
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions FuzzyMath/Functions/atan2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;

namespace FuzzyMath
{
public static partial class Functions
{

public static FuzzyNumber Atan2(FuzzyNumber Y, FuzzyNumber X)
{
var rotated = Y.Support.Contains(0) && !X.Support.Contains(0);

return FuzzyNumber.Map(Y, X, (y, x) => Atan2(y, x, rotated));
}

private static double Atan2Rotated(double y, double x)
{
if (x < 0 && y >= 0)
{
return Math.Atan(y / x) - Math.PI;
}

return Math.Atan2(y, x);
}

private static Interval Atan2(Interval y, Interval x, bool rotated)
{
double[] values;

if (rotated)
{
if (y.Contains(0) && x.Contains(0))
return new Interval(-1.5 * Math.PI, 0.5 * Math.PI);

values = new double[4] {
Atan2Rotated(y.A, x.A),
Atan2Rotated(y.A, x.B),
Atan2Rotated(y.B, x.A),
Atan2Rotated(y.B, x.B)
};
}
else
{
if (y.Contains(0) && x.Contains(0))
return new Interval(-Math.PI, Math.PI);

values = new double[4] {
Math.Atan2(y.A, x.A),
Math.Atan2(y.A, x.B),
Math.Atan2(y.B, x.A),
Math.Atan2(y.B, x.B)
};
}

Array.Sort(values);

return new Interval(values[0], values[3]);
}

}
}

0 comments on commit 4ff8670

Please sign in to comment.