sin()cos()环公式

时间:2013-07-31 15:15:53

标签: c++ visual-c++ geometry

这是源代码:

#include <Windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

#define d_open  fstream::in | fstream::out | fstream::app | fstream::ate
#define Space " "
#define d_qq "\""


struct point3D
{
    float X;
    float Y;
    float Z;
};

struct ObjectStruct 
{
    point3D T_LT;
    point3D T_LB;
    point3D T_RT;
    point3D T_RB;
    point3D B_LT;
    point3D B_LB;
    point3D B_RT;
    point3D B_RB;
};

void CreateObject(ObjectStruct myobject)
{
    fstream outmap;
    outmap.open("prefab.map", d_open);

    float * X = new float[6];
    float * Y = new float[6];
    float * Z = new float[6];
    float * X2 = new float[6];
    float * Y2 = new float[6];
    float * Z2 = new float[6];
    float * X3 = new float[6];
    float * Y3 = new float[6];
    float * Z3 = new float[6];

    X[0] = myobject.T_LT.X;
    Y[0] = myobject.T_LT.Y;
    Z[0] = myobject.T_LT.Z;
    X2[0] = myobject.T_RT.X;
    Y2[0] = myobject.T_RT.Y;
    Z2[0] = myobject.T_RT.Z;
    X3[0] = myobject.T_RB.X;
    Y3[0] = myobject.T_RB.Y;
    Z3[0] = myobject.T_RB.Z;
    X[1] = myobject.B_LB.X;
    Y[1] = myobject.B_LB.Y;
    Z[1] = myobject.B_LB.Z;
    X2[1] = myobject.B_RB.X;
    Y2[1] = myobject.B_RB.Y;
    Z2[1] = myobject.B_RB.Z;
    X3[1] = myobject.B_RT.X;
    Y3[1] = myobject.B_RT.Y;
    Z3[1] = myobject.B_RT.Z;
    X[2] = myobject.T_LT.X;
    Y[2] = myobject.T_LT.Y;
    Z[2] = myobject.T_LT.Z;
    X2[2] = myobject.T_LB.X;
    Y2[2] = myobject.T_LB.Y;
    Z2[2] = myobject.T_LB.Z;
    X3[2] = myobject.B_LB.X;
    Y3[2] = myobject.B_LB.Y;
    Z3[2] = myobject.B_LB.Z;
    X[3] = myobject.B_RT.X;
    Y[3] = myobject.B_RT.Y;
    Z[3] = myobject.B_RT.Z;
    X2[3] = myobject.B_RB.X;
    Y2[3] = myobject.B_RB.Y;
    Z2[3] = myobject.B_RB.Z;
    X3[3] = myobject.T_RB.X;
    Y3[3] = myobject.T_RB.Y;
    Z3[3] = myobject.T_RB.Z;
    X[4] = myobject.T_RT.X;
    Y[4] = myobject.T_RT.Y;
    Z[4] = myobject.T_RT.Z;
    X2[4] = myobject.T_LT.X;
    Y2[4] = myobject.T_LT.Y;
    Z2[4] = myobject.T_LT.Z;
    X3[4] = myobject.B_LT.X;
    Y3[4] = myobject.B_LT.Y;
    Z3[4] = myobject.B_LT.Z;
    X[5] = myobject.B_RB.X;
    Y[5] = myobject.B_RB.Y;
    Z[5] = myobject.B_RB.Z;
    X2[5] = myobject.B_LB.X;
    Y2[5] = myobject.B_LB.Y;
    Z2[5] = myobject.B_LB.Z;
    X3[5] = myobject.T_LB.X;
    Y3[5] = myobject.T_LB.Y;
    Z3[5] = myobject.T_LB.Z;




    outmap

        << "{" << endl

        << "( " << X[0] << Space << Y[0] << Space << Z[0] << " ) "
        << "( " << X2[0] << Space << Y2[0] << Space << Z2[0] << " ) "
        << "( " << X3[0] << Space << Y3[0] << Space << Z3[0] << " ) "
        << "YELLOW"
        << " [ 1 0 0 0 ] [ 0 -1 0 0 ] 0 1 1 " << endl

        << "( " << X[1] << Space << Y[1] << Space << Z[1] << " ) "
        << "( " << X2[1] << Space << Y2[1] << Space << Z2[1] << " ) "
        << "( " << X3[1] << Space << Y3[1] << Space << Z3[1] << " ) "
        << "YELLOW"
        << " [ 1 0 0 0 ] [ 0 -1 0 0 ] 0 1 1 " << endl

        << "( " << X[2] << Space << Y[2] << Space << Z[2] << " ) "
        << "( " << X2[2] << Space << Y2[2] << Space << Z2[2] << " ) "
        << "( " << X3[2] << Space << Y3[2] << Space << Z3[2] << " ) "
        << "YELLOW"
        << " [ 0 1 0 0 ] [ 0 0 -1 0 ] 0 1 1 " << endl

        << "( " << X[3] << Space << Y[3] << Space << Z[3] << " ) "
        << "( " << X2[3] << Space << Y2[3] << Space << Z2[3] << " ) "
        << "( " << X3[3] << Space << Y3[3] << Space << Z3[3] << " ) "
        << "YELLOW"
        << " [ 0 1 0 0 ] [ 0 0 -1 0 ] 0 1 1 " << endl

        << "( " << X[4] << Space << Y[4] << Space << Z[4] << " ) "
        << "( " << X2[4] << Space << Y2[4] << Space << Z2[4] << " ) "
        << "( " << X3[4] << Space << Y3[4] << Space << Z3[4] << " ) "
        << "YELLOW"
        << " [ 1 0 0 0 ] [ 0 0 -1 0 ] 0 1 1 " << endl

        << "( " << X[5] << Space << Y[5] << Space << Z[5] << " ) "
        << "( " << X2[5] << Space << Y2[5] << Space << Z2[5] << " ) "
        << "( " << X3[5] << Space << Y3[5] << Space << Z3[5] << " ) "
        << "YELLOW"
        << " [ 1 0 0 0 ] [ 0 0 -1 0 ] 0 1 1 " << endl

        << "}" << endl;

    delete [] X;
    delete [] Y;
    delete [] Z;
    delete [] X2;
    delete [] Y2;
    delete [] Z2;
    delete [] X3;
    delete [] Y3;
    delete [] Z3;
    outmap.flush();
    //
    outmap.close();
}





int main(int argc, char ** argv)
{
    //HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTitle("Prefab Creator by Raichu (xgm.ru,d3scene.ru)");
    Sleep(200);
    SetConsoleTitle("PrefabTool by Raichu  v0.1");
    int select = 0;
    cout << "Available shapes: 1-Ring, 2-Cube, 3-Triangle" << endl;
    cin >> select;
    if (select == 1)
    {
        cout << "Enter quantity of objects:" << endl;
        int quality;

        cin >> quality;

        if (quality > 200 || quality < 10)
        {
            cout << "Error... Restart..." << endl;
            return main(argc, argv);
        }


        cout << "Enter Ring radius:(example 20.0)" << endl;
        float WhSz;


        cin >> WhSz;

        float inangle = 360 / quality;
        //system("cls");
        float X1_, X2_, Y1_, Y2_;
        float X1, X2, Y1, Y2;
        float a = 0;
        X1_ = WhSz * cos(a);
        Y1_ = WhSz * sin(a);
        X2_ = (WhSz + 4) * cos(a);
        Y2_ = (WhSz + 4) * sin(a);
        a = inangle;
        for (; a <= 360; )
        {



            X1 = WhSz * cos(a);
            Y1 = WhSz * sin(a);
            X2 = (WhSz + 4) * cos(a);
            Y2 = (WhSz + 4) * sin(a);
#pragma region TEST
            //cout << "X:" << X1 << " Y:" << Y1 << endl;

        /*  COORD xy;
            xy.X = X1;
            xy.Y = Y1;
            SetConsoleCursorPosition(console, xy);
            cout << "+" << endl;
            xy.X = X2;
            xy.Y = Y2;
            SetConsoleCursorPosition(console, xy);
            cout << "?" << endl;*/
#pragma endregion

            ObjectStruct NewObject;
            /* down */
            NewObject.B_LB.Z = NewObject.B_LT.Z = NewObject.B_RB.Z = NewObject.B_RT.Z = -5;
            /* top */ 
            NewObject.T_LB.Z = NewObject.T_LT.Z = NewObject.T_RB.Z = NewObject.T_RT.Z = 5;


            NewObject.T_LT.X = NewObject.B_LT.X = X2_;
            NewObject.T_LT.Y = NewObject.B_LT.Y = Y2_;
            NewObject.T_LB.X = NewObject.B_LB.X = X1_;
            NewObject.T_LB.Y = NewObject.B_LB.Y = Y1_;

            NewObject.T_RT.X = NewObject.B_RT.X = X2;
            NewObject.T_RT.Y = NewObject.B_RT.Y = Y2;
            NewObject.T_RB.X = NewObject.B_RB.X = X1;
            NewObject.T_RB.Y = NewObject.B_RB.Y = Y1;


            CreateObject(NewObject);


            X1_ = X1;
            Y1_ = Y1;
            X2_ = X2;
            Y2_ = Y2;

            //a += inangle/3; // - Create SUN
            a += inangle;  

        }

    }

    system("pause");
    return 0;
}

问题代码:

if (select == 1)
    {
        cout << "Enter quantity of objects:" << endl;
        int quality;

        cin >> quality;

        if (quality > 200 || quality < 10)
        {
            cout << "Error... Restart..." << endl;
            return main(argc, argv);
        }


        cout << "Enter Ring radius:(example 20.0)" << endl;
        float WhSz;


        cin >> WhSz;

        float inangle = 360 / quality;
        //system("cls");
        float X1_, X2_, Y1_, Y2_;
        float X1, X2, Y1, Y2;
        float a = 0;
        X1_ = WhSz * cos(a);
        Y1_ = WhSz * sin(a);
        X2_ = (WhSz + 4) * cos(a);
        Y2_ = (WhSz + 4) * sin(a);
        a = inangle;
        for (; a <= 360; )
        {



            X1 = WhSz * cos(a);
            Y1 = WhSz * sin(a);
            X2 = (WhSz + 4) * cos(a);
            Y2 = (WhSz + 4) * sin(a);
#pragma region TEST
            //cout << "X:" << X1 << " Y:" << Y1 << endl;

        /*  COORD xy;
            xy.X = X1;
            xy.Y = Y1;
            SetConsoleCursorPosition(console, xy);
            cout << "+" << endl;
            xy.X = X2;
            xy.Y = Y2;
            SetConsoleCursorPosition(console, xy);
            cout << "?" << endl;*/
#pragma endregion

            ObjectStruct NewObject;
            /* down */
            NewObject.B_LB.Z = NewObject.B_LT.Z = NewObject.B_RB.Z = NewObject.B_RT.Z = -5;
            /* top */ 
            NewObject.T_LB.Z = NewObject.T_LT.Z = NewObject.T_RB.Z = NewObject.T_RT.Z = 5;


            NewObject.T_LT.X = NewObject.B_LT.X = X2_;
            NewObject.T_LT.Y = NewObject.B_LT.Y = Y2_;
            NewObject.T_LB.X = NewObject.B_LB.X = X1_;
            NewObject.T_LB.Y = NewObject.B_LB.Y = Y1_;

            NewObject.T_RT.X = NewObject.B_RT.X = X2;
            NewObject.T_RT.Y = NewObject.B_RT.Y = Y2;
            NewObject.T_RB.X = NewObject.B_RB.X = X1;
            NewObject.T_RB.Y = NewObject.B_RB.Y = Y1;


            CreateObject(NewObject);


            X1_ = X1;
            Y1_ = Y1;
            X2_ = X2;
            Y2_ = Y2;

            //a += inangle/3; // - Create SUN
            a += inangle;  

        }

    }

我想使用对象创建一个环。它转3圈(3层)而不是1圈。怎么解决?这似乎是度数的问题,但我无法理解。 (对不起,我使用的是翻译Google翻译)

1 个答案:

答案 0 :(得分:3)

sincos将弧度视为输入,而非度数。在将角度传递到sincos之前,您需要将角度转换为弧度:

X1 = WhSz * cos(a * 3.141592654 / 180.0);