1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Buttplug Rust Source Code File - See https://buttplug.io for more info.
//
// Copyright 2016-2022 Nonpolynomial Labs LLC. All rights reserved.
//
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.

#[allow(dead_code)]
pub fn calculate_distance(duration: u32, mut speed: f64) -> f64 {
  if speed <= 0f64 {
    return 0f64;
  }

  if speed > 1f64 {
    speed = 1f64;
  }

  let mil = (speed / 250f64).powf(-0.95);
  let diff = mil - (duration as f64);
  if diff.abs() < 0.001 {
    0f64
  } else {
    ((90f64 - (diff / mil * 90f64)) / 100f64)
      .min(1f64)
      .max(0f64)
  }
}

pub fn calculate_speed(mut distance: f64, duration: u32) -> f64 {
  if distance < 0f64 {
    return 0f64;
  }

  if distance > 1f64 {
    distance = 1f64;
  }

  let scalar = ((duration as f64 * 90f64) / (distance * 100f64)).powf(-1.05);

  250f64 * scalar
}

pub fn calculate_duration(mut distance: f64, mut speed: f64) -> u32 {
  if distance <= 0f64 || speed <= 0f64 {
    return 0;
  }

  if distance > 1f64 {
    distance = 1f64;
  }

  if speed > 1f64 {
    speed = 1f64;
  }

  let mil = (speed / 250f64).powf(-0.95);
  (mil / (90f64 / (distance * 100f64))) as u32
}