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).

1  
// cycleLength e.g. 360
2  
static double cyclicDistance(double a, double b, double cycleLength) {
3  
  a = mod(a, cycleLength); // a is now between 0 and 360
4  
  b = mod(b, cycleLength); // b is now between 0 and 360
5  
  
6  
  // if e.g. a is 40 and b is 50, then distance is 10
7  
  // if e.g. a is 10 and b is 350, then distance is 20
8  
  // if e.g. b is 10 and a is 350, then distance is 20
9  
  double diff = absDiff(a, b);
10  
  if (a < b)
11  
    diff = min(diff, a+cycleLength-b);
12  
  else
13  
    diff = min(diff, b+cycleLength-a);
14  
  ret diff;
15  
}

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: 59 / 91
Version history: 1 change(s)
Referenced in: [show references]