我想创建一个3D引擎,但我有问题找到公式

问题描述:

我想创建一个迷你3D引擎。我必须将下面的代码放在一个公式中,或者如果 - 语句的分辨率为0.01。但我找不到办法做到这一点。也许这里有人可以帮助我?我想创建一个3D引擎,但我有问题找到公式

0 = 0.00 
45 = 0.50 
90 = 1.00 
135 = 0.50 
180 = 0.00 
225 = 0.50 
270 = 1.00 
315 = 0.50 
360 = 0.00 

这可以用一个公式或if语句来计算吗?

这里是我的代码(我知道它的垃圾,我只是尝试一点点)

int circX(float pi,int radius,int angle){ 
    float angleRad = (angle%360)/(180/pi); 

    return (radius * cos(angleRad)); 
} 

int circY(float pi,int radius,int angle){ 
    float angleRad = (angle%360)/(180/pi); 

    return (radius * sin(angleRad)); 
} 

void rotCube(int angleX,int angleY,int angleZ,byte state) { 

    float pi = 3.141592; 

    int x = 8; 
    int y = 4; 

    int x1 = 8; 
    int y1 = 12; 

    float rX = ?; 
    float rY = ?; 
    float rZ = ?; 

    float flat = .5; 

    int X0,X1,X2,X3,X4,X5,X6,X7; 
    int Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7; 

    X0 = (circX(pi,7,(angleX))*.5+4); 
    Y0 = (circY(pi,7,(angleX))+4); 

    X1 = circX(pi,7,(angleX))*.5-4; 
    Y1 = circY(pi,7,(angleX))+4; 

    X2 = circX(pi,7,(angleX)+90)*.5-4; 
    Y2 = circY(pi,7,(angleX)+90)+4; 

    X3 = circX(pi,7,(angleX)+90)*.5+4; 
    Y3 = circY(pi,7,(angleX)+90)+4; 

    X4 = circX(pi,7,(angleX)+270)*.5+4; 
    Y4 = circY(pi,7,(angleX)+270)-4; 

    X5 = circX(pi,7,(angleX)+270)*.5-4; 
    Y5 = circY(pi,7,(angleX)+270)-4; 

    X6 = circX(pi,7,(angleX)+180)*.5-4; 
    Y6 = circY(pi,7,(angleX)+180)-4; 

    X7 = circX(pi,7,(angleX)+180)*.5+4; 
    Y7 = circY(pi,7,(angleX)+180)-4; 



    X0 = circX(pi,7,(angleY)); 
    Y0 = circY(pi,7,(angleY))+4; 

    X1 = circX(pi,7,(angleY)+90); 
    Y1 = circY(pi,7,(angleY)+90)+4; 

    X2 = circX(pi,7,(angleY)+90); 
    Y2 = circY(pi,7,(angleY)+90)+4; 

    X3 = circX(pi,7,(angleY)); 
    Y3 = circY(pi,7,(angleY))+4; 

    X4 = circX(pi,7,(angleY)+270); 
    Y4 = circY(pi,7,(angleY)+270)-4; 

    X5 = circX(pi,7,(angleY)+180); 
    Y5 = circY(pi,7,(angleY)+180)-4; 

    X6 = circX(pi,7,(angleY)+180); 
    Y6 = circY(pi,7,(angleY)+180)-4; 

    X7 = (circX(pi,7,(angleY)+270)); 
    Y7 = (circY(pi,7,(angleY)+270)-4); 



    X0 = circX(pi,7,(angleZ)); 
    Y0 = circY(pi,7,(angleZ))*flat; 

    X1 = circX(pi,7,(angleZ)+90); 
    Y1 = circY(pi,7,(angleZ)+90)*flat; 

    X2 = circX(pi,7,(angleZ)+180); 
    Y2 = circY(pi,7,(angleZ)+180)*flat; 

    X3 = circX(pi,7,(angleZ)+270); 
    Y3 = circY(pi,7,(angleZ)+270)*flat; 

    X4 = circX(pi,7,(angleZ)); 
    Y4 = circY(pi,7,(angleZ))*flat; 

    X5 = circX(pi,7,(angleZ)+90); 
    Y5 = circY(pi,7,(angleZ)+90)*flat; 

    X6 = circX(pi,7,(angleZ)+180); 
    Y6 = circY(pi,7,(angleZ)+180)*flat; 

    X7 = circX(pi,7,(angleZ)+270); 
    Y7 = circY(pi,7,(angleZ)+270)*flat; 

    matrix.drawLine(X0+x,Y0+y, X1+x,Y1+y, state); 
    matrix.drawLine(X1+x,Y1+y, X2+x,Y2+y, state); 
    matrix.drawLine(X2+x,Y2+y, X3+x,Y3+y, state); 
    matrix.drawLine(X3+x,Y3+y, X0+x,Y0+y, state); 

    matrix.drawLine(X4+x1,Y4+y1, X5+x1,Y5+y1, state); 
    matrix.drawLine(X5+x1,Y5+y1, X6+x1,Y6+y1, state); 
    matrix.drawLine(X6+x1,Y6+y1, X7+x1,Y7+y1, state); 
    matrix.drawLine(X7+x1,Y7+y1, X4+x1,Y4+y1, state); 

    matrix.drawLine(X0+x,Y0+y, X4+x1,Y4+y1, state); 
    matrix.drawLine(X1+x,Y1+y, X5+x1,Y5+y1, state); 
    matrix.drawLine(X2+x,Y2+y, X6+x1,Y6+y1, state); 
    matrix.drawLine(X3+x,Y3+y, X7+x1,Y7+y1, state); 
    } 
+1

** **大约'的sin(x)/ 2 + 0.5'的外观,或更容易,'abs(sin(x))' –

+0

上部块既没有命名也没有在这里解释 - 应该是什么 - 例如输出 - 什么? – RuDevel

+0

@RuDevel这是该公式需要做的,但在谷歌更高分辨率 –

它看起来像你的函数的周期是180,你可以把输入值和MOD所有的180首先:

x = x % 180; 

然后,你需要从90-180处理,它降低了事实:

if(x > 90) 
    x = 180 - x; 

X现在为0和90之间的值可以缩放并转换为浮动:

float value = (float)x/90.0f; 

在函数形式:

float getValue(int x) 
{ 
    if(x < 0) // check for negatives (may not be necessary if you never pass them in) 
     x = -x; // your function is symmetrical about 0 so we can just negate 
    x %= 180; 
    if(x > 90) 
     x = 180 - x; 
    return (float)x/90.0f; 
}