Skip to content

cjccjj/simpler_3D_cube_drawing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

simpler 3D cube drawing

This shows a different, simpler way to draw a cube from vertices to edges in a routine.

Most code hard-codes the 12 edges, drawing them one by one. A cube has 8 vertices and 12 edges, so you can’t just loop 8 times to connect them. I find that really annoying.

     7-------6
    /|      /|
   4-------5 |
   | 3-----|-2
   |/      |/
   0-------1
// Bottom
for (int i = 0; i < 4; i++) {
    printf("%d - %d\n", vertices[i], vertices[(i+1)%4]);
}
// Top square
for (int i = 4; i < 8; i++) {
    printf("%d - %d\n", vertices[i], vertices[4 + (i+1)%4]);
}
// Vertical
for (int i = 0; i < 4; i++) {
    printf("%d - %d\n", vertices[i], vertices[i+4]);
}

Then I found the method below: you group the vertices into 4 even and 4 odd ones, and each even vertex connects only to 3 odd vertices, and vice versa.

     2-------7
    /|      /|
   3-------6 |
   | 5-----|-4
   |/      |/
   0-------1

In this setup, the 12 edges can be drawn in a single loop of 4, which I think is cleaner and nicer. It also reflects the fundamental math behind a cube.

for (int i = 0; i < 8; i += 2) {
    for (int j = i + 1; j < i + 7; j += 2) {
          draw_line(i, j % 8 ); 
    }
}

This was a side discovery while trying to render a 3D menu on Arduino.

Hardware:

  • Arduino Nano
  • 0.96 inch 12864 OLED

Libs:

  • Adafruite GFX
  • Adafruite ssd1306

Video demo here:

About

A simpler way to draw 3D cube edges from vertices

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages