二次贝塞尔(QuadTo)和三次贝塞尔(CubicTo)是绘制平滑曲线的核心工具。
| 函数 | 说明 |
|---|---|
int xgeShapeExQuadTo(xge_shape_ex pShape, float fCX, float fCY, float fX, float fY) | 二次贝塞尔(1 控制点 + 终点) |
int xgeShapeExCubicTo(xge_shape_ex pShape, float fC1X, float fC1Y, float fC2X, float fC2Y, float fX, float fY) | 三次贝塞尔(2 控制点 + 终点) |
gcc -O2 -I. -o ch37.exe ch37_main.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch37.exe
xge_shape_ex curve = NULL;
xgeShapeExCreate(&curve);
xgeShapeExMoveTo(curve, 100, 400); // 起点
xgeShapeExQuadTo(curve, 400, 50, 700, 400); // 控制点(400,50) 终点(700,400)
xgeShapeExStrokeColor(curve, 0x4FD8C2FF);
xgeShapeExStrokeWidth(curve, 3.0f);
xgeShapeExDraw(curve, 0.25f);xge_shape_ex s = NULL;
xgeShapeExCreate(&s);
xgeShapeExMoveTo(s, 100, 300);
xgeShapeExCubicTo(s,
250, 100, // 控制点 1(向上拉)
550, 500, // 控制点 2(向下拉)
700, 300); // 终点
xgeShapeExStrokeColor(s, 0xFFB454FF);
xgeShapeExStrokeWidth(s, 3.0f);
xgeShapeExDraw(s, 0.25f);xge_shape_ex heart = NULL;
xgeShapeExCreate(&heart);
xgeShapeExMoveTo(heart, 400, 480);
xgeShapeExCubicTo(heart, 100,300, 150,100, 400,200);
xgeShapeExCubicTo(heart, 650,100, 700,300, 400,480);
xgeShapeExClose(heart);
xgeShapeExFillColor(heart, 0xFF6B5EFF);
xgeShapeExDraw(heart, 0.25f);将以下代码保存为 ch37_main.c,编译后运行即可看到二次/三次贝塞尔曲线与心形路径效果:
#include "xge.h"
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0x081114FF);
// 二次贝塞尔弧线
xge_shape_ex quad = NULL;
xgeShapeExCreate(&quad);
xgeShapeExMoveTo(quad, 80, 250);
xgeShapeExQuadTo(quad, 400, 20, 720, 250);
xgeShapeExStrokeColor(quad, 0x4FD8C2FF);
xgeShapeExStrokeWidth(quad, 3.0f);
xgeShapeExDraw(quad, 0.25f);
xgeShapeExDestroy(quad);
// 三次贝塞尔 S 曲线
xge_shape_ex s = NULL;
xgeShapeExCreate(&s);
xgeShapeExMoveTo(s, 80, 400);
xgeShapeExCubicTo(s, 250,250, 550,550, 720,400);
xgeShapeExStrokeColor(s, 0xFFB454FF);
xgeShapeExStrokeWidth(s, 3.0f);
xgeShapeExDraw(s, 0.25f);
xgeShapeExDestroy(s);
// 心形路径(填充)
xge_shape_ex heart = NULL;
xgeShapeExCreate(&heart);
xgeShapeExMoveTo(heart, 400, 560);
xgeShapeExCubicTo(heart, 150,400, 180,280, 400,340);
xgeShapeExCubicTo(heart, 620,280, 650,400, 400,560);
xgeShapeExClose(heart);
xgeShapeExFillColor(heart, 0xFF6B5EFF);
xgeShapeExDraw(heart, 0.25f);
xgeShapeExDestroy(heart);
return XGE_OK;
}
int main(void) {
xge_desc_t d = {0};
d.iWidth = 800;
d.iHeight = 600;
d.sTitle = "ch37 - Bezier Curve";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xgeRun(onFrame, NULL);
xgeUnit();
return 0;
}