From b8cc26793babe4d471867431c1ee7b4425459ea2 Mon Sep 17 00:00:00 2001 From: Dunbaratu Date: Thu, 26 Jun 2014 23:43:30 -0500 Subject: [PATCH] Added function anglediff( a1, a2 ). returns the difference in angle between a1 and a2, quashed into the range (-180,180]. With positve numbers meaning a2 is a bigger angle than a1. example: anglediff( 5, 355 ) returns -10. anglediff(355,5) returns +10. Also squashes the result into meaningful ranges. i.e. anglediff(365,355) handles the fact that 365 is > 360 just fine. Important because users can't write functions yet, and KSP tends to return things like longitude or Rotational components in stupid ways like "540 degrees". TODO: wheelsteering should call the same thing this code does to fix the 'can't steer north' bug, but that isn't part of this commit. --- src/Function/Trigonometry.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Function/Trigonometry.cs b/src/Function/Trigonometry.cs index 4dc4df716..c39aec9bf 100644 --- a/src/Function/Trigonometry.cs +++ b/src/Function/Trigonometry.cs @@ -82,4 +82,16 @@ public override void Execute(SharedObjects shared) shared.Cpu.PushStack(result); } } + + [FunctionAttribute("anglediff")] + public class FunctionAngleDiff : FunctionBase + { + public override void Execute(SharedObjects shared) + { + double ang2 = GetDouble(shared.Cpu.PopValue()); + double ang1 = GetDouble(shared.Cpu.PopValue()); + double result = kOS.Utilities.Utils.DegreeFix( ang2 - ang1, -180 ); + shared.Cpu.PushStack(result); + } + } }