Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/llafuente/js-2dmath

Fast 2d geometry math: Vector2, Rectangle, Circle, Matrix2x3 (2D transformation), Circle, BoundingBox, Line2, Segment2, Intersections, Distances, Transitions (animation/tween), Noise, Random numbers
https://github.com/llafuente/js-2dmath

Last synced: about 1 month ago
JSON representation

Fast 2d geometry math: Vector2, Rectangle, Circle, Matrix2x3 (2D transformation), Circle, BoundingBox, Line2, Segment2, Intersections, Distances, Transitions (animation/tween), Noise, Random numbers

Lists

README

        

# js-2dmath [![Build Status](https://secure.travis-ci.org/llafuente/js-2dmath.png?branch=master)](http://travis-ci.org/llafuente/js-2dmath)

Fast 2d geometry math: Vector2, Rectangle, Circle, Matrix2x3 (2D transformation), Circle, BoundingBox, Line2, Segment2, Intersections, Distances, Transitions (animation/tween), Noise, Random numbers.

So the objective is "**Be fast**"

## Help needed / TODO LIST

* API completeness
* Testing
* Use falafel/esprima to create an asm.js build
* More Numerical integrators
* AI: Path-finding, Steer, Backtracking
* IK: FABRIK
* Minkowski distance, euclidean, Manhattan
* Beizer math
* Serialization / De-serialization
* did I miss anything useful?

## Performance

Performance is based on good practices.

* Avoid **new**
* Use arrays instead of objects, this is huge performance boost!
* Avoid creating unnecessary variables (reuse intermediate variables) only `create` & `clone` methods should create new variables.
* Cache every function call to a single variable. example: `Vec2.add` => `vec2_add`, even `Math.*`
* If access a multi-dimensional array in a loop, cache the array access. `for(i...) carr=arr[i]; carr[X]`
* Do not use `forEach`, `map`, `every` etc. or other looping method that require `apply`/`call` usage, both are costly.

See some [performance test](https://github.com/llafuente/js-2dmath/blob/master/js-performance-tests.markdown) that prove it.

[funlinify](https://github.com/llafuente/funlinify) It's a library that do function inline expansion for javascript. It's in early stage but it works perfectly for our usage here.

Obviously I ignore myself in some parts of this library. Feel free to issue me :)

## Grunt

```bash
npm install -g grunt
npm install -g grunt-cli
```

### grunt dist

Create distribution packages using [browserify](https://github.com/substack/node-browserify) and documentation.

* debug: *debug/js-2dmath-browser-debug.js*
* [argumentify](https://github.com/llafuente/argumentify) Assert on invalid arguments to properly debug your app.

* dist: *dist/js-2dmath-browser.js*
* [funlinify](https://github.com/llafuente/funlinify) inline function

* dist.min: *js-2dmath-browser.min.js*
* [funlinify](https://github.com/llafuente/funlinify) inline function
* [uglify](https://github.com/mishoo/UglifyJS)

### grunt watch

Watch every change and rebuild the distribution code.

## What can you do with js-2dmath?

See some examples.

* [Angles](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/angle.html)
* [Beizer](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/beizer.html)
* [Circle](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/circle.html)
* [Intersections](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/intersections.html)
* [line2](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/line2.html)
* [Matrix23](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/matrix23.html)
* [Segment2](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/segment2.html)
* [Transitions](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/transitions.html)
* [Triangle](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/triangle.html)
* [Vec2 collisions](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/vec2-collisions.html)
* [Vec2](http://htmlpreview.github.io/?https://github.com/llafuente/js-2dmath/blob/master/test/vec2.html)

## API

The documentation is autogenerated with [falafel](https://github.com/substack/node-falafel) see dist.js for more fun! :)

* [Vec2](https://github.com/llafuente/js-2dmath/blob/master/docs/vec2.markdown)
* [Line2](https://github.com/llafuente/js-2dmath/blob/master/docs/line2.markdown)
* [Segment2](https://github.com/llafuente/js-2dmath/blob/master/docs/segment2.markdown)
* [Rectangle](https://github.com/llafuente/js-2dmath/blob/master/docs/rectangle.markdown)
* [AABB2](https://github.com/llafuente/js-2dmath/blob/master/docs/aabb2.markdown)
* [Circle](https://github.com/llafuente/js-2dmath/blob/master/docs/circle.markdown)
* [Matrix22](https://github.com/llafuente/js-2dmath/blob/master/docs/matrix22.markdown)
* [Matrix23](https://github.com/llafuente/js-2dmath/blob/master/docs/matrix23.markdown)
* [Polygon](https://github.com/llafuente/js-2dmath/blob/master/docs/polygon.markdown)
* [Beizer](https://github.com/llafuente/js-2dmath/blob/master/docs/beizer.markdown)
* [Triangle](https://github.com/llafuente/js-2dmath/blob/master/docs/triangle.markdown)
* [Intersection](https://github.com/llafuente/js-2dmath/blob/master/docs/intersection.markdown)
* [Distance](https://github.com/llafuente/js-2dmath/blob/master/docs/distance.markdown)
* [Transitions](https://github.com/llafuente/js-2dmath/blob/master/docs/transitions.markdown)
* [Xorshift](https://github.com/llafuente/js-2dmath/blob/master/docs/xorshift.markdown)
* [Noise](https://github.com/llafuente/js-2dmath/blob/master/docs/noise.markdown)
* [Collision.SAT](https://github.com/llafuente/js-2dmath/blob/master/docs/collision-sat.markdown)
* [Collision.GJK](https://github.com/llafuente/js-2dmath/blob/master/docs/collision-gjk.markdown)
* [Collision.Resolve](https://github.com/llafuente/js-2dmath/blob/master/docs/collision-resolve.markdown)
* [Collision.Manifold](https://github.com/llafuente/js-2dmath/blob/master/docs/collision-manifold.markdown)

## FAQ

**How do i know a variable type?**

You can't, there is no *instanceof* or anything like that, everything are numbers/arrays.

I choose to keep track of all types using meaningful naming or enclose the variable in an object like

```js
var movable = {
body: Polygon.create(/*...*/), // could be a circle, change the type...
type: "polygon"
}
```