简单基于OPENGL的三维CAD框架(1)工具类

在vc++中有CDC类,同样也可以开发基于OPENGL的OPenGLDC类,这样可以像调用CDC类一样调用OPenGLDC类

首先给出两个工具类,点类和向量类

 

typedef struct tagVector3D
{
 double dx;
 double dy;
 double dz;
} VECTOR3D;

class CVector3D : public VECTOR3D
{
public:
    CVector3D();
    virtual ~CVector3D();
       CVector3D(VECTOR3D v);
    CVector3D(CVector3D &v);
    CVector3D(const double *p);
    CVector3D(double ix, double iy, double iz);
 
public:
       double GetLength() const;
       void   Normal();
    CVector3D operator*(VECTOR3D v) const;
    CVector3D operator*(CVector3D v) const;
    void operator=(CVector3D v) ;
    void operator-=(VECTOR3D v);     
    void operator-=(CVector3D v);
    CVector3D operator+(VECTOR3D v) const;
    CVector3D operator+(CVector3D v) const;
    CVector3D operator-(VECTOR3D v) const;
    CVector3D operator-(CVector3D v) const;
    CVector3D operator*(double d) const;

 

 


};


typedef struct tagPoint3D
{
 double x;
 double y;
 double z;
} POINT3D;

class CPoint3D : public POINT3D
{
public:
 CPoint3D();
 CPoint3D(double ix,double iy,double iz=0.0);
 CPoint3D(const double *p);
 CPoint3D(POINT3D p);
 virtual ~CPoint3D();

public:
 CPoint3D operator-(VECTOR3D v) const;
    CPoint3D operator+(VECTOR3D v) const;
 void operator+=(VECTOR3D v);
    void operator-=(VECTOR3D v);

 CPoint3D operator-(CVector3D v) const;
    CPoint3D operator+(CVector3D v) const;
 void operator+=(CVector3D v);
    void operator-=(CVector3D v);
};

 

具体实现如下

 

CVector3D::CVector3D()
{

}

CVector3D::~CVector3D()
{

}

CVector3D::CVector3D(VECTOR3D v)
{
 dx = v.dx;
    dy = v.dy;
 dz = v.dz;
 
}

CVector3D::CVector3D(CVector3D &v)
{
 dx = v.dx;
    dy = v.dy;
 dz = v.dz;
 
}


CVector3D::CVector3D(const double *p)
{
 dx = p[0];
 dy = p[1];
 dz = p[2];
}

CVector3D::CVector3D(double ix, double iy, double iz)
{
 dx = ix;
 dy = iy;
 dz = iz;
}

double CVector3D::GetLength() const//向量的长度
{
 double len = 0;
 len = sqrt(dx * dx + dy * dy + dz * dz);
 return len;
}

void CVector3D::Normal()//单位化
{
 double len = GetLength();

 dx /= len;
 dy /= len;
 dz /= len;
}

//向量叉乘,参数为结构或者类
 CVector3D CVector3D::operator*(VECTOR3D v) const
 {
  return CVector3D(dy * v.dz - dz * v.dy, dz * v.dx - dx * v.dz, dx * v.dy - dy * v.dx);
 }

 CVector3D CVector3D::operator*(CVector3D v) const
 {
     return CVector3D(dy * v.dz - dz * v.dy, dz * v.dx - dx * v.dz, dx * v.dy - dy * v.dx);

 }

void CVector3D::operator-=(VECTOR3D v)
{
 dx -= v.dx;
 dy -= v.dy;
 dz -= v.dz;
}

void CVector3D::operator-=(CVector3D v)
{
 dx -= v.dx;
 dy -= v.dy;
 dz -= v.dz;
}

 CVector3D CVector3D::operator+(VECTOR3D v) const
 {
  return CVector3D(dx + v.dx, dy + v.dy, dz + v.dz);
 }

 CVector3D CVector3D::operator+(CVector3D v) const
 {
     return CVector3D(dx + v.dx, dy + v.dy, dz + v.dz);

 }

  CVector3D CVector3D::operator-(VECTOR3D v) const
 {
  return CVector3D(dx - v.dx, dy - v.dy, dz - v.dz);
 }

 CVector3D CVector3D::operator-(CVector3D v) const
 {
     return CVector3D(dx - v.dx, dy - v.dy, dz - v.dz);

 }

 CVector3D CVector3D::operator*(double d) const
{
 return CVector3D(dx * d, dy * d, dz * d);
}


void CVector3D::operator=(CVector3D v)
{
 dx = v.dx;
 dy = v.dy;
 dz = v.dz;

 
}

 

CPoint3D::CPoint3D()
{

}

CPoint3D::~CPoint3D()
{

}

CPoint3D::CPoint3D(double ix,double iy,double iz)
{
          x = ix;
    y = iy;
    z = iz;
}

CPoint3D::CPoint3D(const double *p)
{
         x = p[0];
   y = p[1];
   z = p[2];

}

CPoint3D::CPoint3D(POINT3D p)
{
        x = p.x;
  y = p.y;
  z = p.z;
}

CPoint3D CPoint3D::operator-(VECTOR3D v) const
{
 return CPoint3D(x - v.dx, y - v.dy, z - v.dz);
}

CPoint3D CPoint3D::operator+(VECTOR3D v) const
{
 return CPoint3D(x + v.dx, y + v.dy, z + v.dz);
}

void CPoint3D::operator+=(VECTOR3D v)
{
 x += v.dx;
 y += v.dy;
 z += v.dz;
}

void CPoint3D::operator-=(VECTOR3D v)
{
 x -= v.dx;
 y -= v.dy;
 z -= v.dz;
}

CPoint3D CPoint3D::operator-(CVector3D v) const
{
 return CPoint3D(x - v.dx, y - v.dy, z - v.dz);
}

CPoint3D CPoint3D::operator+(CVector3D v) const
{
 return CPoint3D(x + v.dx, y + v.dy, z + v.dz);
}

void CPoint3D::operator+=(CVector3D v)
{
 x += v.dx;
 y += v.dy;
 z += v.dz;
}

void CPoint3D::operator-=(CVector3D v)
{
 x -= v.dx;
 y -= v.dy;
 z -= v.dz;
}

 

转载于:https://www.cnblogs.com/lizhengjin/archive/2008/09/24/1297858.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值