我想创建一个迷你3D引擎。我必须将下面的代码放入单个公式,或者分辨率为0.01的情况下。但是我找不到这样做的方法。也许这里有人可以帮助我?
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);
}
看起来您的功能的期限为180,您可以使用输入值,而mod 180首先:
x = x % 180;
然后,您需要处理从90-180降低的事实:
if(x > 90)
x = 180 - x;
x现在将是0到90之间的值。您可以缩放并转换为float:
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;
}