Giter Club home page Giter Club logo

num_remap's Introduction

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

Features

remap method:

Remaps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, and values in-between to values in-between.

It does not constrain the values to the provided range, because out-of-range values are sometimes intended and useful. Use the remapAndClamp method if you wish for the values to be constrained.

Note that the “lower bounds” of either range may be larger or smaller than the “upper bounds” so the remap() method may be used to reverse a range of numbers, for example:

final reversedX = x.remap(1, 50, 50, 1);

The method also handles negative numbers well, so that this example

final y = x.remap(1, 50, 50, -100);

is also valid.


remapAndClamp method:

Same as remap, however, the result is being constrained to the range toLow-toHigh.

For instance, the following code returns 1:

150.remapAndClamp(0, 100, 0, 1)

Note that toLow may be greater than toHigh, so the following code

final y = x.remapAndClamp(0, 100, 100, 0);

works.


Integer-only methods:

Both the remap and the remapAndClamp offer integer-only versions of themselves (named remapInt and remapAndClampInt respectively). These work the same as their num counterparts, however, their returned value is guaranteed to be an integer.


isWithinRange method:

Returns whether a number is within a given range.


Getting started

  1. Add the package as a dependency to your pubspec.yaml.
  2. Import the library:
import 'package:num_remap/num_remap.dart';

Usage

The library acts as an extension to the num and int types. You can use its methods as follows:

import 'package:num_remap/num_remap.dart';

...

final someDouble = 0.5;
final remappedDouble = someDouble.remap(0.0, 1.0, -100.0, 100.0);

num_remap's People

Contributors

adrian-samoticha avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.