Skip to main content

Python convenience functions that I wish were in the math module

Project description

# maff Python convenience functions that I wish were in the math module, as well as some that I agree probably shouldn’t be.

## Quick usage

import maff print(maff.ease(0.1))

The first time you import maff, it will also pollute the math module with its own functions, so you can call them via math on any module that imports math.

import maff, math print(math.ease(0.1))

## To install

Download maff.py and put it in your source directory. To install from command line:

curl https://raw.githubusercontent.com/cosmologicon/maff/master/maff.py > my-source-directory/maff.py

## Usage note

As the name hopefully implies, this is a bit of a frivolous module and not recommended for serious use. I plan to use it for game jams.

## Constants

maff.tau: the circle constant equal to 2pi.

maff.phi and maff.Phi: golden ratio and reciprocal golden ratio.

## Functions taken from GLSL

maff.sign(x): sign of x.

maff.clamp(x, a, b): clamps x to range [a, b].

maff.mix(x, y, a): mix the values x and y with mixing factor a.

maff.step(edge, x): Heaviside step function of x with given edge.

maff.smoothstep(edge0, edge1, x): step function with Hermite interpolation between edge0 and edge1.

maff.length(v): length of vector v. Can be any iterable.

maff.distance(v0, v1): distance between vectors v0 and v1

maff.dot(v0, v1): dot product

maff.norm(v) or maff.normalize(v): normalize vector to length 1. Does not fail on zero vector.

maff.norm(v, a) or maff.normalize(v, a): normalize vector to length a.

## Fade functions

maff.ease(x): Hermite interpolation of x in range (0, 1). Equal to maff.smoothstep(0, 1, x).

maff.fade(x, x0, dx): fade from 0 to 1 starting at x = x0 for a fade interval of dx.

maff.smoothfade(x, x0, dx): like fade with Hermite interpolation.

maff.dfade(x, x0, x1, dx): double-fade from 0 to 1 starting at x0, then back from 1 to 0 ending at x1, with a fade interval of dx on both ends.

maff.dsmoothfade(x, x0, x1, dx): double-fade with Hermite interpolation.

## Approach functions

maff.approach(x, y, dx): increase or decrease x by amount dx in the direction of y. If the distance between x and y is less than dx, then return y instead. x and y can be vectors.

maff.softapproach(x, y, dlogx): increase or decrease x by an amount determined by dlogx. dlogx is a unitless parameter between 0 and infinity. If dlogx is 0, then x will be returned. For sufficiently large dlogx, then y will be returned. Calling this repeatedly and updating the value of x will give exponential decay toward y, with a timescale factor of 1 / dlogx.

maff.softapproach(x, y, dlogx, dxmax=inf, dymin=0.1): After calculating the approach distance dx, it’s compared with dxmax. If it exceeds dxmax then it’s capped at dxmax. If the resulting is a distance less than dymin away from y, then y is returned. This is because with exponential decay it’s impossible to ever exactly reach y.

## Other functions

maff.CS(theta): 2-tuple of cos(theta), sin(theta).

maff.CS(theta, r): 2-tuple of r cos(theta), r sin(theta).

maff.CSround(ntheta): Produces ntheta 2-tuples of cos(theta), sin(theta) distributed around the unit circle.

maff.CSround(ntheta, r=1, jtheta0=0): specify radius and offset.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

maff-0.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

maff-0.1-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file maff-0.1.tar.gz.

File metadata

  • Download URL: maff-0.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for maff-0.1.tar.gz
Algorithm Hash digest
SHA256 cecf9b6b4ecd1109bb9f3d4913e8a5d380ddcb11ed2e1978ba4574657cdd41d0
MD5 4aa7f5c9d324d0b02ee6c3555ceb0126
BLAKE2b-256 b9cf33eec3ca733167e74eb74f3bb29b1c68cd7872ee4396eb3c68cf7fe6eb44

See more details on using hashes here.

File details

Details for the file maff-0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for maff-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d01384fcb53a35257c3c16dfb9dfb70638e8dd18c038f6296d00c0b4ca9181c6
MD5 1f88661cdaf1bba8e959915d42b79389
BLAKE2b-256 e632cba911b907e9add4230a3f7d004950871ede15ec96fe3448fcff735bc96a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page