📄 bezier.cpp
字号:
#include "stdafx.h"
#include "Bezier.h"
/*****************************************************************************************
* FUNCTION : DrawBeziers *
*---------------------------------------------------------------------------------------*
* DESCRIPTION : Draws a series of connected Qubic Bezier-splines. They must be connec *
* ted so that the last point of the bezier is the same as the next ones *
* first point on the curve *
* *
* ARGUMENT : CDC* pDC, The device context where the beziers are drawn *
* : CPoint* pPoints, The points that make up the beziers *
* : int nPoints, The number of points on the curve *
* : int nSegments, How many points are we to compute on each bezier? *
* RETURN : void *
****************************************************************************************/
void DrawBeziers(CDC* pDC, CPoint *pPoints, int nPoints, int nSegments)
{
VERIFY(pPoints != NULL);
ASSERT(nSegments > 0);
for(int i = 0; i + 4 <= nPoints; i += 3)
{
DrawBezier(pDC, &pPoints[i], nSegments);
}
}
/*****************************************************************************************
* FUNCTION : DrawBezier *
*---------------------------------------------------------------------------------------*
* DESCRIPTION : Draws a Qubic bezier-spline based on four control points *
* *
* ARGUMENT : CDC* pDC, The device context where the bezier are drawn *
* : CPoint* pPoints, The points that make up the bezier *
* : int nSegments, How many points are we to compute on the bezier? *
* RETURN : void *
****************************************************************************************/
void DrawBezier(CDC *pDC, CPoint *pPoints, int nSegments)
{
pDC->MoveTo(pPoints[0]);
fPoint fPointBezier;
for(int i = 0; i < nSegments; i++)
{
BezierComputePoint(i / (float)nSegments, &fPointBezier, pPoints);
pDC->LineTo(ROUND(fPointBezier.x), ROUND(fPointBezier.y));
}
pDC->LineTo(pPoints[3]);
}
/*****************************************************************************************
* FUNCTION : BezierComputePoint *
*---------------------------------------------------------------------------------------*
* DESCRIPTION : Computes a point on the spline using four blending functions *
* *
* ARGUMENT : float fU, position on the spline to compute *
* : fPoint* pDstPoint, The computed point is written here *
* : CPoint* pSrcPoints, Pointer to the four control points *
* RETURN : void *
****************************************************************************************/
void BezierComputePoint(float fU, fPoint* pDstPoint, CPoint* pSrcPoints)
{
//
// Add up all the blending functions multiplied with the control points
//
float fBlend;
float f1subu = 1.0f - fU;
//
// First blending function (1-u)^3
//
fBlend = f1subu * f1subu * f1subu;
pDstPoint->x = fBlend * pSrcPoints[0].x;
pDstPoint->y = fBlend * pSrcPoints[0].y;
//
// Second blending function 3u(1-u)^2
//
fBlend = 3 * fU * f1subu * f1subu;
pDstPoint->x += fBlend * pSrcPoints[1].x;
pDstPoint->y += fBlend * pSrcPoints[1].y;
//
// Third blending function 3u^2 * (1-u)
//
fBlend = 3 * fU * fU * f1subu;
pDstPoint->x += fBlend * pSrcPoints[2].x;
pDstPoint->y += fBlend * pSrcPoints[2].y;
//
// Fourth blending function u^3
//
fBlend = fU * fU * fU;
pDstPoint->x += fBlend * pSrcPoints[3].x;
pDstPoint->y += fBlend * pSrcPoints[3].y;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -