[英]How to get the points of a bezier curve/parabola using 3 unique points on a graph
[英]Bezier Curve using static points
所以我发现这个代码在线,它使用随机Bezier曲线,它使用随机点。 我试图让它非随机,以便它使用静态点我得到它只使用4点这很容易。 我之前从未在python中使用过PIL,事实上我正在慢慢学习python。 我只做了前端工作(html,javascript,css等),我只是想知道是否有人可以帮助我。 这是我在线找到的代码:
# Random Bezier Curve using De Casteljau's algorithm
# http://en.wikipedia.org/wiki/Bezier_curve
# http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
# FB - 201111244
import random
from PIL import Image, ImageDraw
imgx = 500
imgy = 500
image = Image.new("RGB", (imgx, imgy))
draw = ImageDraw.Draw(image)
def B(coorArr, i, j, t):
if j == 0:
return coorArr[i]
return B(coorArr, i, j - 1, t) * (1 - t) + B(coorArr, i + 1, j - 1, t) * t
n = 4 # number of control points
coorArrX = []
coorArrY = []
for k in range(n):
x = (0, imgx - 1)
y = (0, imgy - 1)
coorArrX.append(x)
coorArrY.append(y)
# plot the curve
numSteps = 10000
for k in range(numSteps):
t = float(k) / (numSteps - 1)
x = int(B(coorArrX, 0, n - 1, t))
y = int(B(coorArrY, 0, n - 1, t))
try:
image.putpixel((x, y), (0, 255, 0))
except:
pass
# plot the control points
cr = 3 # circle radius
for k in range(n):
x = coorArrX[k]
y = coorArrY[k]
try:
draw.ellipse((x - cr, y - cr, x + cr, y + cr), (255, 0, 0))
except:
pass
# image.save("BezierCurve.png", "PNG")
image.show() I add this so I can see it right away
任何帮助,如果有的话会很棒。
好的,开始这一切的长期详细的BS都在长线以下。 得到的答案就在这里。
你的静态点是x,y坐标,x值和y值放在单独的数组中(分别是coorArrx和coorArrY),确保永远不要使用value = imgx或imy。
# Random Bezier Curve using De Casteljau's algorithm
# http://en.wikipedia.org/wiki/Bezier_curve
# http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
# FB - 201111244
import random
from PIL import Image, ImageDraw
imgx = 500
imgy = 500
image = Image.new("RGB", (imgx, imgy))
draw = ImageDraw.Draw(image)
def B(coorArr, i, j, t):
if j == 0:
return coorArr[i]
return B(coorArr, i, j - 1, t) * (1 - t) + B(coorArr, i + 1, j - 1, t) * t
# n = random.randint(3, 6) # number of control points
n=4
#coorArrX = []
#coorArrY = []
#for k in range(n):
# x = random.randint(0, imgx - 1)
# y = random.randint(0, imgy - 1)
# coorArrX.append(x)
# coorArrY.append(y)
coorArrX=[3,129,12,77]
coorArrY=[128,52,12,491]
# plot the curve
numSteps = 10000
for k in range(numSteps):
t = float(k) / (numSteps - 1)
x = int(B(coorArrX, 0, n - 1, t))
y = int(B(coorArrY, 0, n - 1, t))
try:
image.putpixel((x, y), (0, 255, 0))
except:
pass
# plot the control points
cr = 3 # circle radius
for k in range(n):
x = coorArrX[k]
y = coorArrY[k]
try:
draw.ellipse((x - cr, y - cr, x + cr, y + cr), (255, 0, 0))
except:
pass
image.show()
= ................................................. ........................................ =我也是所有人的新手这一点,我拒绝看到这一点,因为我看到它就像你做...一个学习经验。
但是当我看到这段代码时,我发现了一些奇怪的东西
for k in range(n):
x = (0, imgx - 1)
y = (0, imgy - 1)
coorArrX.append(x)
coorArrY.append(y)
你确定这部分是正确的吗? imgx在其他地方定义为500,n为4.所以这可以解读为
for k in range(4):
x = (0, 500 - 1)
y = (0, 500 - 1)
哪个(因为这些值在此代码中根本不会改变)意味着:
x = (0, 499)
y = (0, 499)
在每一次通过。 所以每次他们到达:
coorArrX.append(x)
coorArrY.append(y)
他们只是不断向数组添加相同数据的新副本,所以当完成后,数组看起来像这样(内部)
[(0, 499), (0, 499), (0, 499), (0,499)]
更令人困惑的是,coorArrX和coorArrY是A)相同的,和B)基本部分相同(即每个元素是相同的)。 因此,当你到达这部分代码时:
# plot the control points
cr = 3 # circle radius
for k in range(n):
x = coorArrX[k]
y = coorArrY[k]
try:
draw.ellipse((x - cr, y - cr, x + cr, y + cr), (255, 0, 0))
except:
pass
并且你在数组中的值中替换,得到:
# plot the control points
cr = 3 # circle radius
for k in range(n):
x = coorArrX[k]
y = coorArrY[k]
try:
draw.ellipse(((0, 499) - 3, (0, 499) - 3, (0, 499) + 3, (0, 499) + 3), (255, 0, 0))
except:
pass
现在这是控制绘图曲线段的部分,但是我没有看到elispe在那些不可能的坐标集上的中心如何可以绘制任何东西?!
打破了并进行了复制粘贴测试运行。
这段代码纯粹是虚假的,要么是为了欺骗人们浪费时间,要么是因为同样的原因而放置在OP的地方。
但这很有趣!
从您的描述中,唯一的问题似乎是Python基础知识。 我已按如下方式重新排列代码,因此唯一需要触及的内容是在底部。 现在,如果你想手动指定4个控制点,请继续执行(在下面的代码中,我自己指定了4个控制点作为示例)。 您需要了解,在原始代码中, coorArrX
和coorArrY
只是列表,每个列表将分别保持4个点(分别为x和y坐标)。 如果您手动指定它们,则使用循环来编写它们是没有意义的。 我希望这段代码足够清晰:
# Random Bezier Curve using De Casteljau's algorithm
# http://en.wikipedia.org/wiki/Bezier_curve
# http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
# FB - 201111244
from PIL import Image, ImageDraw
def plot_curve(image, px, py, steps=1000, color=(0, 255, 0)):
def B(coord, i, j, t):
if j == 0:
return coord[i]
return (B(coord, i, j - 1, t) * (1 - t) +
B(coord, i + 1, j - 1, t) * t)
img = image.load()
for k in range(steps):
t = float(k) / (steps - 1)
x = int(B(px, 0, n - 1, t))
y = int(B(py, 0, n - 1, t))
try:
img[x, y] = color
except IndexError:
pass
def plot_control_points(image, px, py, radi=3, color=(255, 0, 0)):
draw = ImageDraw.Draw(image)
for x, y in zip(px, py):
draw.ellipse((x - radi, y - radi, x + radi, y + radi), color)
# Your fixed, manually specified, points.
n = 4
coord_x = [25, 220, 430, 410]
coord_y = [250, 10, 450, 40]
image = Image.new("RGB", (500, 500))
plot_curve(image, coord_x, coord_y)
plot_control_points(image, coord_x, coord_y)
image.save("BezierCurve.png")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.