www.pudn.com > WaveSimulation.rar > Bezier.cpp
// Bezier.cpp: implementation of the CBezier class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Bezier.h" #include#include ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CBezier::CBezier() { m_x1 = m_y1 = m_x2 = m_y2 = m_x3 = m_y3 = m_x4 = m_y4 = 0.0f; } CBezier::~CBezier() { } void CBezier::Init(float x1,float y1,float x2,float y2, float x3,float y3,float x4,float y4) { m_x1 = x1; m_y1 = y1; m_x2 = x2; m_y2 = y2; m_x3 = x3; m_y3 = y3; m_x4 = x4; m_y4 = y4; } void CBezier::ComputeBezier(float t,float* result) { if(t>1 || t<0) { result[0] = 1.0f; result[1] = 1.0f; } else { result[0] = powf( 1-t,3) * m_x1 + powf( 1-t,2) * 3 * t * m_x2 + 3 * t * t * (1-t) * m_x3 + powf(t,3) * m_x4; result[1] = powf( 1-t,3) * m_y1 + powf( 1-t,2) * 3 * t * m_y2 + 3 * t * t * (1-t) * m_y3 + powf(t,3) * m_y4; } } void CBezier::DrawBezier() { ///* glBegin(GL_LINE_STRIP); for(int i = 0; i < 50; i++ ) { glVertex3fv( (float*)&m_line[i] ); } glEnd(); //*/ glBegin(GL_LINE_STRIP); glVertex3f(0.0f,m_y1,m_x1); glVertex3f(0.0f,m_y2,m_x2); glVertex3f(0.0f,m_y3,m_x3); glVertex3f(0.0f,m_y4,m_x4); glEnd(); } void CBezier::Result() { for(int i = 0; i < 50; i++) { float result[2]; ComputeBezier(i / 49.0f,result); m_line[i].x = 0.0f; m_line[i].y = result[1]; m_line[i].z = result[0]; } } float CBezier::Findy(float z) { static int preIndex = 0; int tempmarkIndex = preIndex; float result = 0; while(tempmarkIndex < 49) { float prez,nextz; prez = z - m_line[tempmarkIndex].z; nextz = z - m_line[tempmarkIndex + 1].z; int sign = prez * nextz > 0 ? 1 : -1; if(sign == -1)//between there. { float s = ( z - m_line[tempmarkIndex].z ) / (m_line[tempmarkIndex + 1].z - m_line[tempmarkIndex].z ); result = (1 - s ) * m_line[tempmarkIndex].y + s * m_line[tempmarkIndex +1].y; return result; } tempmarkIndex++; } //È¡¶ËµãÖµ if(z > m_line[1].z) result = m_line[0].y; if(z < m_line[48].z) result = m_line[49].y; return result; }