pythoncoordinatescube

Find the coordinates of the vertices of a cube with given central coordinates and length in python


I want to find the coordinates of the vertices of a cube with given central coordinates (Xc,Yc,Zc) and length (SL)

def vertices(Xc,Yc,Zc,SL):
    f1 = [(0.5*SL)+ Xc,Yc,Zc]  
    f2 = [(0.5*SL)- Xc,Yc,Zc]
  
    
    x1 = [f1[0],(f1[1])-(0.5*SL), (f1[2])+(0.5*SL)]
    x2 = [f1[0],(f1[1])+(0.5*SL), (f1[2])+(0.5*SL)]
    x3 = [f1[0],(f1[1])-(0.5*SL), (f1[2])-(0.5*SL)]
    x4 = [f1[0],(f1[1])+(0.5*SL), (f1[2])-(0.5*SL)]
    x5 = [f2[0],(f2[1])-(0.5*SL), (f2[2])+(0.5*SL)]
    x6 = [f2[0],(f2[1])+(0.5*SL), (f2[2])+(0.5*SL)]
    x7 = [f2[0],(f2[1])-(0.5*SL), (f2[2])-(0.5*SL)]
    x8 = [f2[0],(f2[1])+(0.5*SL), (f2[2])-(0.5*SL)]
    
    return x1,x2,x3,x4,x5,x6,x7,x8
x1,x2,x3,x4,x5,x6,x7,x8 = vertices(5,5,5,8)

Solution

  • It's best if you return a list with 8 points (sublists) than 8 different variables.

    From the caller perspective it will be the same, you can still unpack it as 8 different variables.

    def vertices(Xc,Yc,Zc,SL):
        return [[Xc + SL/2, Yc + SL/2, Zc + SL/2],
                [Xc + SL/2, Yc + SL/2, Zc - SL/2],
                [Xc + SL/2, Yc - SL/2, Zc + SL/2],
                [Xc + SL/2, Yc - SL/2, Zc - SL/2],
                [Xc - SL/2, Yc + SL/2, Zc + SL/2],
                [Xc - SL/2, Yc + SL/2, Zc - SL/2],
                [Xc - SL/2, Yc - SL/2, Zc + SL/2],
                [Xc - SL/2, Yc - SL/2, Zc - SL/2]]