This repository has been archived by the owner on May 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bezier.py
89 lines (79 loc) · 2.55 KB
/
bezier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File : bezier.py
@Time : 2020/12/16 20:50:32
@Author : Kearney
@Version : 0.0.0
@Contact : 191615342@qq.com
@License : GPL 3.0
@Desc : 贝塞尔曲线
用鼠标左键确定几个点,右键后画出由这些点确定的贝塞尔曲线
按下空格后重新开始取点
https://zhuanlan.zhihu.com/p/203408475
'''
import pygame
from pygame.locals import K_SPACE
import sys
import numpy as np
import time
LINECOLOR = (29, 244, 255) # 颜色设置
POINTCLOLOR = (0, 150, 150)
BACKYCOLOR = (255, 212, 238)
LINEWIDTH = 3 # 线宽
points = []
def B_nx(n, i, x):
if i > n:
return 0
elif i == 0:
return (1 - x)**n
elif i == 1:
return n * x * ((1 - x)**(n - 1))
return B_nx(n - 1, i, x) * (1 - x) + B_nx(n - 1, i - 1, x) * x
def get_value(p, canshu):
sumx = 0.
sumy = 0.
length = len(p) - 1
for i in range(0, len(p)):
sumx += (B_nx(length, i, canshu) * p[i][0])
sumy += (B_nx(length, i, canshu) * p[i][1])
return sumx, sumy
def get_newxy(p, x):
xx = [0] * len(x)
yy = [0] * len(x)
for i in range(0, len(x)):
print('x[i]=', x[i])
a, b = get_value(p, x[i])
xx[i] = a
yy[i] = b
print('xx[i]=', xx[i])
return xx, yy
pygame.init()
screen = pygame.display.set_mode((920, 640))
screen.fill(BACKYCOLOR)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: # 退出程序
pygame.quit()
sys.exit(0)
if event.type == pygame.MOUSEBUTTONDOWN:
if (pygame.mouse.get_pressed()[2]): # 鼠标右键按下绘制贝塞尔曲线
x = np.linspace(0, 1, 101)
xx, yy = get_newxy(points, x)
print(type(xx), len(xx), type(yy), len(yy))
for i in range(len(xx) - 1):
pygame.draw.line(screen, LINECOLOR, (xx[i], yy[i]),
(xx[i + 1], yy[i + 1]), LINEWIDTH)
time.sleep(0.01)
pygame.display.update()
print("R")
else: # 鼠标其它键按下时画点纪录
x, y = event.pos
pygame.draw.circle(screen, POINTCLOLOR, (x, y), 3, 0)
points.append([x, y])
print("L")
# 按下空格重新开始取点
if event.type == pygame.KEYDOWN and pygame.key.get_pressed()[K_SPACE]:
screen.fill(BACKYCOLOR)
points.clear()
pygame.display.update()