forked from vladamakaric/rasterToVector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawBezierFit.py
97 lines (68 loc) · 2.22 KB
/
drawBezierFit.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
90
91
92
93
94
95
96
97
import pygame
import pygame.gfxdraw
from utils import *
import bezier_fit
import bezier_curve
from vec2 import *
from math import pi
from copy import copy
import numpy as np
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
pygame.init()
size = [800, 640]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
done = False
def drawVector(origin, vec, color):
pygame.draw.line(screen, color, (origin.x, origin.y), (origin.x + vec.x, origin.y + vec.y))
def drawPoints(points, color):
for p in points:
pygame.draw.circle(screen, color, (p.x, p.y), 5, 0)
def discretizeBezierCurve(bezCP, pointNum):
return [bezier_curve.Q(bezCP, t) for t in np.linspace(0, 1, num=pointNum)]
def drawBezierCurve(bezCP, pointNum, color):
drawPoints(getIntVec2List(discretizeBezierCurve(bezCP, pointNum)), color)
points = getVec2ListFromTupleList([
(20, 20),
(93, 53),
(232, 89),
(323, 131),
(371, 212),
(497, 285),
(571, 293),
(673, 298)])
angles = np.linspace(np.pi, 0, num=10)
r = 300
tuples = zip( 300 + r*np.cos(angles), 300 + r*np.sin(angles) )
# halfCircle = getVec2ListFromTupleList(tuples)
# halfCBez = bezier_fit.getBezierFit(Vec2(0,1), Vec2(0,1), halfCircle)
# halfCircle = getIntVec2List(halfCircle)
bez2 = [Vec2(0,0), Vec2(0,300), Vec2(400,300), Vec2(400,0)]
# pnum = 4
#
# bez2RefP = discretizeBezierCurve(bez2, pnum)
# beziers = bezier_fit.getBezierFit(Vec2(0,1), Vec2(0,1), bez2RefP)
# t1, t2 = bezier_fit._getPathEndpointTangents(points)
beziers = bezier_fit.getBezierCurveListToFitPoints(points)
while not done:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
screen.fill(WHITE)
# drawBezierCurve(bez2, pnum, BLUE)
for bezCP in beziers:
pygame.gfxdraw.bezier(screen, getTupleListFromVec2List(bezCP), 30, RED)
drawPoints(points, BLUE)
# drawPoints(halfCircle, RED)
# pygame.gfxdraw.bezier(screen, getTupleListFromVec2List(halfCBez), 30, RED)
# pygame.gfxdraw.bezier(screen, [(0,0), (100,0), (320,640), (0,640)], 30, RED)
# pygame.gfxdraw.bezier(screen, getTupleListFromVec2List(bez), 30, RED)
# drawBezierCurve(halfCBez, 10, BLUE)
pygame.display.flip()
# Be IDLE friendly
pygame.quit()