Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабы / igs (супер 2001) / Spline_3d_line / Spline
.cpp// Spline.cpp: implementation of the CSpline class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Spline_3d.h"
#include "Spline.h"
#include <math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSpline::CSpline(int PtNum, CPoint Center)
{
PointNum = PtNum;
PointRad = 6;
XYZCenter = Center;
Point = new PointCoord[PointNum];
L = 100; R = 50;
Point0 = PointCoord();
PointX = PointCoord(0, L, 0);
PointY = PointCoord(L, 0, 0);
PointZ = PointCoord(0, 0, L);
for(int i=0; i<PointNum; i++)
Point[i] = PointCoord(150*cos(2*pi*i/PointNum), 150*sin(2*pi*i/PointNum), 0);
}
CSpline::~CSpline()
{
if(Point) delete Point;
}
void CSpline::DrawXYZ(CPaintDC &dc, double a, double b, int d, CPoint Center)
{ A = a; B = b; D = d; XYZCenter = Center;
COLORREF Col = RGB(0, 150, 0);
CPen Pen(PS_SOLID,2,Col);
CPen *pPen = dc.SelectObject(&Pen);
dc.SelectObject(Pen);
CPoint Px = Transform3Dto2D(PointX);
CPoint Py = Transform3Dto2D(PointY);
CPoint Pz = Transform3Dto2D(PointZ);
dc.MoveTo(XYZCenter);
dc.LineTo(Px);
dc.TextOut(Px.x, Px.y, "x");
dc.MoveTo(XYZCenter);
dc.LineTo(Py);
dc.TextOut(Py.x, Py.y, "y");
dc.MoveTo(XYZCenter);
dc.LineTo(Pz);
dc.TextOut(Pz.x, Pz.y, "z");
dc.SelectObject(pPen);
}
void CSpline::DrawSpline(CPaintDC &dc, int ShowCarcas, double a, double b, int d)
{ A = a; B = b; D = d;
if(ShowCarcas)
for(int i=0; i<PointNum; i++)
{
dc.MoveTo(Transform3Dto2D(Point[i]));
if(i!=PointNum -1) dc.LineTo(Transform3Dto2D(Point[i+1]));
else dc.LineTo(Transform3Dto2D(Point[0]));
}
for(int i=0; i<PointNum; i++)
{
CPoint P = Transform3Dto2D(Point[i]);
dc.Ellipse(P.x - PointRad, P.y - PointRad,
P.x + PointRad, P.y + PointRad);
}
PointCoord r, r0;
int N = 40;
double t, dt = 1.0/N;
BSplineCom(Point, 0, 0, r);
r0 = r;
dc.MoveTo(Transform3Dto2D(r));
COLORREF Col = RGB(250, 100, 0);
CPen Pen(PS_SOLID,3,Col);
CPen *pPen = dc.SelectObject(&Pen);
dc.SelectObject(Pen);
for(int j=0; j<PointNum; j++)
for(int i=0; i<N; i++)
{ t = dt*i;
BSplineCom(Point, j, t, r);
dc.LineTo(Transform3Dto2D(r));
}
dc.LineTo(Transform3Dto2D(r0));
dc.SelectObject(pPen);
}
CPoint CSpline::Transform3Dto2D(PointCoord Point)
{ CPoint P;
P.x = XYZCenter.x + int(D/(50+R)*(-Point.x*sin(A) + Point.y*cos(A)));
P.y = XYZCenter.y + int(D/(50+R)*(-Point.x*cos(A)*cos(B) - Point.y*sin(A)*cos(B) + Point.z*sin(B)));
return P;
}
void CSpline::BSplineCom(PointCoord *Point, int i, double t, PointCoord &r)
{ double t2 = pow(t, 2), t3 = pow(t, 3);
double b0 = (1-3*t+3*t2-t3)/6;
double b1 = (4-6*t2+3*t3)/6;
double b2 = (1+3*t+3*t2-3*t3)/6;
double b3 = t3/6;
int i1, i2, i3;
if(i+1>=PointNum)i1 = i+1-PointNum;else i1 = i+1;
if(i+2>=PointNum)i2 = i+2-PointNum;else i2 = i+2;
if(i+3>=PointNum)i3 = i+3-PointNum;else i3 = i+3;
r = PointCoord(Point[i].x*b0 + Point[i1].x*b1 + Point[i2].x*b2 + Point[i3].x*b3,
Point[i].y*b0 + Point[i1].y*b1 + Point[i2].y*b2 + Point[i3].y*b3,
Point[i].z*b0 + Point[i1].z*b1 + Point[i2].z*b2 + Point[i3].z*b3);
}
void CSpline::DrawFunction(CPaintDC &dc, double a, double b, int d, int rast)
{
}
Соседние файлы в папке Spline_3d_line