📄 cylinder.cpp
字号:
// Cylinder.cpp: implementation of the CCylinder class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MagicScissors.h"
#include "Cylinder.h"
#include "EdgeList.h"
#include "EdgePoint.h"
#include "Image/Image.h"
#include <math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCylinder::CCylinder()
{
m_nType = CYLINDER;
m_nRadius = 0;
m_nHeight = 0;
}
CCylinder::CCylinder(CCylinder* pSrc)
{
m_nType = CYLINDER;
m_ptCenter = pSrc->m_ptCenter;
m_nRadius = pSrc->m_nRadius;
m_nHeight = pSrc->m_nHeight;
m_fXScale = pSrc->m_fXScale;
m_fYScale = pSrc->m_fYScale;
m_fDepth = pSrc->m_fDepth;
m_fRotate = pSrc->m_fRotate;
m_Rect = pSrc->m_Rect;
m_XMove = pSrc->m_XMove;
m_YMove = pSrc->m_YMove;
m_bValidate = TRUE;
}
CCylinder::~CCylinder()
{
}
void CCylinder::Render(GLUquadricObj* pQuad, CImage* pImage, BOOL bGlobal)
{
if( !m_bValidate || !pQuad ) return;
int h;
int sx = m_Rect.left;
int sy = m_Rect.top;
CPoint pt;
if( bGlobal )
{
glPushMatrix();
h = m_nY;
pt.x = m_ptCenter.x;
pt.y = h-m_ptCenter.y;
glColor3f(0.f,0.5f,0.5f);
glTranslatef( 0, 0, -(500+m_fDepth) );
glTranslatef( (float)m_XMove, (float)m_YMove, 0 );
glTranslatef( (float)pt.x, (float)pt.y, 0 );
glRotatef( m_fRotate, 0, 0, 1 );
glScalef( m_fXScale, m_fYScale, m_fXScale );
glTranslatef( 0, (float)-m_nHeight/2, 0 );
glRotatef( -90, 1, 0, 0 );
gluCylinder(pQuad, m_nRadius, m_nRadius, m_nHeight, 48, 24);
glPopMatrix();
}
else
{
h = m_Rect.Height();
pt.x = m_ptCenter.x - sx;
pt.y = m_ptCenter.y - sy;
pt.y = h - pt.y;
glColor3f(0.f,0.5f,0.5f);
glTranslatef( (float)m_XMove, (float)m_YMove, 0 );
glTranslatef( (float)pt.x, (float)pt.y, (float)-m_nRadius );
glRotatef( m_fRotate, 0, 0, 1 );
glScalef( m_fXScale, m_fYScale, 1.f );
glTranslatef( 0, (float)-m_nHeight/2, 0 );
glRotatef( -90, 1, 0, 0 );
gluCylinder(pQuad, m_nRadius, m_nRadius, m_nHeight, 24, 4);
}
}
BOOL CCylinder::Create(CEdgeList* pList, CRect rect, int height)
{
m_pList = pList;
FitCylinder(pList);
m_Rect = rect;
m_nY = height;
m_bValidate = TRUE;
return TRUE;
}
BOOL CCylinder::Read(CFile* pFile)
{
C3DObject::Read(pFile);
// read radius
pFile->Read( &m_nRadius, sizeof(int) );
// read height
pFile->Read( &m_nHeight, sizeof(int) );
return TRUE;
}
BOOL CCylinder::Write(CFile* pFile)
{
C3DObject::Write(pFile);
// write radius
pFile->Write( &m_nRadius, sizeof(int) );
// write height
pFile->Write( &m_nHeight, sizeof(int) );
return TRUE;
}
#define PI 3.14159265358979
void CCylinder::FitCylinder(CEdgeList* pList)
{
int i;
int cx, cy;
float xx,yy,xy;
int n = pList->GetLength();
CPoint pt;
cx = cy = 0;
for( i = 0 ; i < n ; i++ )
{
pt = *(pList->GetPoint(i));
cx += pt.x;
cy += pt.y;
}
cx = (int)((float)cx/n+0.5);
cy = (int)((float)cy/n+0.5);
m_ptCenter.x = cx;
m_ptCenter.y = cy;
xx = yy = xy = 0.f;
for( i = 0 ; i < n ; i++ )
{
pt = *(pList->GetPoint(i));
xx += (pt.x-cx)*(pt.x-cx);
yy += (pt.y-cy)*(pt.y-cy);
xy += (pt.x-cx)*(pt.y-cy);
}
xx /= n;
yy /= n;
xy /= n;
float major, minor, angle;
major = (float)(1.3*2*sqrt(2*(xx*yy-xy*xy))/sqrt(xx+yy-sqrt((xx-yy)*(xx-yy)+4*xy*xy)));
minor = (float)(1.3*2*sqrt(2*(xx*yy-xy*xy))/sqrt(xx+yy+sqrt((xx-yy)*(xx-yy)+4*xy*xy)));
if( yy > xx )
angle = (float)atan2( (yy-xx+sqrt((yy-xx)*(yy-xx)+4*xy)), -2*xy );
else
angle = (float)atan2( -2*xy, (xx-yy+sqrt((xx-yy)*(xx-yy)+4*xy)) );
m_nRadius = (int)(minor/2);
m_nHeight = (int)major;
m_fRotate = (float)((angle*180/PI)-90);
}
BOOL CCylinder::CopyParam(C3DObject* pObject)
{
if( !pObject ) return FALSE;
CCylinder* pSrc = (CCylinder*)pObject;
m_fXScale = pSrc->m_fXScale;
m_fYScale = pSrc->m_fYScale;
m_fDepth = pSrc->m_fDepth;
m_fRotate = pSrc->m_fRotate;
m_XMove = pSrc->m_XMove;
m_YMove = pSrc->m_YMove;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -