Skip to content

Instantly share code, notes, and snippets.

@nkint
nkint / vec3-mat4.py
Created November 19, 2019 17:35
numpy - vec3 transformation mat4
import numpy as np
def cube_geometry(x, y, z):
xSize = x / 2
ySize = y / 2
zSize = z / 2
positions = np.array([
[-xSize, ySize, zSize], [xSize, ySize, zSize],
[-xSize, -ySize, zSize], [xSize, -ySize, zSize],
@nkint
nkint / gist:6b9ecbfc85af06a787163dfb027c71a4
Created December 17, 2019 13:27
Circle as SVG path with A
function circlePath(cx=0, cy=0, r=10) {
// https://stackoverflow.com/a/10477334/433685
const startPointX = - r
const startPointY = 0
return `M ${cx} ${cy}
m ${startPointX}, ${startPointY}
a ${r},${r} 0 1,0 ${(r * 2)},0
@nkint
nkint / pil-on-the-fly.py
Last active May 7, 2021 09:54
Flask return numpy array on the fly
#!/usr/bin/env python3
from io import BytesIO
import numpy as np
import numpy.linalg as linalg
import scipy.interpolate as interpolate
from PIL import Image
from flask import Flask, send_file, escape, request
app = Flask(__name__)
import { Type, typedArray } from '@thi.ng/api'
import { MemPool } from '@thi.ng/malloc'
import { Vec } from '@thi.ng/vectors'
import { computeNewData } from './buffers'
const { numSliceRows, numSliceColumns, resolution } = globalState.deref()
const poolSize = numSliceRows * numSliceColumns * (resolution + 3)
const POOL = new MemPool({ size: poolSize })
console.log('poolSize of', { poolSize })
@nkint
nkint / gist:3564dcf246b09c2dfbe4e9d8af9a4713
Created January 31, 2020 18:16
Umbrella, wireframe lines from Vec[]
import {
comp,
juxtR,
map,
mapcat,
partition,
reducer,
transduce,
wrapSides,
} from '@thi.ng/transducers'
@nkint
nkint / fitIntoBounds3.ts
Created February 13, 2020 18:04
fitIntoBounds3
// 3d version of the 2d fitIntoBounds2
// https://github.com/thi-ng/umbrella/blob/feaf8d3f8c1dd3e9141a151b4e473423a6f62242/packages/geom/src/ops/fit-into-bounds.ts#L34
import { Vec, MAX3, MIN3, ReadonlyVec, neg, div4, div3, abs3, sub3, dist3 } from '@thi.ng/vectors';
import { concat, translation44, ReadonlyMat, scale44, mulV344 } from '@thi.ng/matrices';
import { bounds, centroid } from '@thi.ng/geom-poly-utils';
import { isArray, isNull } from '@thi.ng/checks';
/**
@nkint
nkint / index.ts
Last active July 26, 2020 13:20
umbrella webgl-to-pixel
/*
yarn add @thi.ng/hdom @thi.ng/pixel @thi.ng/hdom-canvas @thi.ng/geom @thi.ng/transducers @thi.ng/hdom-components @thi.ng/webgl @thi.ng/memoize @thi.ng/shader-ast @thi.ng/shader-ast-stdlib webgl-shadertoy
*/
import { renderOnce } from "@thi.ng/hdom";
import { PackedBuffer, canvas2d, ABGR8888, GRAY8 } from "@thi.ng/pixel";
import { canvas as hdomCanvas } from "@thi.ng/hdom-canvas";
import * as g from "@thi.ng/geom";
import { range, map, normRange } from "@thi.ng/transducers";
import { glCanvas } from "@thi.ng/webgl";
@nkint
nkint / gist:1120843ad5292ac4fb4887c97b606332
Created July 23, 2020 15:08
Dayjs group by time-range
var dayjs = require('dayjs')
var minMax = require('dayjs/plugin/minMax')
dayjs.extend(minMax)
const input = [
{time: '2020-07-23T11:30:00Z', value: 1},
{time: '2020-07-24T11:30:00Z', value: 1},
{time: '2020-07-05T11:30:00Z', value: 2},
{time: '2020-07-30T11:30:00Z', value: 3},
@nkint
nkint / index.ts
Created April 2, 2021 15:59
Map Index Values in typescript
export function mapIndexValues<Key extends string | number | symbol, ValueInput, ValueOutput>(
input: Record<Key, ValueInput>,
iteratee: (key: Key, val: ValueInput, index: number) => ValueOutput
): Record<Key, ValueOutput> {
const entries = Object.entries(input).map(([key, value], index) => [
key,
iteratee(key as Key, value as ValueInput, index)
]);
return Object.fromEntries(entries);
@nkint
nkint / isTruncated.tsx
Created May 4, 2021 13:48
react hook is truncated
import { useLayoutEffect, useState } from 'react';
/**
* Determine if the input DOM element is truncated by CSS (using ellipse for example)
* @param domElement
* @returns boolean
*/
export function isTruncated(domElement: Element): boolean {
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth
return domElement.scrollWidth > domElement.clientWidth;