Hur man rita Sweet 3D-grafik för LED kuber (7 / 9 steg)
Steg 7: 3D primitiver: kuber
Rita en kub följer en liknande logik till området.
Konturerna av en kub har åtta hörn, med tolv kanter ansluter dessa hörn. Precis som området tänker jag inte fylla i insidan av sfären, men snarare bara rita kanter och hörn. För att veta om att sätta kuben, behöver jag veta positionen för ett av hörnen och kantalängd. Jag kommer för att basera alla ståndpunkter från toppen tillbaka vänster vertex (visas i cyan i bilden ovan)
PVector [] topPoints = ny PVector [4].
PVector [] bottomPoints = ny PVector [4].
topPoints [0] = topLeft;
topPoints [1] = ny PVector (topLeft.x+side, topLeft.y, topLeft.z);
topPoints [2] = ny PVector (topLeft.x+side, topLeft.y+side, topLeft.z);
topPoints [3] = ny PVector (topLeft.x, topLeft.y+side, topLeft.z);
PVector bottomLeft = ny PVector (topLeft.x, topLeft.y, topLeft.z+side);
bottomPoints [0] = bottomLeft;
bottomPoints [1] = ny PVector (bottomLeft.x+side, bottomLeft.y, bottomLeft.z);
bottomPoints [2] = ny PVector (bottomLeft.x+side, bottomLeft.y+side, bottomLeft.z);
bottomPoints [3] = ny PVector (bottomLeft.x, bottomLeft.y+side, bottomLeft.z);
Rita tolv kanterna på kuben
för (int jag = 0; jag < 4; i ++)
{
drawLine (topPoints [i], bottomPoints [i], col);
drawLine (topPoints [i], topPoints [(i+1) %4], col);
drawLine (bottomPoints [i], bottomPoints [(i+1) %4], col);
}
nu dra hörnen. Jag tror att det ser trevligt att göra dessa i en annan färg än kanterna.
för (int jag = 0; jag < 4; i ++)
{
cube.setVoxel (topPoints [i], color(255,0,0));
cube.setVoxel (bottomPoints [i], color(255,0,0));
}
Komplett funktion och arbetande koden är i exemplet kuber i bildbehandling bibliotek.