num_remap 1.0.2 copy "num_remap: ^1.0.2" to clipboard
num_remap: ^1.0.2 copied to clipboard

An implementation for the “Arduino map” function in Dart, which allows numbers to be remapped from one range to another.

example/num_remap_example.dart

import 'package:num_remap/num_remap.dart';

void printSeparator() {
  print('-' * 16);
}

void main() {
  // basic usage
  print(50.remap(0, 100, 100, 200)); // prints '150.0'
  print(150.remap(100, 200, 0.0, 1.0)); // prints '0.5'

  printSeparator();

  // remap and clamp
  print(150.remap(0, 100, 0, 1)); // prints '1.5'
  print(150.remapAndClamp(0, 100, 0,
      1)); // prints '1' since the value is clamped to be in the range 0-1

  printSeparator();

  // the “lower bound” may also be greater than the “upper bound”
  print(150.remapAndClamp(0, 100, 1,
      0)); // prints '0' since the value is clamped to be in the range 1-0
  print(150.remapAndClamp(100, 0, 1,
      0)); // prints '1' since the value is clamped to be in the range 1-0

  printSeparator();

  // both methods also exist as integer-only versions
  print(75.remapInt(
      0, 100, 0, 10)); // prints '7' since the return value is an integer
  print(115.remapAndClampInt(0, 100, 0,
      10)); // prints '10' since the value is clamped to be in the range 0-10
}
3
likes
140
pub points
75%
popularity

Publisher

unverified uploader

An implementation for the “Arduino map” function in Dart, which allows numbers to be remapped from one range to another.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

More

Packages that depend on num_remap