Построить цилиндр, как поверхность вращения в среде Builder C++
Задание: построить цилиндр, как поверхность вращения в C++. Основание цилиндра находится в плоскости XZ. Центр основания совпадает с началом координат. Одно из оснований совпадает с началом координат.
Построить sweep поверхность:
Кривая ссылки – составная кривая Безье 3-ей степени по 12 опорным точкам, выполнить условие гладкости кривой.
Направляющая – открытый В-сплайн 3-ей степени по 10 опорным точкам, параметризация неравномерная.
Образующая – составная кривая Безье 4-ой степени по 11 опорным точкам (образующая связана с профилем).
Алгоритм построения цилиндра, как поверхность вращения
1) Строим следующие кривые: кривая ссылки, направляющая и образующая кривая.
2) Строим в плоскости XZ окружность с центром в начале координат.
3) Далее мы определяем нормаль к направляющей кривой в каждой точке. Определив нормаль, мы знаем плоскость (Ax+By+Cz+D=0), в которой будем строить профиль (окружность).
4) Далее определяем углы поворота через косинусы, используя следующее соотношения:

5) Чтобы построить окружность в нужной нам плоскости

Интерфейс разработанной программы

Вы можете задать значения опорных точек для построения всех трех кривых.
Чтобы построить поверхность необходимо нажать на кнопку «построить» после чего откроется новое окно, в котором будут построены кривые и цилиндр.
Пример работы программы
Синяя линия – кривая ссылки; зеленая линия – образующая; красная линия – направляющая

Красная прямая – ось X
Синяя прямая – ось Y
Зеленая прямая – ось Z
Другой вид этого же цилиндра и кривых:

Исходный код программы с пояснениями
//—————————————————————————
#include
#pragma hdrstop#include «Unit1.h»
#include «Unit2.h»
#include «math.h»
//—————————————————————————
#pragma package(smart_init)
#pragma resource «*.dfm»
TForm1 *Form1;//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//—————————————————————————void __fastcall TForm1::FormCreate(TObject *Sender)
{
//———-Задаем значения по умолчанию————-
StringGrid1->Cells[0][0]=’X';
StringGrid1->Cells[0][1]=’Y';
StringGrid1->Cells[0][2]=’Z'; //кривая ссылкиStringGrid2->Cells[0][0]=’X';
StringGrid2->Cells[0][1]=’Y';
StringGrid2->Cells[0][2]=’Z'; //направляющаяStringGrid3->Cells[0][0]=’X';
StringGrid3->Cells[0][1]=’Y';
StringGrid3->Cells[0][2]=’Z'; //образующая//———-инициализация значений по умолчанию (кривая ссылки)—————-
StringGrid1->Cells[1][0]=IntToStr(-5);
StringGrid1->Cells[2][0]=IntToStr(10);
StringGrid1->Cells[3][0]=IntToStr(35);
StringGrid1->Cells[4][0]=IntToStr(40);
StringGrid1->Cells[5][0]=IntToStr(43);
StringGrid1->Cells[6][0]=IntToStr(35);
StringGrid1->Cells[7][0]=IntToStr(60);
StringGrid1->Cells[8][0]=IntToStr(65);
StringGrid1->Cells[9][0]=IntToStr(70);
StringGrid1->Cells[10][0]=IntToStr(90);
StringGrid1->Cells[11][0]=IntToStr(110);
StringGrid1->Cells[12][0]=IntToStr(80);StringGrid1->Cells[1][1]=IntToStr(-15);
StringGrid1->Cells[2][1]=IntToStr(-25);
StringGrid1->Cells[3][1]=IntToStr(-35);
StringGrid1->Cells[4][1]=IntToStr(-55);
StringGrid1->Cells[5][1]=IntToStr(-60);
StringGrid1->Cells[6][1]=IntToStr(-64);
StringGrid1->Cells[7][1]=IntToStr(-76);
StringGrid1->Cells[8][1]=IntToStr(-97);
StringGrid1->Cells[9][1]=IntToStr(-100);
StringGrid1->Cells[10][1]=IntToStr(-94);
StringGrid1->Cells[11][1]=IntToStr(-86);
StringGrid1->Cells[12][1]=IntToStr(-70);StringGrid1->Cells[1][2]=IntToStr(-5);
StringGrid1->Cells[2][2]=IntToStr(-10);
StringGrid1->Cells[3][2]=IntToStr(20);
StringGrid1->Cells[4][2]=IntToStr(25);
StringGrid1->Cells[5][2]=IntToStr(40);
StringGrid1->Cells[6][2]=IntToStr(30);
StringGrid1->Cells[7][2]=IntToStr(7);
StringGrid1->Cells[8][2]=IntToStr(10);
StringGrid1->Cells[9][2]=IntToStr(30);
StringGrid1->Cells[10][2]=IntToStr(20);
StringGrid1->Cells[11][2]=IntToStr(70);
StringGrid1->Cells[12][2]=IntToStr(100);
//———-инициализация значений по умолчанию (направляющая)—————-
StringGrid2->Cells[1][0]=IntToStr(0);
StringGrid2->Cells[2][0]=IntToStr(2);
StringGrid2->Cells[3][0]=IntToStr(7);
StringGrid2->Cells[4][0]=IntToStr(5);
StringGrid2->Cells[5][0]=IntToStr(7);
StringGrid2->Cells[6][0]=IntToStr(5);
StringGrid2->Cells[7][0]=IntToStr(6);
StringGrid2->Cells[8][0]=IntToStr(4);
StringGrid2->Cells[9][0]=IntToStr(5);
StringGrid2->Cells[10][0]=IntToStr(6);StringGrid2->Cells[1][1]=IntToStr(0);
StringGrid2->Cells[2][1]=IntToStr(-2);
StringGrid2->Cells[3][1]=IntToStr(-6);
StringGrid2->Cells[4][1]=IntToStr(-9);
StringGrid2->Cells[5][1]=IntToStr(-10);
StringGrid2->Cells[6][1]=IntToStr(-15);
StringGrid2->Cells[7][1]=IntToStr(-25);
StringGrid2->Cells[8][1]=IntToStr(-30);
StringGrid2->Cells[9][1]=IntToStr(-40);
StringGrid2->Cells[10][1]=IntToStr(-90);StringGrid2->Cells[1][2]=IntToStr(0);
StringGrid2->Cells[2][2]=IntToStr(2);
StringGrid2->Cells[3][2]=IntToStr(3);
StringGrid2->Cells[4][2]=IntToStr(5);
StringGrid2->Cells[5][2]=IntToStr(4);
StringGrid2->Cells[6][2]=IntToStr(5);
StringGrid2->Cells[7][2]=IntToStr(4);
StringGrid2->Cells[8][2]=IntToStr(6);
StringGrid2->Cells[9][2]=IntToStr(4);
StringGrid2->Cells[10][2]=IntToStr(5);//———-инициализация значений по умолчанию (обращующая)—————-
StringGrid3->Cells[1][0]=IntToStr(0);
StringGrid3->Cells[2][0]=IntToStr(-3);
StringGrid3->Cells[3][0]=IntToStr(-1);
StringGrid3->Cells[4][0]=IntToStr(3);
StringGrid3->Cells[5][0]=IntToStr(7);
StringGrid3->Cells[6][0]=IntToStr(5);
StringGrid3->Cells[7][0]=IntToStr(-4);
StringGrid3->Cells[8][0]=IntToStr(-10);
StringGrid3->Cells[9][0]=IntToStr(-12);
StringGrid3->Cells[10][0]=IntToStr(-13);
StringGrid3->Cells[11][0]=IntToStr(-15);StringGrid3->Cells[1][1]=IntToStr(0);
StringGrid3->Cells[2][1]=IntToStr(-15);
StringGrid3->Cells[3][1]=IntToStr(-35);
StringGrid3->Cells[4][1]=IntToStr(-45);
StringGrid3->Cells[5][1]=IntToStr(-55);
StringGrid3->Cells[6][1]=IntToStr(-60);
StringGrid3->Cells[7][1]=IntToStr(-70);
StringGrid3->Cells[8][1]=IntToStr(-80);
StringGrid3->Cells[9][1]=IntToStr(-88);
StringGrid3->Cells[10][1]=IntToStr(-99);
StringGrid3->Cells[11][1]=IntToStr(-104);StringGrid3->Cells[1][2]=IntToStr(0);
StringGrid3->Cells[2][2]=IntToStr(-1);
StringGrid3->Cells[3][2]=IntToStr(4);
StringGrid3->Cells[4][2]=IntToStr(5);
StringGrid3->Cells[5][2]=IntToStr(3);
StringGrid3->Cells[6][2]=IntToStr(8);
StringGrid3->Cells[7][2]=IntToStr(10);
StringGrid3->Cells[8][2]=IntToStr(5);
StringGrid3->Cells[9][2]=IntToStr(12);
StringGrid3->Cells[10][2]=IntToStr(14);
StringGrid3->Cells[11][2]=IntToStr(17);
}
//—————————————————————————
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Form1->Close();
}
//—————————————————————————
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form2->Show(); //функция открытия формы 2 и построения
}
//—————————————————————————Форма №2:
//—————————————————————————
#include
#pragma hdrstop#include «Unit2.h»
#include «Unit1.h»
#include «math.h»
//—————————————————————————
#pragma package(smart_init)
#pragma resource «*.dfm»
TForm2 *Form2;
GLfloat spine[10][3];
GLfloat link[12][3];
GLfloat guide[11][3];
//—————————————————————————
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
Application->OnIdle = IdleLoop;
}
//—————————————————————————
void __fastcall TForm2::Button1Click(TObject *Sender)
{
Form2->Close();
}
//—————————————————————————
void __fastcall TForm2::IdleLoop(TObject*, bool& done)
{
done = false;
RenderScene();
SwapBuffers(hDC);
}
//—————————————————————————bool TForm2::OpenGLInit(void)
{
hDC = GetDC(Handle);
if (hDC == NULL)
return (false);
SetGLPixelFormat(hDC);
hRC = wglCreateContext(hDC);
if (hRC == NULL)
return (false);
wglMakeCurrent( hDC, hRC);
return (true);
}
//—————————————————————————
void TForm2::SetGLPixelFormat( HDC hdc)
{
int PixelFormatIndex;
PIXELFORMATDESCRIPTOR PixelFormat =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,0,0,0,0,0,0,0,
32,
0,0,
PFD_MAIN_PLANE,
0,0,0,0
};
PixelFormatIndex = ChoosePixelFormat(hdc, &PixelFormat);
SetPixelFormat(hdc, PixelFormatIndex, &PixelFormat);
}
//—————————————————————————
void TForm2::SetViewport(bool Perspective)
{
float w,h,Aspect;
w=Width;
h=Height;
if (h == 0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (Perspective)
{
Aspect=(GLfloat)w/(GLfloat)h;
gluPerspective(60.0f,Aspect,1.0f,1000.0f);
}
else
{
if (w <= h)
glOrtho(-250.0f,250.0f,-250.0f*h/w,250.0f*h/w,1.0f,1000.0f);
else
glOrtho(-250.0f*h/w,250.0f*h/w,-250.0f,250.0f,1.0f,1000.0f);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
m=1;//зум
koleso=1;
OpenGLInit();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormResize(TObject *Sender)
void __fastcall TForm2::FormResize(TObject *Sender)
{
float w,h;
GLfloat nRange = 100.0;
w = ClientWidth;
h = ClientHeight;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange*2.0f,
nRange*2.0f);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange*2.0f,
nRange*2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();}
//----------Построение окружности с радиусом r с центром x,y,z в плоскости ax+by+cz-------------
void TForm2::Circle(int r, int x, int y, int z, double a, double b, double c)
{
glPointSize (1);
glBegin(GL_LINES);
glColor3f (0.0, 0.0, 0.0);
double i,x1,y1,z1,x2,y2,z2,a1,b1,c1,tempx1,tempy1,tempz1,tempx2,tempy2,tempz2;
if ((a==0)&&(c==0)) {a1=0;c1=0;}
else{
a1=-acos(a/sqrt((a*a+b*b+c*c)))+M_PI_2;
c1=acos(c/sqrt((a*a+b*b+c*c)))-M_PI_2;
}for(i=0;i<180;i++)
{
//------круг в XZ-------
x1=(r*sin(2*i*M_PI/180)); tempx1=x1;
x2=(r*sin(2*(i+1)*M_PI/180)); tempx2=x2;y1=(0); tempy1=y1;
y2=(0); tempy2=y2;z1=(r*cos(2*i*M_PI/180)); tempz1=z1;
z2=(r*cos(2*(i+1)*M_PI/180)); tempz2=z2;//-------поворот Rx----
y1=tempy1*cos(c1)-tempz1*sin(c1);
y2=tempy2*cos(c1)-tempz2*sin(c1);z1=tempy1*sin(c1)+tempz1*cos(c1)+z;
z2=tempy2*sin(c1)+tempz2*cos(c1)+z;
tempy1=y1;tempy2=y2;//------- поворот Rz----
x1=tempx1*cos(a1)-tempy1*sin(a1)+x;
x2=tempx2*cos(a1)-tempy2*sin(a1)+x;y1=tempx1*sin(a1)+tempy1*cos(a1)+y;
y2=tempx2*sin(a1)+tempy2*cos(a1)+y;glVertex4f(x1,y1,z1,m);
glVertex4f(x2,y2,z2,m);
}
glEnd();
}
void TForm2::RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f );glPushMatrix();
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT,GL_FILL);glEnable(GL_NORMALIZE);
//---------------строим оси координат----------------
glBegin(GL_LINES);
glColor3f (0.0, 1.0, 0.0);
glVertex4f(0,0,0,m);
glVertex4f(0,0,120,m);
glColor3f (1.0, 0.0, 0.0);
glVertex4f(0,0,0,m);
glVertex4f(120,0,0,m);
glColor3f (0.0, 0.0, 1.0);
glVertex4f(0,0,0,m);
glVertex4f(0,60,0,m);
glEnd();//---------------------END----------------------
//---------------загрузка координат в массивы----------------
int i,j,k;
for(i=0;i<10;i++)
{spine[i][0]=StrToInt(Form1->StringGrid2->Cells[i+1][0]);
spine[i][1]=StrToInt(Form1->StringGrid2->Cells[i+1][1]);
spine[i][2]=StrToInt(Form1->StringGrid2->Cells[i+1][2]);
}
for(i=0;i<11;i++)
{guide[i][0]=StrToInt(Form1->StringGrid3->Cells[i+1][0]);
guide[i][1]=StrToInt(Form1->StringGrid3->Cells[i+1][1]);
guide[i][2]=StrToInt(Form1->StringGrid3->Cells[i+1][2]);
}
for(i=0;i<12;i++)
{link[i][0]=StrToInt(Form1->StringGrid1->Cells[i+1][0]);
link[i][1]=StrToInt(Form1->StringGrid1->Cells[i+1][1]);
link[i][2]=StrToInt(Form1->StringGrid1->Cells[i+1][2]);
}
//———————END———————-//—————построение точек—————-
glPointSize (3);
glBegin(GL_POINTS);
glColor3f (1.0, 0.0, 0);
for(i=0;i<10;i++)
{ glVertex4f(spine[i][0],spine[i][1],spine[i][2],m);
glVertex4f(spine[i][0],spine[i][1],spine[i][2],m);
}
glColor3f (0.0, 1.0, 0.0);
for(i=0;i<11;i++)
{ glVertex4f(guide[i][0],guide[i][1],guide[i][2],m);
glVertex4f(guide[i][0],guide[i][1],guide[i][2],m);
}
glColor3f (0.0, 0.0, 1.0);
for(i=0;i<12;i++)
{ glVertex4f(link[i][0],link[i][1],link[i][2],m);
glVertex4f(link[i][0],link[i][1],link[i][2],m);
}
glEnd();
//---------------------END----------------------int h=300;
double t=0,rx1, ry1, rx2, ry2, rz1,tempx1, tempy1, tempx2,
tempy2,tempz1,tempz2, rz2;
//------------кривая ссылки (Безье 3ей степени)----------------------
glColor3f (0.0, 0.0, 1.0);
glBegin(GL_LINES);
i=0;
tempx1=link[0][0];
tempy1=link[0][1];
tempz1=link[0][2];
tempx2=(link[i+2][0]+link[i+4][0])*0.5;
tempy2=(link[i+2][1]+link[i+4][1])*0.5;
tempz2=(link[i+2][2]+link[i+4][2])*0.5;
for(j=0;j{
rx1=(1-t)*(1-t)*(1-t)*tempx1+3*t*(1-t)*(1-t)*link[i+1][0]+3*t*t*(1-t)*link[i+2][0]+t*t*t*tempx2;
ry1=(1-t)*(1-t)*(1-t)*tempy1+3*t*(1-t)*(1-t)*link[i+1][1]+3*t*t*(1-t)*link[i+2][1]+t*t*t*tempy2;
rz1=(1-t)*(1-t)*(1-t)*tempz1+3*t*(1-t)*(1-t)*link[i+1][2]+3*t*t*(1-t)*link[i+2][2]+t*t*t*tempz2;
t=t+1.0/h;
rx2=(1-t)*(1-t)*(1-t)*tempx1+3*t*(1-t)*(1-t)*link[i+1][0]+3*t*t*(1-t)*link[i+2][0]+t*t*t*tempx2;
ry2=(1-t)*(1-t)*(1-t)*tempy1+3*t*(1-t)*(1-t)*link[i+1][1]+3*t*t*(1-t)*link[i+2][1]+t*t*t*tempy2;
rz2=(1-t)*(1-t)*(1-t)*tempz1+3*t*(1-t)*(1-t)*link[i+1][2]+3*t*t*(1-t)*link[i+2][2]+t*t*t*tempz2;
glVertex4f(rx1,ry1,rz1,m);
glVertex4f(rx2,ry2,rz2,m);
}
i=3;t=0;
tempx1=tempx2;
tempy1=tempy2;
tempz1=tempz2;
tempx2=(link[i+2][0]+link[i+4][0])*0.5;
tempy2=(link[i+2][1]+link[i+4][1])*0.5;
tempz2=(link[i+2][2]+link[i+4][2])*0.5;
for(j=0;j{
rx1=(1-t)*(1-t)*(1-t)*tempx1+3*t*(1-t)*(1-t)*link[i+1][0]+3*t*t*(1-t)*link[i+2][0]+t*t*t*tempx2;
ry1=(1-t)*(1-t)*(1-t)*tempy1+3*t*(1-t)*(1-t)*link[i+1][1]+3*t*t*(1-t)*link[i+2][1]+t*t*t*tempy2;
rz1=(1-t)*(1-t)*(1-t)*tempz1+3*t*(1-t)*(1-t)*link[i+1][2]+3*t*t*(1-t)*link[i+2][2]+t*t*t*tempz2;
t=t+1.0/h;
rx2=(1-t)*(1-t)*(1-t)*tempx1+3*t*(1-t)*(1-t)*link[i+1][0]+3*t*t*(1-t)*link[i+2][0]+t*t*t*tempx2;
ry2=(1-t)*(1-t)*(1-t)*tempy1+3*t*(1-t)*(1-t)*link[i+1][1]+3*t*t*(1-t)*link[i+2][1]+t*t*t*tempy2;
rz2=(1-t)*(1-t)*(1-t)*tempz1+3*t*(1-t)*(1-t)*link[i+1][2]+3*t*t*(1-t)*link[i+2][2]+t*t*t*tempz2;
glVertex4f(rx1,ry1,rz1,m);
glVertex4f(rx2,ry2,rz2,m);
}
i=6;t=0;
tempx1=tempx2;
tempy1=tempy2;
tempz1=tempz2;
tempx2=(link[i+2][0]+link[i+4][0])*0.5;
tempy2=(link[i+2][1]+link[i+4][1])*0.5;
tempz2=(link[i+2][2]+link[i+4][2])*0.5;
for(j=0;j{
rx1=(1-t)*(1-t)*(1-t)*tempx1+3*t*(1-t)*(1-t)*link[i+1][0]+3*t*t*(1-t)*link[i+2][0]+t*t*t*tempx2;
ry1=(1-t)*(1-t)*(1-t)*tempy1+3*t*(1-t)*(1-t)*link[i+1][1]+3*t*t*(1-t)*link[i+2][1]+t*t*t*tempy2;
rz1=(1-t)*(1-t)*(1-t)*tempz1+3*t*(1-t)*(1-t)*link[i+1][2]+3*t*t*(1-t)*link[i+2][2]+t*t*t*tempz2;
t=t+1.0/h;
rx2=(1-t)*(1-t)*(1-t)*tempx1+3*t*(1-t)*(1-t)*link[i+1][0]+3*t*t*(1-t)*link[i+2][0]+t*t*t*tempx2;
ry2=(1-t)*(1-t)*(1-t)*tempy1+3*t*(1-t)*(1-t)*link[i+1][1]+3*t*t*(1-t)*link[i+2][1]+t*t*t*tempy2;
rz2=(1-t)*(1-t)*(1-t)*tempz1+3*t*(1-t)*(1-t)*link[i+1][2]+3*t*t*(1-t)*link[i+2][2]+t*t*t*tempz2;
glVertex4f(rx1,ry1,rz1,m);
glVertex4f(rx2,ry2,rz2,m);
}i=9;t=0;
tempx1=tempx2;
tempy1=tempy2;
tempz1=tempz2;
tempx2=link[i+2][0];
tempy2=link[i+2][1];
tempz2=link[i+2][2];
for(j=0;j{
rx1=(1-t)*(1-t)*(1-t)*tempx1+3*t*(1-t)*(1-t)*link[i+1][0]+3*t*t*(1-t)*link[i+2][0]+t*t*t*tempx2;
ry1=(1-t)*(1-t)*(1-t)*tempy1+3*t*(1-t)*(1-t)*link[i+1][1]+3*t*t*(1-t)*link[i+2][1]+t*t*t*tempy2;
rz1=(1-t)*(1-t)*(1-t)*tempz1+3*t*(1-t)*(1-t)*link[i+1][2]+3*t*t*(1-t)*link[i+2][2]+t*t*t*tempz2;
t=t+1.0/h;
rx2=(1-t)*(1-t)*(1-t)*tempx1+3*t*(1-t)*(1-t)*link[i+1][0]+3*t*t*(1-t)*link[i+2][0]+t*t*t*tempx2;
ry2=(1-t)*(1-t)*(1-t)*tempy1+3*t*(1-t)*(1-t)*link[i+1][1]+3*t*t*(1-t)*link[i+2][1]+t*t*t*tempy2;
rz2=(1-t)*(1-t)*(1-t)*tempz1+3*t*(1-t)*(1-t)*link[i+1][2]+3*t*t*(1-t)*link[i+2][2]+t*t*t*tempz2;
glVertex4f(rx1,ry1,rz1,m);
glVertex4f(rx2,ry2,rz2,m);
}
glEnd();
//---------------------END----------------------//------------построение образующей (Безье 4ей степени)----------------------
glColor3f (0.0, 1.0, 0.0);
double guidemas[2100][3]; int indguid=0;
glBegin(GL_LINES);
i=0; t=0;h=700;
tempx1=guide[0][0];
tempy1=guide[0][1];
tempz1=guide[0][2];
tempx2=(guide[i+3][0]+guide[i+5][0])*0.5;
tempy2=(guide[i+3][1]+guide[i+5][1])*0.5;
tempz2=(guide[i+3][2]+guide[i+5][2])*0.5;
for(j=0;j{
rx1=(1-t)*(1-t)*(1-t)*(1-t)*tempx1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][0]+6*t*t*(1-t)*(1-t)*guide[i+2][0]+4*t*t*t*(1-t)*guide[i+3] [0]+t*t*t*t*tempx2;
ry1=(1-t)*(1-t)*(1-t)*(1-t)*tempy1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][1]+6*t*t*(1-t)*(1-t)*guide[i+2][1]+4*t*t*t*(1-t)*guide[i+3] [1]+t*t*t*t*tempy2;
rz1=(1-t)*(1-t)*(1-t)*(1-t)*tempz1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][2]+6*t*t*(1-t)*(1-t)*guide[i+2][2]+4*t*t*t*(1-t)*guide[i+3] [2]+t*t*t*t*tempz2;
t=t+1.0/h;
rx2=(1-t)*(1-t)*(1-t)*(1-t)*tempx1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][0]+6*t*t*(1-t)*(1-t)*guide[i+2][0]+4*t*t*t*(1-t)*guide[i+3] [0]+t*t*t*t*tempx2;
ry2=(1-t)*(1-t)*(1-t)*(1-t)*tempy1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][1]+6*t*t*(1-t)*(1-t)*guide[i+2][1]+4*t*t*t*(1-t)*guide[i+3] [1]+t*t*t*t*tempy2;
rz2=(1-t)*(1-t)*(1-t)*(1-t)*tempz1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][2]+6*t*t*(1-t)*(1-t)*guide[i+2][2]+4*t*t*t*(1-t)*guide[i+3] [2]+t*t*t*t*tempz2;
glVertex4f(rx1,ry1,rz1,m);
glVertex4f(rx2,ry2,rz2,m);
guidemas[indguid][0]=rx1;
guidemas[indguid][1]=ry1;
guidemas[indguid][2]=rz1; indguid++;
}
i=4;t=0;
tempx1=tempx2;
tempy1=tempy2;
tempz1=tempz2;
tempx2=(guide[i+3][0]+guide[i+5][0])*0.5;
tempy2=(guide[i+3][1]+guide[i+5][1])*0.5;
tempz2=(guide[i+3][2]+guide[i+5][2])*0.5;
for(j=0;j{
rx1=(1-t)*(1-t)*(1-t)*(1-t)*tempx1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][0]+6*t*t*(1-t)*(1-t)*guide[i+2][0]+4*t*t*t*(1-t)*guide[i+3] [0]+t*t*t*t*tempx2;
ry1=(1-t)*(1-t)*(1-t)*(1-t)*tempy1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][1]+6*t*t*(1-t)*(1-t)*guide[i+2][1]+4*t*t*t*(1-t)*guide[i+3] [1]+t*t*t*t*tempy2;
rz1=(1-t)*(1-t)*(1-t)*(1-t)*tempz1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][2]+6*t*t*(1-t)*(1-t)*guide[i+2][2]+4*t*t*t*(1-t)*guide[i+3] [2]+t*t*t*t*tempz2;
t=t+1.0/h;
rx2=(1-t)*(1-t)*(1-t)*(1-t)*tempx1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][0]+6*t*t*(1-t)*(1-t)*guide[i+2][0]+4*t*t*t*(1-t)*guide[i+3] [0]+t*t*t*t*tempx2;
ry2=(1-t)*(1-t)*(1-t)*(1-t)*tempy1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][1]+6*t*t*(1-t)*(1-t)*guide[i+2][1]+4*t*t*t*(1-t)*guide[i+3] [1]+t*t*t*t*tempy2;
rz2=(1-t)*(1-t)*(1-t)*(1-t)*tempz1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][2]+6*t*t*(1-t)*(1-t)*guide[i+2][2]+4*t*t*t*(1-t)*guide[i+3] [2]+t*t*t*t*tempz2;
glVertex4f(rx1,ry1,rz1,m);
glVertex4f(rx2,ry2,rz2,m);
guidemas[indguid][0]=rx1;
guidemas[indguid][1]=ry1;
guidemas[indguid][2]=rz1; indguid++;
}
i=8;t=0;
tempx1=tempx2;
tempy1=tempy2;
tempz1=tempz2;
tempx2=guide[i+2][0];
tempy2=guide[i+2][1];
tempz2=guide[i+2][2];
for(j=0;j{
rx1=(1-t)*(1-t)*(1-t)*(1-t)*tempx1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][0]+6*t*t*(1-t)*(1-t)*guide[i+1][0]+4*t*t*t*(1-t)*guide[i+2] [0]+t*t*t*t*tempx2;
ry1=(1-t)*(1-t)*(1-t)*(1-t)*tempy1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][1]+6*t*t*(1-t)*(1-t)*guide[i+1][1]+4*t*t*t*(1-t)*guide[i+2] [1]+t*t*t*t*tempy2;
rz1=(1-t)*(1-t)*(1-t)*(1-t)*tempz1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][2]+6*t*t*(1-t)*(1-t)*guide[i+1][2]+4*t*t*t*(1-t)*guide[i+2] [2]+t*t*t*t*tempz2;
t=t+1.0/h;
rx2=(1-t)*(1-t)*(1-t)*(1-t)*tempx1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][0]+6*t*t*(1-t)*(1-t)*guide[i+1][0]+4*t*t*t*(1-t)*guide[i+2] [0]+t*t*t*t*tempx2;
ry2=(1-t)*(1-t)*(1-t)*(1-t)*tempy1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][1]+6*t*t*(1-t)*(1-t)*guide[i+1][1]+4*t*t*t*(1-t)*guide[i+2] [1]+t*t*t*t*tempy2;
rz2=(1-t)*(1-t)*(1-t)*(1-t)*tempz1+4*t*(1-t)*(1-t)*(1-t)*guide[i+1][2]+6*t*t*(1-t)*(1-t)*guide[i+1][2]+4*t*t*t*(1-t)*guide[i+2] [2]+t*t*t*t*tempz2;
glVertex4f(rx1,ry1,rz1,m);
glVertex4f(rx2,ry2,rz2,m);
guidemas[indguid][0]=rx1;
guidemas[indguid][1]=ry1;
guidemas[indguid][2]=rz1; indguid++;
}
glEnd();
//---------------------END----------------------//---построение направляющей (открытый В-сплайн при неравномерной параметризации)-------
glColor3f (1.0, 0.0, 0.0);
glBegin(GL_LINES);
double c[10],sumc=0,ti[15],temp=0;
for (i=1; i<10;i++)
{c[i]=sqrt((spine[i][0]-spine[i-1][0])*(spine[i][0]-spine[i-1][0]) + (spine [i][1]-spine[i-1][1])*(spine[i][1]-spine[i-1][1]) + (spine[i][2]-spine[i-1] [2])*(spine[i][2]-spine[i-1][2]));
sumc+=c[i];
}
for(i=1;i<=4;i++) ti[i]=0;
for(i=11;i<=14;i++) ti[i]=1;
for(i=1;i<=6;i++)
{temp=0;
for(j=1;j<=i;j++)
temp+=c[j];
ti[i+4]=(i/7*c[i+1]+temp)/sumc;
}double n[16];
int num=300;int ii=0,ind=0;
double spinemas[2100][3];
k=3;t=0;
for(ii=0;ii<7;ii++)
for(i=0;i{
k=ii+1;n[0]= ((ti[k+4]-t)*(ti[k+4]-t)*(ti[k+4]-t))/((ti[k+4]-ti[k+1])*(ti[k+4]-ti[k+2])*(ti[k+4]-ti[k+3]));
k=ii+2;n[1]= ((t-ti[k])*(ti[k+3]-t)*(ti[k+3]-t))/((ti[k+3]-ti[k])*(ti[k+3]-ti[k+1])*(ti[k+3]-ti[k+2])) + (ti[k+4]-t)/(ti[k+4]-ti[k+1])* (((t-ti[k+1])*(ti[k+3]-t))/((ti[k+3]-ti[k+1])*(ti[k+3]-ti[k+2])) + ((ti[k+4]-t)*(t-ti[k+2]))/((ti[k+4]-ti[k+2])*(ti[k+3]-ti[k+2])));
k=ii+3;n[2]= ((t-ti[k])/(ti[k+3]-ti[k]))* ( (t-ti[k])*(ti[k+2]-t)/((ti[k+2]-ti[k])*(ti[k+2]-ti[k+1])) + ((ti[k+3]-t)*(t-ti[k+1]))/((ti[k+3]-ti[k+1])*(ti[k+2]-ti[k+1]))) + ((ti[k+4]-t)*(t-ti[k+1])*(t-ti[k+1]))/((ti[k+4]-ti[k+1])*(ti[k+3]-ti[k+1])*(ti[k+2]-ti[k+1]));
k=ii+4;n[3]= ((t-ti[k])*(t-ti[k])*(t-ti[k]))/((ti[k+3]-ti[k])*(ti[k+2]-ti[k])*(ti[k+1]-ti[k]));
j=ii;
rx1= n[0]*spine[j][0]+n[1]*spine[j+1][0]+n[2]*spine[j+2][0]+n[3]*spine[j+3][0];
ry1= n[0]*spine[j][1]+n[1]*spine[j+1][1]+n[2]*spine[j+2][1]+n[3]*spine[j+3][1];
rz1= n[0]*spine[j][2]+n[1]*spine[j+1][2]+n[2]*spine[j+2][2]+n[3]*spine[j+3][2];
t+=(ti[ii+5]-ti[ii+4])/num;
k=ii+1;n[0]= ((ti[k+4]-t)*(ti[k+4]-t)*(ti[k+4]-t))/((ti[k+4]-ti[k+1])*(ti[k+4]-ti[k+2])*(ti[k+4]-ti[k+3]));
k=ii+2;n[1]= ((t-ti[k])*(ti[k+3]-t)*(ti[k+3]-t))/((ti[k+3]-ti[k])*(ti[k+3]-ti[k+1])*(ti[k+3]-ti[k+2])) + (ti[k+4]-t)/(ti[k+4]-ti[k+1])* (((t-ti[k+1])*(ti[k+3]-t))/((ti[k+3]-ti[k+1])*(ti[k+3]-ti[k+2])) + ((ti[k+4]-t)*(t-ti[k+2]))/((ti[k+4]-ti[k+2])*(ti[k+3]-ti[k+2])));
k=ii+3;n[2]= ((t-ti[k])/(ti[k+3]-ti[k]))* ( (t-ti[k])*(ti[k+2]-t)/((ti[k+2]-ti[k])*(ti[k+2]-ti[k+1])) + ((ti[k+3]-t)*(t-ti[k+1]))/((ti[k+3]-ti[k+1])*(ti[k+2]-ti[k+1]))) + ((ti[k+4]-t)*(t-ti[k+1])*(t-ti[k+1]))/((ti[k+4]-ti[k+1])*(ti[k+3]-ti[k+1])*(ti[k+2]-ti[k+1]));
k=ii+4;n[3]= ((t-ti[k])*(t-ti[k])*(t-ti[k]))/((ti[k+3]-ti[k])*(ti[k+2]-ti[k])*(ti[k+1]-ti[k]));
rx2= n[0]*spine[j][0]+n[1]*spine[j+1][0]+n[2]*spine[j+2][0]+n[3]*spine[j+3][0];
ry2= n[0]*spine[j][1]+n[1]*spine[j+1][1]+n[2]*spine[j+2][1]+n[3]*spine[j+3][1];
rz2= n[0]*spine[j][2]+n[1]*spine[j+1][2]+n[2]*spine[j+2][2]+n[3]*spine[j+3][2];
glVertex4f(rx1,ry1,rz1,m);
glVertex4f(rx2,ry2,rz2,m);spinemas[ind][0]=rx1;
spinemas[ind][1]=ry1;
spinemas[ind][2]=rz1; ind++;
}
glEnd();
//---------------------END----------------------//-------------построение цилиндра-----------
double a1,b1,c1;for(i=0;i<30;i++)
Circle(30,0,i,0,0,0,0);for(i=0;i<2000;i++)
{
k=i;
a1=spinemas[k+1][0]-spinemas[k][0];
b1=spinemas[k+1][1]-spinemas[k][1];
c1=spinemas[k+1][2]-spinemas[k][2];
Circle(30,guidemas[k][0],guidemas[k][1],guidemas[k][2],a1,b1,c1);
}//---------------------END----------------------
glPopMatrix();
glFlush();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
Flag_mouse=False;
m_x=X-l;
m_y=Y-k;
}
//---------------------------------------------------------------------------void __fastcall TForm2::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
Flag_mouse=True;
l=X-m_x;
k=Y-m_y;
}
//---------------------------------------------------------------------------void __fastcall TForm2::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if (Flag_mouse)
{
xRot=Y-k;
yRot=X-l;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormMouseWheelUp(TObject *Sender,
TShiftState Shift, TPoint &MousePos, bool &Handled)
{
if (koleso < 1)
{
koleso=koleso+0.05;
m=m-0.1;
}
}
//---------------------------------------------------------------------------void __fastcall TForm2::FormMouseWheelDown(TObject *Sender,
TShiftState Shift, TPoint &MousePos, bool &Handled)
{
if (koleso > 0)
{
koleso=koleso-0.05;
m=m+0.1;
}
}
//—————————————————————————#ifndef Unit2H
#define Unit2H
//—————————————————————————
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define GL_PI 3.1415f
static GLfloat xRot = 22.0f;
static GLfloat yRot = 11.0f;
static GLfloat split = 4;//—————————————————————————
class TForm2 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormResize(TObject *Sender);
void __fastcall FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall FormMouseWheelUp(TObject *Sender,
TShiftState Shift, TPoint &MousePos, bool &Handled);
void __fastcall FormMouseWheelDown(TObject *Sender,
TShiftState Shift, TPoint &MousePos, bool &Handled);private: // User declarations
HDC hDC;
HGLRC hRC;
public: // User declarations
__fastcall TForm2(TComponent* Owner);
float k,m_y,m_x,l,koleso;
bool Flag_mouse;
GLfloat m;
void __fastcall IdleLoop(TObject*, bool&);
bool OpenGLInit(void);
void SetGLPixelFormat(HDC dhc);
void SetViewport(bool Perspective);
void RenderScene();
void Circle(int r, int x, int y, int z, double a, double b, double c);
};
//—————————————————————————
extern PACKAGE TForm2 *Form2;
//—————————————————————————
#endif
- Открытый В-сплайн 3-ей степени на Builder C++
- Составная кривая Безье 3-ей степени на Builder C++
- Программа эмуляции работы КЭШ-памяти
- Код Фримэна на Builder C++
- Разработка приложения-клиент и приложения-сервер, взаимодействующие по OLE технологии в среде Delphi
- Механизм обработки сообщений в Delphi
- Обработка исключений при работе с базами данных
- Многооператорные инструкции и атомарность
- Структура объекта, RTTI, операторы Is и As, иерархия классов Delphi
- Массивы в Pascal, многомерные массивы, примеры использования
- C++: Памятка C++, конструкторы, копирование, присвоение, деструкторы
- Ссылочные типы и динамические объекты в VHDL
- Рассылки сообщений, методы: Dispatch, Perform, Broadcast, NotifyControls, функции Windows API
- Освобождение памяти в DotNet, Рефлексия и недостатки DotNet
- Среда исполнения общего языка CLR
- Использование методов-сообщений для вызова корреспондирующих методов
- Многомерный случай для задач безусловной минимизации