[CPP] 벡터 계산 클래스

simpled 2013.06.23 14:59 조회 수 : 6090

출처 : hiddenviewer.tistory.com/33


main.cpp




#include <iostream>

#include <math.h>

 

using namespace std;

 

#define PI 3.141592

 

class clVector

{

public:

        double Magnitude;

        double x, y, z;

public:

 

    // 벡터의생성자.

        clVector(doubledoubledouble);

 

        // 재정의

        void Init(doubledoubledouble);

 

        // 벡터의크기

        double GetMagnitude();

 

        // 벡터의스칼라곱

        clVector operator * (double num);

 

        // 벡터의합

        clVector operator + (const clVector &vec);

 

        // 벡터의차

        clVector operator - (const clVector &vec);

 

        // 벡터의정규화

        void normalize(void);

 

        // 벡터의내적

        double dot_clVector(const clVector &vec);

 

        // 벡터의외적

        clVector cross_clVector(const clVector &vec);

 

        // 내적을이용해서두벡터간의각을구함(단위(degree))

        double Angle_Degree_clVector(clVector &vec);

 

        // 내적을이요해서두벡터간의각을구함(단위라디안(Radian))

        double Angle_Radian_clVector(clVector &vec);

 

};

 

clVector::clVector(double _x=0, double _y=0, double _z=0)

{

        x=_x;

        y=_y;

        z=_z;

}

 

void clVector::Init(double _x=0, double _y=0, double _z=0)

{

        x=_x;

        y=_y;

        z=_z;

}

 

// 벡터의크기

double clVector::GetMagnitude()

{

        Magnitude=sqrt(x*x + y*y + z*z);

        return Magnitude;

}

 

// 벡터의스칼라곱

clVector clVector::operator *(double num)

{

        return clVector(x*num + y*num + z*num);

}

 

// 벡터의합

clVector clVector:: operator + (const clVector &vec)

{

        return clVector(x+vec.x, y+vec.y, z+vec.z);

}

 

// 벡터의차

clVector clVector:: operator - (const clVector &vec)

{

        return clVector(x-vec.x, y-vec.y, z-vec.z);

}

 

// 벡터의정규화

void clVector::normalize(void)

{

        GetMagnitude();

        x = x / Magnitude;

        y = y / Magnitude;

        z = z / Magnitude;

}

 

// 벡터의내적

double clVector::dot_clVector(const clVector &vec)

{

        return double(x*vec.x + y*vec.y + z*vec.z);

}

 

// 벡터의외적

clVector clVector::cross_clVector(const clVector &vec)

{

        double a = y*vec.z - z*vec.y;

        double b = z*vec.x - x*vec.z;

        double c = x*vec.y - y*vec.x;

 

        return clVector(a, b, c);

}

 

// 내적을이용해서두벡터간의각을구함(단위(degree))

double clVector::Angle_Degree_clVector(clVector &vec)

{

        return acos( dot_clVector(vec) / (GetMagnitude() * vec.GetMagnitude())) * (180.0 / PI);

}

 

// 내적을이요해서두벡터간의각을구함(단위라디안(Radian))

double clVector::Angle_Radian_clVector(clVector &vec)

{

        return acos( dot_clVector(vec) / (GetMagnitude() * vec.GetMagnitude()));

}

 

 

int main(void)

{

    clVector a, b, c;

    double ax, ay, az;

    double bx, by, bz;

 

    while(1)

    {

        cout <<"Vector 1 (3번입력): ";

        cin>>ax>>ay>>az;

        cout <<"Vector 2 (3번입력): ";

        cin>>bx>>by>>bz;

        cout <<endl;

 

        cout <<"Vector 1 : "<<ax <<", "<<ay <<", "<<az <<endl;

        cout <<"Vector 2 : "<<bx <<", "<<by <<", "<<bz <<endl;

        cout <<endl;

 

        // x-z평면상의각도구하기

        a.Init(ax, ay, az);

        b.Init(bx, by, bz);

        cout <<endl;

 

        cout <<"정규화하지않음"<<endl;

        cout <<"Angle a-b (Degree) "<<a.Angle_Degree_clVector(b) <<endl;

        cout <<"Angle b-a (Degree): "<<b.Angle_Degree_clVector(a) <<endl;

        cout <<"Angle a-b (Radian) "<<a.Angle_Radian_clVector(b) <<endl;

        cout <<"Angle b-a (Radian): "<<b.Angle_Radian_clVector(a) <<endl;

        cout<<endl;

 

        cout<<"노말벡터구하기"<<endl;

        c=a.cross_clVector(b);

        cout <<"Vector 3 : "<<c.x <<", "<<c.y <<", "<<c.z <<endl;

        cout<<endl;

 

        cout<<"정규화하였음"<<endl;

        a.normalize();

        b.normalize();

        cout <<"Angle a-b (Degree) "<<a.Angle_Degree_clVector(b) <<endl;

        cout <<"Angle b-a (Degree): "<<b.Angle_Degree_clVector(a) <<endl;

        cout <<"Angle a-b (Radian) "<<a.Angle_Radian_clVector(b) <<endl;

        cout <<"Angle b-a (Radian): "<<b.Angle_Radian_clVector(a) <<endl;

        cout <<endl;

 

        cout<<"노말벡터구하기"<<endl;

        c=a.cross_clVector(b);

        cout <<"Vector 3 : "<<c.x <<", "<<c.y <<", "<<c.z <<endl;

        cout<<endl;

    }

    return 0;

}


001.png 


002.png

번호 제목 글쓴이 날짜 조회 수
29 [CPP] file 입출력 : space data file simpled 2013.06.23 5257
28 [CPP] 1차 방정식을 통한 y값 구하기 simpled 2013.06.23 5777
27 [CPP] Vector 계산 클래스 (v2.0) file simpled 2013.06.23 4675
26 [CPP] 자리수 표현 simpled 2013.06.23 5618
25 [CPP] 각도 클래스(+ 아크탄젠트 리턴값 조정) file simpled 2013.06.23 6024
» [CPP] 벡터 계산 클래스 file simpled 2013.06.23 6090
23 [CPP] file 입출력 simpled 2013.06.23 4964
22 [CPP] 수치해석 : Rectangle Method 구분구적분 (적분 프로그래밍) simpled 2013.06.23 8111
21 [CPP] 분포하중 _ Beam의 SFD, BMD 값 simpled 2013.06.23 6471
20 [CPP] OOP6. CPP Programming :: Virtual Banking system PPT [끝] file 심플디 2013.06.23 6607
19 [CPP] OOP6. CPP Programming :: Virtual Banking system v3.8 file simpled 2013.06.23 5920
18 [CPP] OOP6. CPP Programming :: Virtual Banking system 모듈프로그램들 file simpled 2013.06.23 5244
17 [CPP] OOP6. CPP Programming :: Virtual Banking system v3.7 file simpled 2013.06.23 5954
16 [CPP] OOP6. CPP Programming :: Virtual Banking system v3.6 file simpled 2013.06.23 6287
15 [CPP] OOP6. CPP Programming :: Virtual Banking system v3.5.1 file simpled 2013.06.23 6442
14 [CPP] OOP6. CPP Programming :: Virtual Banking system v3.5 file simpled 2013.06.23 6620
13 [CPP] -----------------------------------절취선----------------------------------- simpled 2013.06.23 5830
12 [CPP] Banksys 휴식 simpled 2013.06.23 6496
11 [CPP] OOP5. CPP Programming :: Virtual Banking system v3.0.2 [Class design3. String Sorting] simpled 2013.06.23 5612
10 [CPP] OOP5. CPP Programming :: Virtual Banking system v3.0.2 [Class design2. File I/O] simpled 2013.06.23 6548