-- 这是代码,写得比较随意,不是很好。
IDLE = 0
DRAW_START = 1
DRAW_MOVING = 2
DRAW_END = 3
EDIT = 4
function setup()
print("三阶贝塞尔曲线")
print("拖动创建线条")
print("移动控制点调整曲线")
controlPoints = {}
size = 35 -- 控制点大小
status = IDLE
selected = nil
end
function draw()
background(255)
if status == DRAW_START then
status = IDLE
sp = touchPos
elseif status == DRAW_MOVING then
styleLine(sp,touchPos,color(0, 75),8)
elseif status == DRAW_END then
ep = touchPos
cp = CP(sp,ep) -- 计算控制点
table.insert(controlPoints,{p0 = sp,p1 = ep,cp1 = cp[1],cp2 = cp[2],b={}})
for i = 0,1,0.05 do
local cpoint = controlPoints[#controlPoints]
table.insert(cpoint.b,calculatebezier(i,cpoint.p0,cpoint.p1,cpoint.cp1,cpoint.cp2))
end
status = IDLE
end
if selected ~= nil then
if selected.y == 1 then
controlPoints[selected.x].cp1 = touchPos
elseif selected.y == 2 then
controlPoints[selected.x].cp2 = touchPos
elseif selected.y == 3 then
controlPoints[selected.x].p0 = touchPos
elseif selected.y == 4 then
controlPoints[selected.x].p1 = touchPos
end
end
for k,v in pairs(controlPoints) do
for i = 1,#v.b - 1 do
for t = 0,20 do
v.b[t+1] = calculatebezier(t/20,v.p0,v.p1,v.cp1,v.cp2)
end
styleLine(v.b[i],v.b[i + 1],color(0),8)
end
styleLine(v.p0,v.cp1,color(129, 65),8)
styleLine(v.p1,v.cp2,color(129, 65),8)
drawPoints(v,color(255),size)
end
end
IDLE = 0
DRAW_START = 1
DRAW_MOVING = 2
DRAW_END = 3
EDIT = 4
function setup()
print("三阶贝塞尔曲线")
print("拖动创建线条")
print("移动控制点调整曲线")
controlPoints = {}
size = 35 -- 控制点大小
status = IDLE
selected = nil
end
function draw()
background(255)
if status == DRAW_START then
status = IDLE
sp = touchPos
elseif status == DRAW_MOVING then
styleLine(sp,touchPos,color(0, 75),8)
elseif status == DRAW_END then
ep = touchPos
cp = CP(sp,ep) -- 计算控制点
table.insert(controlPoints,{p0 = sp,p1 = ep,cp1 = cp[1],cp2 = cp[2],b={}})
for i = 0,1,0.05 do
local cpoint = controlPoints[#controlPoints]
table.insert(cpoint.b,calculatebezier(i,cpoint.p0,cpoint.p1,cpoint.cp1,cpoint.cp2))
end
status = IDLE
end
if selected ~= nil then
if selected.y == 1 then
controlPoints[selected.x].cp1 = touchPos
elseif selected.y == 2 then
controlPoints[selected.x].cp2 = touchPos
elseif selected.y == 3 then
controlPoints[selected.x].p0 = touchPos
elseif selected.y == 4 then
controlPoints[selected.x].p1 = touchPos
end
end
for k,v in pairs(controlPoints) do
for i = 1,#v.b - 1 do
for t = 0,20 do
v.b[t+1] = calculatebezier(t/20,v.p0,v.p1,v.cp1,v.cp2)
end
styleLine(v.b[i],v.b[i + 1],color(0),8)
end
styleLine(v.p0,v.cp1,color(129, 65),8)
styleLine(v.p1,v.cp2,color(129, 65),8)
drawPoints(v,color(255),size)
end
end