Licence
MIT
Version
1.2.0
Deps
1
Size
839 kB
Vulns
0
Weekly
0
geometrik
Geometry classes and utils.
npm i geometrik
|
pnpm add geometrik
|
yarn add geometrik
|
Examples
# rect-collision
# view source
example/rect-collision.ts import { Point, Rect } from 'geometrik'
let randomSeed = 15151523
function random(x = 1) {
return (
(Math.sin(
10e10 * randomSeed
* Math.cos(5e10 * ++randomSeed)
)
+ Math.sin(
10e10 * randomSeed
* Math.sin(5e10 * ++randomSeed)
)
+ Math.sin(
10e10 * randomSeed
* Math.cos(5e10 * ++randomSeed)
))
/ 3
* 0.5 + 0.5
)
* x
}
const view = new Point(400, 400)
const minSize = 10
const scale = 50
const rects = Array.from(
{ length: 20 },
() =>
new Rect(
random(view.x),
random(view.y),
minSize + random(scale),
minSize + random(scale)
) as Rect & { div: HTMLDivElement }
)
for (const r of rects) {
const div = document.createElement('div')
;(r as any).div = div
const color = `hsl(${random(360)}, 50%, 50%)`
Object.assign(div.style, r.toStyle(), { position: 'absolute' })
div.style.backgroundColor = color
document.body.appendChild(div)
}
const h = rects[0]
window.onpointermove = e => {
h.x = e.pageX - h.width / 2
h.y = e.pageY - h.height / 2
for (const r of rects) {
if (h === r) continue
if (h.intersectsRect(r)) {
const p = h.collisionResponse(r)
// const c2 = r.intersectPoint(h)
h.translateSelf(p.scale(0.5))
r.translateSelf(p.scale(-0.5))
// r.setPosition(c2.add(h.center).sub(r.size.scale(0.5)))
// r.translateSelf(c.scale(-0.5))
Object.assign(r.div.style, r.toStylePosition())
// h.setPosition(h.touchPoint(r))
}
}
Object.assign(h.div.style, h.toStylePosition())
}
# web
# view source
example/web.ts import * as geometrik from 'geometrik'
console.log(new geometrik.Rect())
API
# Line
# Matrix
# Point
# add(this, x)
# addSelf(this, x)
# clampSelf(this, min, max)
# draw(this, color, position)
# interpolate(this, other, t)
# interpolateSelf(this, other, t)
# multiply(this, x)
# multiplySelf(this, x)
# normalize(this, other)
# normalizeSelf(this, other)
# scale(this, x)
# scaleLinear(this, x)
# scaleLinearSelf(this, x)
# scaleSelf(this, x)
# sub(this, x)
# subSelf(this, x)
# translate(this, x)
# translateSelf(this, x)
# zoomLinear(this, x)
# zoomLinearSelf(this, x)
# Polygon
# Rect
# add(this, x)
# addSelf(this, x)
# draw(this, color, position)
# interpolate(this, other, t)
# interpolateSelf(this, other, t)
# multiply(this, x)
# multiplySelf(this, x)
# normalize(this, matrix)
# normalizeSelf(this, matrix)
# scale(this, x)
# scaleLinear(this, x)
# scaleLinearSelf(this, x)
# scaleSelf(this, x)
# sub(this, x)
# subSelf(this, x)
# translate(this, x)
# translateSelf(this, x)
# zoomLinear(this, x)
# zoomLinearSelf(this, x)
# Shape
# cardinal(data, closed, tension)
Credits
- everyday-types by stagas – Everyday utility types
Contributing
All contributions are welcome!