Move polar<=>rect conversion to new class

This commit is contained in:
YoshiRulz 2019-06-13 13:25:34 +10:00 committed by James Groom
parent 7e244be91a
commit 1c0622d272
3 changed files with 33 additions and 10 deletions

View File

@ -142,6 +142,7 @@
<Compile Include="inputAdapters\OverrideAdaptor.cs" />
<Compile Include="inputAdapters\SimpleController.cs" />
<Compile Include="inputAdapters\UDLRController.cs" />
<Compile Include="input\PolarRectConversion.cs" />
<Compile Include="IonicZipWriter.cs" />
<Compile Include="IPS.cs" />
<Compile Include="IZipWriter.cs" />
@ -320,7 +321,6 @@
<Name>BizHawk.Bizware.BizwareGL</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>

View File

@ -0,0 +1,20 @@
using System;
using DoublePair = System.Tuple<double, double>;
namespace BizHawk.Client.Common
{
public static class PolarRectConversion
{
/// <param name="θ">angle in degrees</param>
/// <returns>rectangular (Cartesian) coordinates (x, y)</returns>
public static DoublePair PolarDegToRect(double r, double θ) => PolarRadToRect(r, θ * Math.PI / 180);
/// <param name="θ">angle in radians</param>
/// <returns>rectangular (Cartesian) coordinates (x, y)</returns>
public static DoublePair PolarRadToRect(double r, double θ) => new DoublePair(r * Math.Cos(θ), r * Math.Sin(θ));
/// <returns>polar coordinates (r, θ) where θ is in degrees</returns>
public static DoublePair RectToPolarDeg(double x, double y) => new DoublePair(Math.Sqrt(x * x + y * y), Math.Atan2(y, x) * 180 / Math.PI);
}
}

View File

@ -178,15 +178,17 @@ namespace BizHawk.Client.EmuHawk
private void PolarNumeric_Changed(object sender, EventArgs e)
{
ManualX.ValueChanged -= ManualXY_ValueChanged;
ManualX.ValueChanged -= ManualXY_ValueChanged; //TODO is setting and checking a bool faster than subscription?
ManualY.ValueChanged -= ManualXY_ValueChanged;
ManualX.Value = Math.Ceiling(manualR.Value * (decimal)Math.Cos(Math.PI * (double)manualTheta.Value / 180)).Clamp(-127, 127) + rangeAverageX;
ManualY.Value = Math.Ceiling(manualR.Value * (decimal)Math.Sin(Math.PI * (double)manualTheta.Value / 180)).Clamp(-127, 127) + rangeAverageY;
AnalogStick.X = (int)ManualX.Value;
AnalogStick.Y = (int)ManualY.Value;
var rect = PolarRectConversion.PolarDegToRect((double) manualR.Value, (double) manualTheta.Value);
rect = new Tuple<double, double>(
rangeAverageX + Math.Ceiling(rect.Item1).Clamp(-127, 127),
rangeAverageY + Math.Ceiling(rect.Item2).Clamp(-127, 127));
ManualX.Value = (decimal) rect.Item1;
ManualY.Value = (decimal) rect.Item2;
AnalogStick.X = (int) rect.Item1;
AnalogStick.Y = (int) rect.Item2;
AnalogStick.HasValue = true;
AnalogStick.Refresh();
@ -238,8 +240,9 @@ namespace BizHawk.Client.EmuHawk
manualR.ValueChanged -= PolarNumeric_Changed;
manualTheta.ValueChanged -= PolarNumeric_Changed;
manualR.Value = Math.Min(manualR.Value, (decimal)Math.Sqrt(Math.Pow(AnalogStick.X - rangeAverageX, 2) + Math.Pow(AnalogStick.Y - rangeAverageY, 2)));
manualTheta.Value = (decimal)(Math.Atan2(AnalogStick.Y - rangeAverageY, AnalogStick.X - rangeAverageX) * (180 / Math.PI));
var polar = PolarRectConversion.RectToPolarDeg(AnalogStick.X - rangeAverageX, AnalogStick.Y - rangeAverageY);
manualR.Value = Math.Min(manualR.Value, (decimal) polar.Item1); //TODO bug? if not, this can be `if (polar.Item1 < manualR.Value) manualR.Value = polar.Item1;`
manualTheta.Value = (decimal) polar.Item2;
manualR.ValueChanged += PolarNumeric_Changed;
manualTheta.ValueChanged += PolarNumeric_Changed;