3D图形,立方体似乎不合逻辑

时间:2018-01-18 22:11:07

标签: java algorithm graphics 3d

我设计了一个简单的java程序来观看3D立方体,但我不知道为什么我猜这里有一些不合逻辑的东西。

肋骨长度= 350 正方形深度(z轴)为0

后方正方形深度(z轴)等于肋的长度

但结果是非常高的立方体!!:

我设计了一个简单的java程序来观看3D立方体,但我不知道为什么我猜这里有一些不合逻辑的东西。

肋骨长度= 350 正方形深度(z轴)为0

后方正方形深度(z轴)等于肋骨长度, 但结果是很高的立方体!!:

enter image description here

代码:

public void paint(Graphics g){
    super.paint(g);

    int squareLengthRib  = 350; // Ribs length
    int frontSquareDepth = 0;   // Z value (Depth of front square)
    int backSquareDepth  = frontSquareDepth + squareLengthRib; // Z value (Depth of back square)

    x1 = 300;
    y1 = 300;
    z1 = frontSquareDepth;

    x2 = x1 + squareLengthRib;
    y2 = y1 ;
    z2 = z1;

    x3 = x2;
    y3 = y2 + squareLengthRib;
    z3 = z2;

    x4 = x1;
    y4 = y3;
    z4 = z3;

    /**********************************************/

    x5 = x1;
    y5 = y1;
    z5 = backSquareDepth;

    x6 = x2;
    y6 = y2;
    z6 = z5;

    x7 = x3;
    y7 = y3;
    z7 = z6;

    x8 = x4;
    y8 = y4;
    z8 = z7;

    PerspectiveProjection();

    g.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
    g.drawLine((int)x2, (int)y2, (int)x3, (int)y3);
    g.drawLine((int)x3, (int)y3, (int)x4, (int)y4);
    g.drawLine((int)x4, (int)y4, (int)x1, (int)y1);

    g.drawLine((int)x5, (int)y5, (int)x6, (int)y6);
    g.drawLine((int)x6, (int)y6, (int)x7, (int)y7);
    g.drawLine((int)x7, (int)y7, (int)x8, (int)y8);
    g.drawLine((int)x8, (int)y8, (int)x5, (int)y5);

    g.drawLine((int)x1, (int)y1, (int)x5, (int)y5);
    g.drawLine((int)x2, (int)y2, (int)x6, (int)y6);
    g.drawLine((int)x3, (int)y3, (int)x7, (int)y7);
    g.drawLine((int)x4, (int)y4, (int)x8, (int)y8);



}

private void PerspectiveProjection() {
    double d = 100;
    double xd, yd;

    xd = (x1 * d) / (z1 + d); 
    yd = (y1 * d) / (z1 + d);
    x1 = xd;
    y1 = yd;

    xd = (x2 * d) / (z2 + d); 
    yd = (y2 * d) / (z2 + d);
    x2 = xd;
    y2 = yd;

    xd = (x3 * d) / (z3 + d); 
    yd = (y3 * d) / (z3 + d);
    x3 = xd;
    y3 = yd;

    xd = (x4 * d) / (z4 + d); 
    yd = (y4 * d) / (z4 + d);
    x4 = xd;
    y4 = yd;

    xd = (x5 * d) / (z5 + d); 
    yd = (y5 * d) / (z5 + d);
    x5 = xd;
    y5 = yd;

    xd = (x6 * d) / (z6 + d); 
    yd = (y6 * d) / (z6 + d);
    x6 = xd;
    y6 = yd;

    xd = (x7 * d) / (z7 + d); 
    yd = (y7 * d) / (z7 + d);
    x7 = xd;
    y7 = yd;

    xd = (x8 * d) / (z8 + d); 
    yd = (y8 * d) / (z8 + d);
    x8 = xd;
    y8 = yd;
}

0 个答案:

没有答案