I did some house cleaning (refactoring) and post a new version
Chrisir
// Demo for a **graphical representation** of a list of PVectors that represent the directions you can go in a 3D grid (like L/R and up/down and diagonal etc.):
// in the upper etage 9 directions, in the lower etage 9 directions and in the same plane (middle etage) 8 directions. This gives you 9+9+8 = 26 directions (without vector 0,0,0 itself of course).
// cf. https://discourse.processing.org/t/3d-walker-in-a-3d-grid-cube-what-are-the-directions-he-can-go-to/24419
// (I used this in a 3D Tic-Tac-Toe Game to find all possible lines (of 3 cells) in the 3D-board (grid) you can **win** in (these indices / lines are NOT what is shown here).)
// auto-generated list of 3D PVectors for all possible directions in 3D (right, left, up, diagonal right downwards....)
PVector[] pvList = {
// new PVector ( 0, 0, 0 ),
new PVector ( 0, 0, 1 ),
new PVector ( 0, 0, -1 ),
new PVector ( 0, 1, 0 ),
new PVector ( 0, 1, 1 ),
new PVector ( 0, 1, -1 ),
new PVector ( 0, -1, 0 ),
new PVector ( 0, -1, 1 ),
new PVector ( 0, -1, -1 ),
new PVector ( 1, 0, 0 ),
new PVector ( 1, 0, 1 ),
new PVector ( 1, 0, -1 ),
new PVector ( 1, 1, 0 ),
new PVector ( 1, 1, 1 ),
new PVector ( 1, 1, -1 ),
new PVector ( 1, -1, 0 ),
new PVector ( 1, -1, 1 ),
new PVector ( 1, -1, -1 ),
new PVector ( -1, 0, 0 ),
new PVector ( -1, 0, 1 ),
new PVector ( -1, 0, -1 ),
new PVector ( -1, 1, 0 ),
new PVector ( -1, 1, 1 ),
new PVector ( -1, 1, -1 ),
new PVector ( -1, -1, 0 ),
new PVector ( -1, -1, 1 ),
new PVector ( -1, -1, -1 )
};
// old wrong version for comparison
PVector[] pvList_OLD = {
new PVector (0, 0, 1),
new PVector (0, 1, 0),
new PVector (1, 0, 0),
new PVector (1, 0, 1),
new PVector (1, 1, 0),
new PVector (0, 1, 1),
new PVector (1, 1, 1),
//---
new PVector (0, 0, -1),
new PVector (0, -1, 0),
new PVector (-1, 0, 0),
new PVector (-1, 0, -1),
new PVector (-1, -1, 0),
new PVector (0, -1, -1),
new PVector (-1, -1, -1)
};
// rotate the object
float angleRotate;
// -----------------------------------------------------------------------------------------------
void setup() {
size(1800, 900, P3D);
textSize(19);
textMode(SHAPE);
println("Number of directions: "+pvList.length);
} // func
void draw() {
background(0);
lights();
showListOfPVectors(pvList);
if (! keyPressed) // stop rotation with any key
angleRotate += 0.004;
decoration();
} // func
// -----------------------------------------------------------------------------------------------
void showListOfPVectors(PVector[] pvList_) {
// prepare scene
pushMatrix();
translate(width/2, height/2, 0);
rotateX(-0.2992);
rotateY(angleRotate); // rotate animation
// loop over the list
int i=0;
for (PVector pv : pvList_) {
showThisVector(pv, i); // show one PVector
i++;
}//for
// reset
popMatrix();
}
void showThisVector(PVector pv_, int intNumber_) {
float lengthLine = 188;
// calc color for this vector
float amt = map(intNumber_,
0, pvList.length,
0, 1);
color colLine3d = lerpColor(color(255), color(255, 0, 0), amt);
// draw pv_ as a 3D line
line3D(
pv_.copy().mult(8),
pv_.copy().mult(lengthLine),
16,
colLine3d,
intNumber_);
}
void line3D(PVector pv1_,
PVector pv2_,
float weight_,
color color_,
int intNumber_) {
// draws a 3D-Line from pv1_ TO pv2_ with thickness weight_ and color color_
// and shows a number intNumber_ at its end.
// Was called drawLine; programmed by James Carruthers, modified,
// see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9
PVector v1 = new PVector(pv2_.x-pv1_.x, pv2_.y-pv1_.y, pv2_.z-pv1_.z);
float rho = sqrt(v1.x*v1.x + v1.y*v1.y + v1.z*v1.z);
float phi = acos(v1.z/rho);
float the = atan2(v1.y, v1.x);
v1.mult(0.5);
// The line
pushMatrix();
translate(pv1_.x, pv1_.y, pv1_.z);
translate(v1.x, v1.y, v1.z);
rotateZ(the);
rotateY(phi);
// noStroke();
stroke(0);
fill(color_);
// box(weight,weight,p1.dist(p2)*1.2);
box(weight_, weight_, pv1_.dist(pv2_)*1.0);
popMatrix();
// The text (optional)
pushMatrix();
float yAdd = -21;// for the 2 upper etages we push intNumber_ up by 21
if (pv2_.y > height/2.0-300) {
yAdd = 23; // for the lower etage we push intNumber_ down by 23
}
translate( pv2_.x+21, pv2_.y + yAdd, pv2_.z-21);
rotateY(-angleRotate);// rotate towards camera
fill(255);
text(intNumber_, 0, 0);
popMatrix();
}
// -----------------------------------------------------------------------------------------------
void decoration() {
stroke(100);
fill(255, 0, 0);
pushMatrix();
translate(100, 100, 0);
rotateZ(angleRotate);
box(60);
popMatrix();
pushMatrix();
translate(400, 100, 0);
rotateX(angleRotate);
fill(0, 0, 255);
box(60);
popMatrix();
pushMatrix();
translate(700, 100, 0);
rotateY(angleRotate);
box(60);
popMatrix();
pushMatrix();
translate(1000, 100, -30);
noStroke();
sphere(50);
popMatrix();
}
//