how to program a simple wireframe cube with 3d maths (without box) only lines or vertexes ??

i would like to know the code of a 3D wireframe cube, without using any help like box etc. only the 3D Translation matrixes...thanks a lot for helping. martinpla

Answers

  • draw diagrams.

    a cube has 8 corners. work out the coordinates for these (in 3 dimensions) and stick them in an array, call it vertices.

    then work out which corners are connected to which other corners. put this into another array, call it edges.

    in draw() loop over the edges array picking out the pairs of points. look up each of these points in the vertices table. draw a line from the one to the other.

  • edited August 2015
    // the points 
    PVector[] vertices = new PVector [9];
    
    // the connecting lines
    // e.g. edge 1 goes from vertices edgesFrom[0] to edgesTo[0]  
    int[] edgesFrom = new int [9]; 
    int[] edgesTo = new int [9];
    
    
    void setup() {
      size(900, 790, OPENGL);
      background(0);
      vertices[0]=new PVector (100, 100, 100);
    } // func 
    
    void draw() {
      background(0);
    
      //  use for loop i and line (x1,y1,z1, x2,y2,z2);  here
      //  with float x1 = vertices [ edgesFrom [i] ].x; 
    } // func 
    //
    
  • Hi Koogs, Hi Chrisir, would it be possible to get the whole code ? i am a beginner and i need more help in this topic...many thanks in advance, martinpla

  • Show your attempt as code please

  • One way to approach this problem is to:

    1. begin by creating your own square. can you solve that problem first? now you have 4 out of 8 points and 4 of 12 edges.
    2. now create a second square which is a side-length above the first square. can you draw the two squares? now you have all 8 points and 8 out of 12 edges
    3. finally, connect the four pairs of points vertically. now you have all 12 edges.

    This is more or less the way I would do it if I was using toothpicks and glue.

  • edited September 2018

    Naive approach:

    Array of pvector named points or corners

    Store 8 Points here each with its 3D position

    2 parallel Array of int named from and to that hold the indexes(!) of first array. For loop over from and to and show lines:

    for....
    
     line(points[from[i]].x, points[from[i]].y, ...,
    
           points[to[i]].x, ....
    

    Using 3D line command with 6 Parameters

  • More advanced:

    Since the corners in the cube can be easily calculated , it’s only necessary to know the position of upper left backfacing corner and the length of the edges.

    Then you can calculate the values for all 8 corners

Sign In or Register to comment.