-
Notifications
You must be signed in to change notification settings - Fork 1
/
bezier.py
229 lines (183 loc) · 4.52 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#
# Demo program for drawing Bezier curve using Python
#
# $Id: bezier.py,v 1.2 2005/05/09 21:16:28 exact Exp $
#
# see http://www.doc.ic.ac.uk/~dfg/AndysSplineTutorial/Beziers.html
# for an interactive bezier drawing applet
#
# if use corelib, then do the following
#
# from corelib import *
# NT = BigFloat
#
# else use python's math library
#
# from math import *
# NT = float
#from corelib import *
#NT = BigFloat
from math import *
NT = float
from pylab import *
#from matplotlib.matlab import *
from matplotlib.pylab import *
# class Point
class Point:
def __init__(self, x = 0.0, y = 0.0):
self.x = x
self.y = y
def distance(self, p):
return sqrt((p.x-self.x)*(p.x-self.x)+(p.y-self.y)*(p.y-self.y))
def length(self):
return self.distance(Point(NT(0), NT(0)))
def __sub__(self, p):
return Point(self.x-p.x, self.y-p.y)
def __add__(self, p):
return Point(self.x+p.x, self.y+p.y)
def __mul__(self, c):
return Point(c*self.x, c*self.y)
def __eq__(self, p):
return self.x == p.x and self.y == p.y
def __ne__(self, p):
return not (self == p)
def towards(self, target, t):
if t == 0.5:
return self.halfway(target)
else:
return Point((1.0-t)*self.x+t*target.x, (1.0-t)*self.y+t*target.y)
def halfway(self, target):
return Point((self.x+target.x).div2(), (self.y+target.y).div2())
def compare_lex(self, p):
if self.x < p.x:
return -1
if self.x > p.x:
return 1
if self.y < p.y:
return -1
if self.y > p.y:
return 1
return 0
def less_lex(self, p):
return self.compare_lex(p) < 0
def less_eq_lex(self, p):
return self.compare_lex(p) <= 0
def __repr__(self):
return "Point(%s, %s)" % (self.x, self.y)
def orientation2d(a, b, c):
d1 = (a.x - b.x) * (a.y - c.y)
d2 = (a.y - b.y) * (a.x - c.x)
if d1 == d2:
return 0
elif d1 > d2:
return 1
else:
return -1
def leftTurn(a, b, c):
return orientation2d(a, b, c) > 0
def rightTurn(a, b, c):
return orientation2d(a, b, c) < 0
def betweenVar(a, b, c):
return (a.x-b.x)*(c.x-b.x)+(a.y-b.y)*(c.y-b.y)<0
# class CHull
class CHull:
def __init__(self, vp):
self.n = len(vp)
self.vp = vp
self.isConvex = False
self.diam = 0
def convexify(self):
pass
def diameter():
if self.diam == 0:
self.xmin = self.vp[0].x
self.xmax = self.xmin
self.ymin = self.vp[0].y
self.ymax = self.ymin
for p in self.vp:
if p.x < self.xmin:
self.xmin = p.x
if p.x > self.xmax:
self.xmax = p.x
if p.y < self.ymin:
self.ymin = p.y
if p.y > self.ymax:
self.ymax = p.y
self.diam = min(xmax-xmin, ymax-ymin)
# class Bezier
class Bezier(CHull):
def __init__(self, v):
self.deg = len(v) - 1
self.cp = v
self.tmin = NT(0)
self.tmax = NT(1)
def getPoint(self, t):
curr = [0]*self.deg
# get initial
for i in range(self.deg):
curr[i] = self.cp[i].towards(self.cp[i+1], t)
for i in range(self.deg-1):
for j in range(self.deg-1-i):
curr[j] = curr[j].towards(curr[j+1], t)
return curr[0]
def subdivision(self, t):
lseq = [0]*(self.deg+1)
rseq = [0]*(self.deg+1)
curr = [0.0]*self.deg
lseq[0] = self.cp[0]
rseq[self.deg] = self.cp[self.deg]
for i in range(self.deg):
curr[i] = self.cp[i].towards(self.cp[i+1], t)
for i in range(self.deg-1):
lseq[i+1] = curr[0]
rseq[self.deg-i-1] = curr[self.deg-i-1]
for j in range(self.deg-1-i):
curr[j] = curr[j].towards(curr[j+1], t)
lseq[self.deg] = curr[0]
rseq[0] = curr[0]
return [lseq, rseq]
def plotCP(cp):
x = []; y = []
for i in range(len(cp)):
x.append(cp[i].x)
y.append(cp[i].y)
print cp[i].x, cp[i].y
plot(x, y)
def plotBezier(bezier, n):
eps = NT(1)/n
x = []
y = []
t = 0
for i in range(n+1):
p = bezier.getPoint(t)
t = t + eps
x.append(p.x)
y.append(p.y)
plot(x, y)
def pt(x, y):
return Point(NT(x), NT(y))
def demo():
#vp = [pt(0, 0), pt(0.2, 1), pt(1, 1), pt(1, 0.2), pt(2, 0.2), pt(2, 1)]
#this next curve has a singularity:
vp = [pt(0,0), pt(1,0), pt(.5, -.5), pt(.5, .5)]
bc = Bezier(vp)
plotCP(vp)
plotBezier(bc, 100)
show()
def demoSubdivision():
vp = [pt(0, 0), pt(0.2, 1), pt(1, 1), pt(1, 0.2), pt(2, 0.2), pt(2, 1)]
bc = Bezier(vp)
[left, right] = bc.subdivision(0.4)
print "left=", left
print "right=", right
# plot original
plotCP(vp)
# plot left
plotCP(left)
plotBezier(Bezier(left), 100)
# plot left
plotCP(right)
plotBezier(Bezier(right), 100)
show()
if __name__ == "__main__":
demoSubdivision()