2017-02-23 11 views
1

私はミニ3Dエンジンを作成したい。私は以下のコードを単一の公式またはif文に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 + (abs(sin(x)) ' –

+0

上のブロックは名前も説明もされていません。 – RuDevel

+0

@RuDevelこれは、式が行う必要があるものの、より高い解像度で行われます。 –

答えて

2

それはあなたの関数のように見える私のコードである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; 
} 
関連する問題