Chrisir
Full Member
Offline
Posts: 207
Germany
Re: Platonic solids - 3d Cartesian coordinates
Reply #4 - Apr 25th , 2010, 9:18am
I can now diplay the first and the last solid.
Chrisir
// if you're using opengl then all the platonic solids (and more)
// are available via glut anyway.
// http://www.cs.duke.edu/courses/fall09/cps124/resources/jogldoc/com/sun/opengl/util/gl2/GLUT.html
// read http://en.wikipedia.org/wiki/Tetrahedron and related
float x;
float y;
color colRed = color(255, 0,0);
color colGreen = color(0, 255, 0);
color colBlue = color(0,0,255);
color colWhite = color(255,255,255);
color colBlack = color(0,0,0);
color colYellow = color(255,255,0);
/*
float _ = 1.618033;
float __ = 0.618033;
PVector[] dodecahedron = {
new PVector(0, __, _),
new PVector(0, __, -_),
new PVector(0, -__, _),
new PVector(0, -__, -_),
new PVector(_, 0, __),
new PVector(_, 0, -__),
new PVector(-_, 0, __),
new PVector(-_, 0, -__),
new PVector(__, _, 0),
new PVector(__, -_, 0),
new PVector(-__, _, 0),
new PVector(-__, -_, 0),
new PVector(1, 1, 1),
new PVector(1, 1, -1),
new PVector(1, -1, 1),
new PVector(1, -1, -1),
new PVector(-1, 1, 1),
new PVector(-1, 1, -1),
new PVector(-1, -1, 1),
new PVector(-1, -1, -1)
};
*/
// tetrahedron
PVector[] tetrahedron = {
new PVector( 1, 1, 1 ),
new PVector(-1, -1, 1 ),
new PVector(-1, 1,-1 ),
new PVector( 1, -1, -1 ) };
final int FACES = 12; // number of faces
final int VERTICES = 5; // VERTICES per face
final float A = 1.618033989; // (1 * sqr(5) / 2) - wikipedia
final float B = 0.618033989; // 1 / (1 * sqr(5) / 2) - wikipedia
PVector[] vert = new PVector[20]; // list of vertices
int[][] faces = new int[FACES][VERTICES]; // list of faces (joining vertices)
// ==================================================
void setup(){
size(1000,700,P3D);
camera(0,0,10,
0,0,0,
0, 1, 0);
vert[ 0] = new PVector(1, 1, 1);
vert[ 1] = new PVector(1, 1, -1);
vert[ 2] = new PVector(1, -1, 1);
vert[ 3] = new PVector(1, -1, -1);
vert[ 4] = new PVector(-1, 1, 1);
vert[ 5] = new PVector(-1, 1, -1);
vert[ 6] = new PVector(-1, -1, 1);
vert[ 7] = new PVector(-1, -1, -1);
vert[ 8] = new PVector(0, B, A);
vert[ 9] = new PVector(0, B, -A);
vert[10] = new PVector(0, -B, A);
vert[11] = new PVector(0, -B, -A);
vert[12] = new PVector(B, A, 0);
vert[13] = new PVector(B, -A, 0);
vert[14] = new PVector(-B, A, 0);
vert[15] = new PVector(-B, -A, 0);
vert[16] = new PVector(A, 0, B);
vert[17] = new PVector(A, 0, -B);
vert[18] = new PVector(-A, 0, B);
vert[19] = new PVector(-A, 0, -B);
faces[ 0] = new int[] {
0, 16, 2, 10, 8 };
faces[ 1] = new int[] {
0, 8, 4, 14, 12 };
faces[ 2] = new int[] {
16, 17, 1, 12, 0 };
faces[ 3] = new int[] {
1, 9, 11, 3, 17 };
faces[ 4] = new int[] {
1, 12, 14, 5, 9 };
faces[ 5] = new int[] {
2, 13, 15, 6, 10 };
faces[ 6] = new int[] {
13, 3, 17, 16, 2 };
faces[ 7] = new int[] {
3, 11, 7, 15, 13 };
faces[ 8] = new int[] {
4, 8, 10, 6, 18 };
faces[ 9] = new int[] {
14, 5, 19, 18, 4 };
faces[10] = new int[] {
5, 19, 7, 11, 9 };
faces[11] = new int[] {
15, 7, 19, 18, 6 };
}
void draw(){
background(0);
stroke(255);
tetrahedron () ;
dodecahedron ();
}
void tetrahedron () {
pushMatrix();
translate (5.5,0,0);
x = map (mouseX,0,width,0,2*PI);
rotateY(x);
y = map (mouseY,0,height,0,2*PI);
rotateX(y);
fill(colRed);
ShapeManager(0,1,2);
fill(colGreen);
ShapeManager(1,2,3);
fill(colBlue);
ShapeManager(2,3,0);
fill(colYellow);
ShapeManager(1,3,0);
popMatrix();
}
void ShapeManager (int A, int B, int C) {
float p = 1.1;
int i = 0;
beginShape(); // QUAD
i=A;
vertex(tetrahedron[i].x*p,tetrahedron[i].y*p,tetrahedron[i].z*p);
i=B;
vertex(tetrahedron[i].x*p,tetrahedron[i].y*p,tetrahedron[i].z*p);
i=C;
vertex(tetrahedron[i].x*p,tetrahedron[i].y*p,tetrahedron[i].z*p);
endShape(CLOSE); // CLOSE
}
void dodecahedron () {
pushMatrix();
x = map (mouseX,0,width,0,2*PI);
rotateY(x);
y = map (mouseY,0,height,0,2*PI);
rotateX(y);
for (int i = 0; i < FACES; i = i+1) {
fill(map(i,0,FACES,0,255));
beginShape();
for (int i2 = 0; i2 < VERTICES; i2 = i2+1) {
vertex(vert[faces[i][i2]].x,vert[faces[i][i2]].y,vert[faces[i][i2]].z);
} // for
endShape(CLOSE);
} // for
popMatrix();
} // func