Skip to content

Instantly share code, notes, and snippets.

View marmakoide's full-sized avatar

Devert Alexandre marmakoide

View GitHub Profile
@marmakoide
marmakoide / README.md
Last active May 12, 2018 05:56
Returns N (approximatively) evenly spread points over the unit sphere

Golden dot spiral on the sphere

This code sample demonstrates how to generates a set of points that covers a sphere almost evenly, for any number of points. The trick is to use a 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 / trilateration-3d.py
Last active April 12, 2018 09:09
Trilateration in 3d ie. computing the location of the intersection points of 3 spheres
import numpy
def norm2(X):
return numpy.sqrt(numpy.sum(X ** 2))
def trilateration_3d(A, B, C, rA, rB, rC):
# Find the two intersection points of three spheres
# The routine returns D, W, h_sqr, and the two solutions are D - sqrt(h_sqr) * W and D + sqrt(h_sqr) * W
# The points are at a distance 2 * sqrt(h_sqr) from each other, and both on the line of origin D and direction W
# If h_sqr is negative, there's no solutions to the problem
@marmakoide
marmakoide / pick-orthogonal-vector.py
Last active December 13, 2022 13:03
Pick a 3d unit vector orthogonal to an other 3d unit vector
import numpy
def norm2(X):
return numpy.sqrt(numpy.sum(X ** 2))
def normalized(X):
return X / norm2(X)
def pick_orthogonal_vector(U):
# Original idea from Sam Hocevar
@marmakoide
marmakoide / trilateration-2d.py
Last active April 12, 2018 09:10
Trilateration in 2d ie. computing the location of the intersection points of 2 circles
import numpy
def norm2_sqr(X):
return numpy.sum(X ** 2)
def perp(X):
return numpy.array((-X[1], X[0]))
def trilateration_2d(A, B, rA, rB):
# Find the two intersection points of two circles
@marmakoide
marmakoide / circumcircle-2d.py
Last active April 13, 2018 11:55
Computes the circumcircle of a triangle (assuming 2d coordinates), returning its center and its radius
import numpy
def norm2(X):
return numpy.sqrt(numpy.sum(X ** 2))
def get_circumcircle(A, B, C):
U, V = B - A, C - A
U_norm, V_norm = norm2(U), norm2(V)
U /= U_norm
V /= V_norm
@marmakoide
marmakoide / disc-picking-2d.py
Last active April 17, 2018 06:50
Point picking (ie. uniform sampling) of the unit disc (ie. a disc centered at the origin and radius equals to 1)
import numpy
'''
Pick N points uniformly from the unit disc
This sampling algorithm does not use rejection sampling.
'''
def disc_uniform_pick(N):
angle = (2 * numpy.pi) * numpy.random.random(N)
out = numpy.stack([numpy.cos(angle), numpy.sin(angle)], axis = 1)
out *= numpy.sqrt(numpy.random.random(N))[:,None]
@marmakoide
marmakoide / sphere-picking-3d.py
Created April 17, 2018 07:08
Point picking (ie. uniform sampling) of the unit sphere (ie. a sphere centered at the origin and radius equals to 1)
import numpy
'''
Pick N points uniformly from the unit disc
This sampling algorithm does not use rejection sampling.
'''
def disc_uniform_pick(N):
angle = (2 * numpy.pi) * numpy.random.random(N)
out = numpy.stack([numpy.cos(angle), numpy.sin(angle)], axis = 1)
out *= numpy.sqrt(numpy.random.random(N))[:,None]
@marmakoide
marmakoide / triangle-surface-area.py
Last active June 8, 2020 11:11
Returns a triangle's surface area from the length of its 3 sides, in a numerically stable fashion
'''
Compute a triangle's area from its edges length with Heron's formula
Numerically stable
'''
def triangle_area_from_lengths(a, b, c):
# Sort input
c, b, a = sorted((a, b, c))
# Compute Heron's formula
ret = (a + (b + c)) * (c - (a - b)) * (c + (a - b)) * (a + (b - c))
@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):
@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.