Skip to content

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

Notifications You must be signed in to change notification settings

llafuente/js-2dmath

Repository files navigation

js-2dmath Build Status

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
  • did I miss anything useful?

Performance? HOW?/TIPS

  • Avoid new
  • Use arrays instead of objects, this is huge performance boost!
  • Avoid creating unnecessary variables (reuse intermediate variables) only "create" & "clone" methods must instance new variables.
  • Cache every function call to a single variable. example: Vec2.add -> vec2_add
  • If access an multi-dimensional array in a loop, cache the array access. for(i...) carr=arr[i]; carr[X]

I'm sure I miss some of my own performance tips, PR if you find any error or find a better way!

See some performance test that prove it.

Browser & Debug distributions

grunt dist
# creates:
# - dist/js-2dmath-browser.js
# - debug/js-2dmath-browser-debug.js
# - docs/*.markdown

Why there is a debug dist?

Debug distribution use falafel to add assertions based on the documentation.

Force to use valid sanitized inputs (Arrays, not NaN, not undefined, not null) to every function.

What can you do with js-2dmath?

See some examples that we use as tests :)

API

The documentation is autogenerated with falafel see dist.js for more fun! :)

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

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

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •