당신이하려는 것은 포인트를 보간하는 부드러운 곡선을 만드는 것입니다. 이렇게하려면 베 지어 곡선에 대해 다음을 이해해야합니다.
점 A1, A2, A3, A4 및 B1, B2, B3 및 B4가있는 두 개의 곡선이 있다고 가정 해보십시오.
두 점의 곡선이 같은 점에서 끝나고 첫 번째 점의 마지막 제어점이 다음 점의 첫 번째 제어점과 동일 직선 인 경우 곡선이 매끄 럽습니다.그래서이 예에서, IF :
- A4 == B1
- A3, A4, 및 B2가 동일 선상에있는 (A3, B1, B2 말하는 동일 동일 직선이다)
이어서 곡선 것 매끄럽다.
임의의 점 목록을 취하여 부드러운 곡선을 만들려면이 두 조건을 true로 설정해야합니다.
이 작업을 수행하기 위해, 우리는 점의 집합으로 시작 말할 수 있습니다 :
위의 조건을 강제로 몇 가지 여분의 포인트를 추가 할 수 있습니다. 같이 우리는 다른 모든 쌍 중 중간에서 새로운 점을 배치합니다 :
우리는 지금 등의 포인트 0-3, 3-6, 6-9, 사이 베 지어 곡선을 그릴 수 있습니다 이 도움이
희망 : 우리는 부드러운 곡선을 형성시킬 것이라는 점을 확실 할 수있다!
편집 : 다음은 위에 표시된 것과 정확히 똑같은 것을 구현하는 간단한 파이썬 프로그램입니다. 파이썬과 PIL이 설치되어 있어야합니다 :
from PIL import Image
import math
#
# draws a single point on our image
#
def drawPoint(img, loc, size=5, color=(0,0,0)):
px = img.load()
for x in range(size):
for y in range(size):
xloc = loc[0] + x - size/2
yloc = loc[1] + y - size/2
px[ xloc, yloc ] = color
#
# draws a simple bezier curve with 4 points
#
def drawCurve(img, points):
steps = 20
for i in range(steps):
t = i/float(steps)
xloc = math.pow(1-t,3) * points[0][0] \
+ 3*t*math.pow(1-t,2) * points[1][0] \
+ 3*(1-t)*math.pow(t,2) * points[2][0] \
+ math.pow(t,3) * points[3][0]
yloc = math.pow(1-t,3) * points[0][1] \
+ 3*t*math.pow(1-t,2) * points[1][1] \
+ 3*(1-t)*math.pow(t,2) * points[2][1] \
+ math.pow(t,3) * points[3][1]
drawPoint(img, (xloc,yloc), size=2)
#
# draws a bezier curve with any number of points
#
def drawBezier(img, points):
for i in range(0,len(points),3):
if(i+3 < len(points)):
drawCurve(img, points[i:i+4])
#
# draws a smooth bezier curve by adding points that
# force smoothness
#
def drawSmoothBezier(img, points):
newpoints = []
for i in range(len(points)):
# add the next point (and draw it)
newpoints.append(points[i])
drawPoint(img, points[i], color=(255,0,0))
if(i%2 == 0 and i>0 and i+1<len(points)):
# calculate the midpoint
xloc = (points[i][0] + points[i+1][0])/2.0
yloc = (points[i][1] + points[i+1][1])/2.0
# add the new point (and draw it)
newpoints.append((xloc, yloc))
drawPoint(img, (xloc, yloc), color=(0,255,0))
drawBezier(img, newpoints)
# Create the image
myImage = Image.new("RGB",(627,271),(255,255,255))
# Create the points
points = [ (54,172),
(121,60),
(220,204),
(284,56),
(376,159),
(444,40),
(515,228),
(595,72) ]
# Draw the curve
drawSmoothBezier(myImage, points)
# Save the image
myImage.save("myfile.png","PNG")
라인은 포인트 패턴을 따릅니다. 결과가 지그재그 인 경우 라인이 어떻게 보이기 때문입니다.
안녕하세요 Stargazer712, 귀하의 의견을 기반으로 새로운 질문을 올렸습니다. 내 원래 질문 아래의 게시물을 참조하십시오. 감사합니다. – q0987
@ q0987 - 베 지어 곡선을 그리는 프로그램은 매우 간단합니다. 나는 당신이 가질 수있는 의문에 대한 답이 하나가되기를 바랍니다. – riwalk
큰 도움을 주셔서 대단히 감사합니다. 코딩 스타일이 완벽 해 보입니다. – q0987