Skip to content

Instantly share code, notes, and snippets.

@companje
Last active November 23, 2019 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/8d4ab47db6f4d13a2083da487f6eed1b to your computer and use it in GitHub Desktop.
Save companje/8d4ab47db6f4d13a2083da487f6eed1b to your computer and use it in GitHub Desktop.
Distributed points on Dome / Sphere
import org.processing.wiki.triangulate.*;
PShape dome;
float w=1200, h=1200, wd2=w/2, hd2=h/2, wdh=w/h, cx=wd2, cy=hd2, r=hd2, d=h;
float distToCam = 1200; //1040; //fisheye=1900, mirror=1040;
float globeScaler = 1.02;
ArrayList<PVector> points;
ArrayList<Triangle> triangles;
void settings() {
fullScreen(P3D);
}
void setup() {
surface.setSize(1200, 1200);
surface.setLocation(360, 0);
points = distributePointsOnDome(hd2); //half sphere
triangles = Triangulate.triangulate(points);
}
void draw() {
background(0);
perspective(atan(r/distToCam)*2, 1, distToCam, 10000);
camera(0, 0, -distToCam, 0, 0, 0, 0, 1, 0);
scale(-1, 1, 1);
//scale(globeScaler);
background(0);
stroke(255, 150);
noFill();
beginShape(TRIANGLES);
for (Triangle t : triangles) {
fill(t.p1.copy().normalize().div(2).add(.5, .5, .5).setMag(255));
vertex(t.p1);
fill(t.p2.copy().normalize().div(2).add(.5, .5, .5).setMag(255));
vertex(t.p2);
fill(t.p3.copy().normalize().div(2).add(.5, .5, .5).setMag(255));
vertex(t.p3);
}
endShape();
for (PVector v : points) {
pushMatrix();
orientateOnSphere(v);
fill(255);
noStroke();
hint(DISABLE_DEPTH_TEST);
hint(DISABLE_OPTIMIZED_STROKE);
ellipse(0, 0, 30, 30);
PVector s = screen();
popMatrix();
if (v.z==0) { //make more visible!
ellipse(lerp(s.x-wd2, 0, .025), lerp(s.y-hd2, 0, .025), 50, 50);
}
}
}
ArrayList<PVector> distributePointsOnDome(float radius) {
ArrayList<PVector> v = new ArrayList();
int n[] = {1, 6, 12, 24, 24, 24, 24};
int res = n.length;
for (int i=0; i<res; i++) {
float r = sin(float(i)/(res-1)*HALF_PI) * radius;
for (int j=0; j<n[i]; j++) {
float a = float(j)/n[i] * TWO_PI;
float x = cos(a) * r;
float y = sin(a) * r;
float z = r>=radius ? 0 : sqrt(radius*radius-(x*x)-(y*y));
v.add(new PVector(x, y, z));
}
}
return v;
}
@companje
Copy link
Author

sphere-distribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment