Skip to content
Snippets Groups Projects
Select Git revision
  • 309c25e10bcef28df0b3928642b8bc63178a0213
  • master default protected
  • rednatco-v2
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • servers
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

properties.ts

Blame
  • misc.ts 1.67 KiB
    /**
     * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author Alexander Rose <alexander.rose@weirdbyte.de>
     */
    
    export const halfPI = Math.PI / 2;
    export const PiDiv180 = Math.PI / 180;
    
    export function degToRad(deg: number) {
        return deg * PiDiv180; // deg * Math.PI / 180
    }
    
    export function radToDeg(rad: number) {
        return rad / PiDiv180; // rad * 180 / Math.PI
    }
    
    export function isPowerOfTwo(x: number) {
        return (x !== 0) && (x & (x - 1)) === 0;
    }
    
    /** return the value that has the largest absolute value */
    export function absMax(...values: number[]) {
        let max = 0;
        let absMax = 0;
        for (let i = 0, il = values.length; i < il; ++i) {
            const value = values[i];
            const abs = Math.abs(value);
            if (abs > absMax) {
                max = value;
                absMax = abs;
            }
        }
        return max;
    }
    
    /** Length of an arc with angle in radians */
    export function arcLength(angle: number, radius: number) {
        return angle * radius;
    }
    
    /** Create an outward spiral of given `radius` on a 2d grid */
    export function spiral2d(radius: number) {
        let x = 0;
        let y = 0;
        const delta = [0, -1];
        const size = radius * 2 + 1;
        const halfSize = size / 2;
        const out: [number, number][] = [];
    
        for (let i = Math.pow(size, 2); i > 0; --i) {
            if ((-halfSize < x && x <= halfSize) && (-halfSize < y && y <= halfSize)) {
                out.push([x, y]);
            }
    
            if (x === y || (x < 0 && x === -y) || (x > 0 && x === 1 - y)) {
                [delta[0], delta[1]] = [-delta[1], delta[0]]; // change direction
            }
    
            x += delta[0];
            y += delta[1];
        }
        return out;
    }