How to use the delaunator.from function in delaunator

To help you get started, we’ve selected a few delaunator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / delaunator / delaunator-tests.ts View on Github external
import Delaunator from 'delaunator';

// Zipped points [x0, y0, x1, y1, ...]
const zippedPoints = [168, 180, 168, 178, 168, 179, 168, 181, 168, 183, 167, 183, 167, 184];
const zipped = new Delaunator(zippedPoints);

// Default [x, y]
const defaultPoints = [[168, 180], [168, 178], [168, 179], [168, 181], [168, 183], [167, 183], [167, 184]];
const d = Delaunator.from(defaultPoints);

// Custom getX & getY
interface CustomPoint {
    x: number;
    y: number;
}
const customPoints = [{ x: 168, y: 180 }, { x: 168, y: 178 }, { x: 168, y: 179 }, { x: 168, y: 181 }, { x: 168, y: 183 }, { x: 167, y: 183 }, { x: 167, y: 184 }];

const getX = (point: CustomPoint) => point.x;
const getY = (point: CustomPoint) => point.y;

Delaunator.from(customPoints, point => point.x, point => point.y);
Delaunator.from(customPoints, getX, getY);

// To get the coordinates of all triangles, use:
const triangles = d.triangles; // $ExpectType Uint32Array
github DefinitelyTyped / DefinitelyTyped / types / delaunator / delaunator-tests.ts View on Github external
// Default [x, y]
const defaultPoints = [[168, 180], [168, 178], [168, 179], [168, 181], [168, 183], [167, 183], [167, 184]];
const d = Delaunator.from(defaultPoints);

// Custom getX & getY
interface CustomPoint {
    x: number;
    y: number;
}
const customPoints = [{ x: 168, y: 180 }, { x: 168, y: 178 }, { x: 168, y: 179 }, { x: 168, y: 181 }, { x: 168, y: 183 }, { x: 167, y: 183 }, { x: 167, y: 184 }];

const getX = (point: CustomPoint) => point.x;
const getY = (point: CustomPoint) => point.y;

Delaunator.from(customPoints, point => point.x, point => point.y);
Delaunator.from(customPoints, getX, getY);

// To get the coordinates of all triangles, use:
const triangles = d.triangles; // $ExpectType Uint32Array
const halfedges = d.halfedges; // $ExpectType Int32Array
const hull = d.hull; // $ExpectType Uint32Array
const coords = d.coords; // $ExpectType ArrayLike | Float64Array
const coordinates: number[][][] = [];
for (let i = 0; i < triangles.length; i += 3) {
    coordinates.push([
        defaultPoints[triangles[i]],
        defaultPoints[triangles[i + 1]],
        defaultPoints[triangles[i + 2]]
    ]);
}

// Or use Delaunator.coords (but coords is a flat array in the form of [x0, y0, x1, y1, ...])
github DefinitelyTyped / DefinitelyTyped / types / delaunator / delaunator-tests.ts View on Github external
// Default [x, y]
const defaultPoints = [[168, 180], [168, 178], [168, 179], [168, 181], [168, 183], [167, 183], [167, 184]];
const d = Delaunator.from(defaultPoints);

// Custom getX & getY
interface CustomPoint {
    x: number;
    y: number;
}
const customPoints = [{ x: 168, y: 180 }, { x: 168, y: 178 }, { x: 168, y: 179 }, { x: 168, y: 181 }, { x: 168, y: 183 }, { x: 167, y: 183 }, { x: 167, y: 184 }];

const getX = (point: CustomPoint) => point.x;
const getY = (point: CustomPoint) => point.y;

Delaunator.from(customPoints, point => point.x, point => point.y);
Delaunator.from(customPoints, getX, getY);

// To get the coordinates of all triangles, use:
const triangles = d.triangles; // $ExpectType Uint32Array
const halfedges = d.halfedges; // $ExpectType Int32Array
const hull = d.hull; // $ExpectType Uint32Array
const coords = d.coords; // $ExpectType ArrayLike | Float64Array
const coordinates: number[][][] = [];
for (let i = 0; i < triangles.length; i += 3) {
    coordinates.push([
        defaultPoints[triangles[i]],
        defaultPoints[triangles[i + 1]],
        defaultPoints[triangles[i + 2]]
    ]);
}
github qrohlf / trianglify / src / trianglify.js View on Github external
const yColors = opts.yColors === 'match'
    ? xColors
    : processColorOpts(opts.yColors)

  console.log(xColors, yColors)

  const xScale = chroma.scale(xColors).mode(opts.colorSpace)
  const yScale = chroma.scale(yColors).mode(opts.colorSpace)

  // Our next step is to generate a pseudo-random grid of {x, y , z} points,
  // (or to simply utilize the points that were passed to us)
  const points = opts.points || getPoints(opts, rand)
  // window.document.body.appendChild(debugRender(opts, points))

  // Once we have the points array, run the triangulation:
  var geomIndices = Delaunator.from(points).triangles

  // And generate geometry and color data:

  // use a different randomizer for the color function so that swapping
  // out color functions, etc, doesn't change the pattern geometry itself
  const colorRand = seedrandom(opts.seed ? opts.seed + 'salt' : undefined)
  const polys = []
  for (let i = 0; i < geomIndices.length; i += 3) {
    const vertices = [
      points[geomIndices[i]],
      points[geomIndices[i + 1]],
      points[geomIndices[i + 2]]
    ]

    const {width, height} = opts
    const norm = num => Math.max(0, Math.min(1, num))
github Financial-Times / x-dash / packages / x-logo / index.js View on Github external
this.random = seedrandom(props.seed);
		this.poisson = new Poisson([150, 150], 100 / this.props.density, 100, 30, this.random);
		this.points = this.poisson.fill()
			.map(([x, y]) => [x - 25, y - 25])
			.filter(point => pointInPolygon(point, this.thickerX));

		const hue = this.random() * 360;
		this.hues = [
			hue,
			hue + props.hueShift,
			hue - props.hueShift,
			hue + props.hueShift * 2,
		];

		this.triangles = Delaunay.from(this.points).triangles;

		this.triangleColours = range(this.triangles.length / 3).map(i =>
			this.getColor(this.points[this.triangles[i * 3]])
		);
	}
github anvaka / vs / src / lib / layout / getDelaunayGraph.js View on Github external
export default function getDelaunayGraph(vertices) {
  const delaunay = Delaunator.from(vertices);
  const triangles = delaunay.triangles;
  const triangulationGraph = createGraph()
  vertices.forEach(v => {
    triangulationGraph.addNode(v.id, v);
  });

  for (let i = triangles.length; i;) {
    --i
    const first = vertices[triangles[i]]
    --i
    const second = vertices[triangles[i]]
    --i
    const third = vertices[triangles[i]]

    addTriangulationLink(first.id, second.id, triangulationGraph)
    addTriangulationLink(second.id, third.id, triangulationGraph)

delaunator

An incredibly fast JavaScript library for Delaunay triangulation of 2D points

ISC
Latest version published 10 months ago

Package Health Score

83 / 100
Full package analysis