Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

15
LINES

< > BotCompany Repo | #1035274 // cyclicDistance - absolute distance between two values on a cyclic scale (e.g. angles)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (141L/1K).

// cycleLength e.g. 360
static double cyclicDistance(double a, double b, double cycleLength) {
  a = mod(a, cycleLength); // a is now between 0 and 360
  b = mod(b, cycleLength); // b is now between 0 and 360
  
  // if e.g. a is 40 and b is 50, then distance is 10
  // if e.g. a is 10 and b is 350, then distance is 20
  // if e.g. b is 10 and a is 350, then distance is 20
  double diff = absDiff(a, b);
  if (a < b)
    diff = min(diff, a+cycleLength-b);
  else
    diff = min(diff, b+cycleLength-a);
  ret diff;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035274
Snippet name: cyclicDistance - absolute distance between two values on a cyclic scale (e.g. angles)
Eternal ID of this version: #1035274/2
Text MD5: cb37c4cdad2ce8fe364a64d57b16fa99
Transpilation MD5: 643e0fb21613d3f592eb466171ed90f0
Author: stefan
Category: javax / maths
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-28 20:44:35
Source code size: 532 bytes / 15 lines
Pitched / IR pitched: No / No
Views / Downloads: 55 / 85
Version history: 1 change(s)
Referenced in: [show references]