⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sphere.cpp

📁 刚上传内容的相关CODEC不能单独上传。于是
💻 CPP
字号:
// Sphere.cpp: implementation of the CSphere class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MagicScissors.h"
#include "Sphere.h"

#include "EdgeList.h"
#include "EdgePoint.h"

#include "Matrix.h"

#include "Image/Image.h"
#include "Image/ColorPixel.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSphere::CSphere()
{
	m_nType = SPHERE;
	m_nRadius = 0;
}

CSphere::CSphere(CSphere* pSrc)
{
	m_nType = SPHERE;
	m_ptCenter = pSrc->m_ptCenter;
	m_nRadius = pSrc->m_nRadius;
	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;
}

CSphere::~CSphere()
{
}

void CSphere::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;
		
		BOOL bTexture = FALSE;
		
		glColor3f(0.f,0.f,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 );
		//glRotatef( -90, 1, 0, 0 );		
		gluSphere(pQuad, m_nRadius, 48, 48);

		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.f,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 );
		gluSphere(pQuad, m_nRadius, 24, 24);
	}
}

BOOL CSphere::Create(CEdgeList* pList, CRect rect, int height)
{
	m_pList = pList;
	FitCircle(pList);
	m_Rect = rect;
	m_nY = height;
	m_bValidate = TRUE;
	return TRUE;
}

BOOL CSphere::Read(CFile* pFile)
{
	C3DObject::Read(pFile);
	
	// read radius
	pFile->Read( &m_nRadius, sizeof(int) );
	return TRUE;
}

BOOL CSphere::Write(CFile* pFile)
{
	C3DObject::Write(pFile);
	
	// write radius
	pFile->Write( &m_nRadius, sizeof(int) );
	return TRUE;
}

BOOL CSphere::FitCircle(CEdgeList* pList)
{
	m_ptCenter = GetCircleCenter(pList);
	m_nRadius = GetCircleRadius(pList, m_ptCenter);
	return TRUE;
}


CPoint CSphere::GetCircleCenter( CEdgeList* pList )
{
	int l = pList->GetLength();
	
	CMatrix<float> mat1( l, 3 );
	CMatrix<float> mat2( l, 1 );
	int x,y;
	int i;	
	CPoint pt;

	for( i = 0 ; i < l ; i++ )
	{
		pt = *(pList->GetPoint(i));
		x = pt.x;
		y =	pt.y;

		mat1[i][0] = (float)2*y;
		mat1[i][1] = (float)2*x;
		mat1[i][2] = (float)1;
		mat2[i][0] = (float)y*y+x*x;		
	}
	CMatrix<float> mat3, mat4;

	mat3 = mat1.Transpose()*mat1;
	mat4 = mat1.Transpose()*mat2;

	CMatrix<float> mat5;

	mat5 = mat3.Inverse()*mat4;

	x = (int)mat5[1][0];
	y = (int)mat5[0][0];

	return CPoint(x,y);

}

int CSphere::GetCircleRadius( CEdgeList* pList, CPoint center )
{
	int x,y,cx,cy;
	float r = 0.f;
	cx = center.x;
	cy = center.y;
	int i;
	int l = pList->GetLength();
	CPoint pt;
	for( i = 0 ; i < l ; i++ )
	{
		pt = *(pList->GetPoint(i));
		x = pt.x;
		y = pt.y;
		r += (float)sqrt( (x-cx)*(x-cx)+(y-cy)*(y-cy)+.5);
	}
	
	return (int)((float)r/l+.5f);	
}

BOOL CSphere::CopyParam(C3DObject* pObject)
{
	if( !pObject ) return FALSE;

	CSphere* pSrc = (CSphere*)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 + -