# ------------------------- 绘制二阶贝塞尔 -------------------------#
def QuadraticBezier(self,ps, pc, pe, t):
'''
获取二阶贝塞尔点
:param ps: 开始点
:param pc: 控制点
:param pe: 结束点
:param t: 0-1
:return:
'''
return pow(1 - t, 2) * ps + 2 * t * (1 - t) * pc + pow(t, 2) * pe
def drawQuadraticBezier(self,ele):
# 开始点
ps = np.array([ele.start.real, ele.start.imag])
# 控制点
p = np.array([ele.control.real, ele.control.imag])
# 结束点
pe = np.array([ele.end.real, ele.end.imag])
point = self.QuadraticBezier(ps, p, pe, 0)
start = int(point[0]), int(point[1])
z = 30.25
# 创建点
point = {'x': start[0], 'y': start[1], 'z': z, 'type': TYPE.MOVE, 'pos': len(self.points)}
# 添加到容器中
self.points.append(point)
# 40个点
for i in range(1, 41):
result = self.QuadraticBezier(ps, p, pe, i / 40)
end = int(result[0]), int(result[1])
# 创建点
point = {'x': end[0], 'y': end[1], 'z': z, 'type': TYPE.MOVE, 'pos': len(self.points)}
# 添加到容器中
self.points.append(point)
# 连接两个点
# cv.line(self.dst, start, end, self.randomColor())
# cv.imshow('dst', self.dst)
# cv.waitKey(5)
# 开始点变成结束点
#start = end
# ------------------------- 三阶贝塞尔 -------------------------#
def CubicBezier(self,ps, pc1, pc2, pe, t):
'''
获取二阶贝塞尔点
:param ps: 开始点
:param pc: 控制点
:param pe: 结束点
:param t: 0-1
:return:
'''
return pow(1 - t, 3) * ps + 3 * t * pow(1 - t, 2) * pc1 + 3 * pow(t, 2) * (1 - t) * pc2 + pow(t, 3) * pe
def drawCubicBezier(self,ele):
print('绘制贝塞尔')
# 开始点
ps = np.array([ele.start.real, ele.start.imag])
# 控制点
p1 = np.array([ele.control1.real, ele.control1.imag])
p2 = np.array([ele.control2.real, ele.control2.imag])
# 结束点
pe = np.array([ele.end.real, ele.end.imag])
result = self.CubicBezier(ps, p1, p2, pe, 0)
print(result)
start = int(result[0]), int(result[1])
z = 30.25
# 创建点
point = {'x': start[0], 'y': start[1], 'z': z, 'type': TYPE.MOVE, 'pos': len(self.points)}
# 添加到容器中
self.points.append(point)
# 40个点
for i in range(1, 41):
result = self.CubicBezier(ps, p1, p2, pe, i / 40)
end = int(result[0]), int(result[1])
# 创建点
point = {'x': end[0], 'y': end[1], 'z': z, 'type': TYPE.MOVE, 'pos': len(self.points)}
# 添加到容器中
self.points.append(point)
# 连接两个点
# cv.line(self.dst, start, end, self.randomColor())
# cv.imshow('dst', self.dst)
# cv.waitKey(5)
# # 开始点变成结束点
# start = end