Étape 7: primitives 3D: cubes
Dessin d’un cube suit une logique semblable à la sphère.
L’esquisse d’un cube a huit angles, avec douze arêtes reliant les angles. À l’instar de la sphère, je ne vais pas à remplir l’intérieur de la sphère, mais au contraire, il suffit de dessiner les bords et les sommets. Pour savoir où mettre le cube, j’ai besoin de connaître la position de l’un des sommets et la longueur d’arête. Je vais à la base de toutes les positions hors de la partie supérieure arrière gauche vertex (montré en cyan dans l’image ci-dessus)
PVector[] topPoints=new PVector[4]; PVector[] bottomPoints=new PVector[4]; topPoints[0]=topLeft; topPoints[1]=new PVector(topLeft.x+side, topLeft.y, topLeft.z); topPoints[2]=new PVector(topLeft.x+side, topLeft.y+side, topLeft.z); topPoints[3]=new PVector(topLeft.x, topLeft.y+side, topLeft.z); PVector bottomLeft=new PVector(topLeft.x, topLeft.y, topLeft.z+side); bottomPoints[0]=bottomLeft; bottomPoints[1]=new PVector(bottomLeft.x+side, bottomLeft.y, bottomLeft.z); bottomPoints[2]=new PVector(bottomLeft.x+side, bottomLeft.y+side, bottomLeft.z); bottomPoints[3]=new PVector(bottomLeft.x, bottomLeft.y+side, bottomLeft.z); //draw the twelve edges of the cube for (int i=0; i<4; i++) { drawLine(topPoints[i], bottomPoints[i], col); drawLine(topPoints[i], topPoints[(i+1)%4], col); drawLine(bottomPoints[i], bottomPoints[(i+1)%4], col); } //now draw the vertices. I think that it looks nice to make these in a different color than the edges. for (int i=0; i<4; i++) { cube.setVoxel(topPoints[i], color(255,0,0)); cube.setVoxel(bottomPoints[i], color(255,0,0)); }
La fonction complète et le code de travail sont dans l’exemple de Cubes dans la bibliothèque de traitement.