Skip to content

Instantly share code, notes, and snippets.

View marmakoide's full-sized avatar

Devert Alexandre marmakoide

View GitHub Profile
@marmakoide
marmakoide / quickhull-2d.py
Created January 17, 2019 07:12
A nice, short compact 2d convex hull routine, implementing the Quickhull algorithm
import numpy
def quickhull_2d(S):
def process(P, a, b):
signed_dist = numpy.cross(S[P] - S[a], S[b] - S[a])
K = [i for s, i in zip(signed_dist, P) if s > 0 and i != a and i != b]
if len(K) == 0:
@marmakoide
marmakoide / circumsphere.py
Last active January 14, 2019 09:52
Computes the N-dimension circumsphere of M points, returning its center and its radius, assuming M <= N + 1
import numpy
def get_circumsphere(S):
U = S[1:] - S[0]
B = numpy.sqrt(numpy.sum(U ** 2, axis = 1))
U /= B[:,None]
C = numpy.dot(numpy.linalg.solve(numpy.inner(U, U), .5 * B), U)
return C + S[0], numpy.sqrt(numpy.sum(C ** 2))
@marmakoide
marmakoide / unit-simplex.py
Last active December 13, 2022 12:49
Computes the coordinates of the regular simplex inscribed in the unit sphere, in N-dimension
import numpy
def get_unit_simplex(N, dtype = float):
# Original algorithm from John Burkardt
A = numpy.full(N, (1. - numpy.sqrt(N + 1)) / N, dtype = dtype)
S = numpy.concatenate([A[:,None].T, numpy.identity(N, dtype = dtype)])
S -= numpy.mean(S, axis = 0)
S /= numpy.sqrt(2.)
return S
@marmakoide
marmakoide / README.md
Last active August 23, 2018 13:27
Test if a box is intersecting a disc. Works in N-dimensions.

Box/disc intersection test

This code sample demonstrates how to check if a box and a disc (in 3d, a sphere) intersects each other. The code works for 2d and 3d, and beyond, without any modifications.

The actual test is the function box_intersect_disc, the rest of the script is just to provide a visual demonstration of the test.

a sample of the script output

@marmakoide
marmakoide / README.md
Last active May 2, 2023 03:27
Returns N (approximatively) evenly spread points over the unit disc (aka. Vogel's method)

Golden dot spiral on the disc

This code sample demonstrates how to generates a set of points that covers a disc almost evenly, for any number of points. The trick is to use a Fermat's spiral and the golden angle.

42 points 256 points 999 points
dot spiral, 42 points dot spiral, 256 points dot spiral, 999 points
@marmakoide
marmakoide / vmf.py
Created May 11, 2018 14:01
Von Mises Fisher distribution in 3d ie. a spherical analog to an isotropic Gaussian distribution.
import numpy
'''
Pick a point uniformly from the unit circle
'''
def circle_uniform_pick(size, out = None):
if out is None:
out = numpy.empty((size, 2))
angle = 2 * numpy.pi * numpy.random.random(size)
@marmakoide
marmakoide / LICENSE.md
Last active November 29, 2025 20:19
Compute and display a Laguerre-Voronoi diagram (aka power diagram), only relying on a 3d convex hull routine. The Voronoi cells are guaranted to be consistently oriented.

MIT License

Copyright (c) 2021 Devert Alexandre

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

@marmakoide
marmakoide / plane-plane-intersection-3d.py
Created May 8, 2018 06:18
Compute the intersection (a line) between two planes in 3d
import numpy
def norm2(X):
return numpy.sqrt(numpy.sum(X ** 2))
def normalized(X):
return X / norm2(X)
'''
Returns the intersection, a line, between the plane A and B
@marmakoide
marmakoide / README.md
Last active May 30, 2021 08:02
Compute and display a Voronoi diagram, only relying on a 3d convex hull routine. The Voronoi cells are guaranted to be consistently oriented.

2d Voronoi diagrams

This code sample demonstrates how to compute a Voronoi diagram in 2d.

thumbnail

It works as following

  • Transform the points (called here lifting the points) from 2d to 3d.
@marmakoide
marmakoide / partition-set.py
Last active April 24, 2018 07:11
A sequence-like object that represents all the partitions of a set, allowing iteration and random access of the partitions and using very little memory
from collections import defaultdict
'''
Represents the sequence of all partitions of a set, by increasing number of
partition. This is a pseudo-sequence, each item of the sequence is generated
rather than stored.
'''
class partition_set(object):