[英]How Can I Make a Thicker Bezier in Pygame?
我正在 Pygame 中構建一個專門的節點編輯器。 每個節點將與一條貝塞爾曲線相連。 該曲線是通過首先單擊一個節點來構建的。 在鼠標 cursor 和節點之間繪制貝塞爾曲線,單擊第二個節點后,貝塞爾曲線就固定了。 我的代碼已經可以繪制曲線並跟隨鼠標 cursor。 我的問題是曲線太細了。 有誰知道在 pygame.gfxdraw.bezier 中輕松指定寬度的方法? 另外,我不知道參數“6”對應的是什么; 我只知道沒有它的代碼不會 function 。
# This draws the bezier curve for the node editor
x, y = pygame.mouse.get_pos()
b_points = [(380,390),(410,385),(425,405), (425, y), (x, y)]
pygame.gfxdraw.bezier(screen, b_points, 6, blue)
簡單的回答:你不能,至少不能用pygame.gfxdraw
或pygame.draw
。 你必須自己做。 沿曲線計算點並將它們與pygame.draw.lines
連接。
請參閱在 Bézier 曲線上找到一個點:De Casteljau 的算法並創建一個 function,按點繪制一個貝塞爾曲線點:
import pygame
def ptOnCurve(b, t):
q = b.copy()
for k in range(1, len(b)):
for i in range(len(b) - k):
q[i] = (1-t) * q[i][0] + t * q[i+1][0], (1-t) * q[i][1] + t * q[i+1][1]
return round(q[0][0]), round(q[0][1])
def bezier(surf, b, samples, color, thickness):
pts = [ptOnCurve(b, i/samples) for i in range(samples+1)]
pygame.draw.lines(surf, color, False, pts, thickness)
最小的例子:
import pygame, pygame.gfxdraw
def ptOnCurve(b, t):
q = b.copy()
for k in range(1, len(b)):
for i in range(len(b) - k):
q[i] = (1-t) * q[i][0] + t * q[i+1][0], (1-t) * q[i][1] + t * q[i+1][1]
return round(q[0][0]), round(q[0][1])
def bezier(surf, b, samples, color, thickness):
pts = [ptOnCurve(b, i/samples) for i in range(samples+1)]
pygame.draw.lines(surf, color, False, pts, thickness)
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
x, y = pygame.mouse.get_pos()
b_points = [(380,390), (410,385), (425,405), (425, y), (x, y)]
screen.fill(0)
bezier(screen, b_points, 20, (255, 255, 0), 6)
pygame.draw.lines(screen, (255, 255, 255), False, b_points, 1)
pygame.gfxdraw.bezier(screen, b_points, 6, (255, 0, 0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.