網(wǎng)站首頁 編程語言 正文
程序截圖
簡單說明
這個函數(shù)就是
void drawBezierCurve(COLORREF color, const unsigned int len, ...)
color 是貝塞爾曲線的顏色,len 是畫出貝塞爾曲線所需要的點(diǎn)的個數(shù),最少 1 個,不要亂傳。之后的參數(shù)傳的就是畫出貝塞爾曲線要的點(diǎn),數(shù)據(jù)類型為 Vec2。
這個函數(shù)實(shí)現(xiàn)的基礎(chǔ)是參數(shù)方程,用參數(shù)方程將一條直線轉(zhuǎn)化為一個參數(shù)的方程,如:
A * x + B * y + C=0 可以轉(zhuǎn)化為 x = x0 - B * t;y = y0 + A * t,x0、y0 為直線上任意一個點(diǎn)的橫縱坐標(biāo)值,t 為未知參數(shù)。
對于一條線段,可以根據(jù)線段上兩個端點(diǎn)轉(zhuǎn)化為參數(shù)方程:
x = x0 + (x1 - x0) * t
y = y0 + (y1 - y0) * t
t ∈ [0, 1]
將這條線段分為 CURVEPIECE 份,t 從 0 到 1 一份一份地加,就能得到這條線段上均勻分布的 CURVEPIECE 個點(diǎn)。
貝塞爾曲線就是對 n 個點(diǎn)連線組成的 n 條(線段上對應(yīng)份的點(diǎn))的連線的 (n - 1) 條(線段的對應(yīng)份點(diǎn))的連線的……直到最后 1 條線段上(對應(yīng)份點(diǎn)的連線)。
這個曲線的算法如果用遞歸的話可能會占用很大內(nèi)存,畢竟每一輪的點(diǎn)的值都保存下來了,我這里用循環(huán)做,空間占用只有兩輪內(nèi)點(diǎn)的值。
代碼實(shí)現(xiàn)
// 程序:畫貝塞爾曲線的函數(shù)
// 編譯環(huán)境:Visual Studio 2019,EasyX_20211109
//
#include <graphics.h>
#include <conio.h>
using namespace std;
// 畫貝塞爾曲線的函數(shù),包括這個 Vec2 結(jié)構(gòu)體
struct Vec2
{
double x, y;
};
void drawBezierCurve(COLORREF color, const unsigned int len, ...)
{
if (len <= 0) return;
va_list list;
va_start(list, len);
Vec2* temp = new Vec2[len];
for (int i = 0; i < len; i++)
temp[i] = va_arg(list, Vec2);
va_end(list);
if (len == 1)
{
putpixel(temp->x, temp->y, color);
return;
}
Vec2* parent = nullptr, * child = nullptr;
Vec2 lastPoint = temp[0];
setlinecolor(color);
for (double LineNum = 0; LineNum < 1 + 1.0 / 100; LineNum += 1.0 / 100)
{
int size = len;
parent = temp;
while (size > 1)
{
child = new Vec2[size - 1];
for (int i = 0; i < size - 1; i++)
{
child[i].x = parent[i].x + (parent[i + 1].x - parent[i].x) * LineNum;
child[i].y = parent[i].y + (parent[i + 1].y - parent[i].y) * LineNum;
}
if (parent != temp)delete[] parent;
parent = child;
size--;
}
line(lastPoint.x, lastPoint.y, parent->x, parent->y);
lastPoint.x = parent->x;
lastPoint.y = parent->y;
delete[] parent;
parent = nullptr;
child = nullptr;
}
delete[] temp;
}
int main()
{
initgraph(640, 480);
Vec2 a = { 100, 80 };
Vec2 b = { 540, 80 };
Vec2 c = { 540, 400 };
Vec2 d = { 100, 400 };
setlinecolor(BLUE);
line(a.x, a.y, b.x, b.y);
line(b.x, b.y, c.x, c.y);
line(c.x, c.y, d.x, d.y);
drawBezierCurve(RED, 4, a, b, c, d);
_getch();
closegraph();
return 0;
}
原文鏈接:https://blog.csdn.net/yx5666/article/details/128315874
相關(guān)推薦
- 2022-04-22 SketchUp:解決鏡頭剪切屏幕出現(xiàn)破面視角的問題圖文教程
- 2023-08-01 elementui自定義Slider樣式
- 2023-10-24 Spring中的@Autowired和@Resource區(qū)別
- 2022-05-24 Asp.net?core?使用SignalR推送消息過程詳解_實(shí)用技巧
- 2022-06-01 Kubernetes集群的組成介紹_云和虛擬化
- 2022-09-29 Python模塊域名dnspython解析_python
- 2022-10-13 云服務(wù)器Windows?Server2012配置FTP服務(wù)器詳細(xì)圖文教程_FTP服務(wù)器
- 2022-05-22 Shell腳本一鍵安裝Nginx服務(wù)自定義Nginx版本_linux shell
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支