For even greater control of the curve arc, a Cubic Bezier curve (I like this one)
// Project: Cubic Bezier Curve
// Created: 2021-09-20
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Bezier Curve - Cubic" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 800, 600 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetClearColor(255,255,255)
SetPrintColor(0,0,0)
global v_start as tVec2
global v_end as tVec2
global v_control_start as tVec2
global v_control_end as tVec2
#Constant C_Red MakeColor(255,0,0, 255)
#Constant C_Green MakeColor(0,255,0, 255)
#Constant C_Blue MakeColor(0,0,255, 255)
v_start.x=100
v_start.y=500
v_control_start.x=250
v_control_start.y=100
v_end.x=500
v_end.y=200
v_control_end.x=400
v_control_end.y=100
mouse_offset_x=0
mouse_offset_y=0
start_move = 0
end_move = 0
control_start_move = 0
control_end_move = 0
do
// Draw points
DrawEllipse(v_start.x, v_start.y, 20, 20, C_Green, C_Green, 1)
DrawEllipse(v_end.x, v_end.y, 20, 20, C_Green, C_Green, 1)
DrawEllipse(v_control_start.x, v_control_start.y, 20, 20, C_Blue, C_Blue, 1)
DrawEllipse(v_control_end.x, v_control_end.y, 20, 20, C_Blue, C_Blue, 1)
DrawLine(v_start.x, v_start.y, v_control_start.x, v_control_start.y, 255, 255, 0)
DrawLine(v_end.x, v_end.y, v_control_end.x, v_control_end.y, 255, 255, 0)
// Draw curve
for t#=0 to 1.01 step 0.05
p as tVec2
p = Cubic(v_start, v_control_start, v_control_end, v_end, t#)
DrawEllipse(p.x, p.y, 5, 5, C_Red, C_Red, 1)
next
// Move curve points
mouse_x = GetPointerX()
mouse_y = GetPointerY()
x_len = Abs(v_start.x - mouse_x)
y_len = Abs(v_start.y - mouse_y )
if x_len < 20 and y_len < 20
DrawEllipse(v_start.x, v_start.y, 20, 20, C_Red, C_Red, 0)
if GetPointerPressed()
mouse_offset_x=x_len
mouse_offset_y=y_len
start_move=1
endif
endif
x_len = Abs(v_end.x - mouse_x)
y_len = Abs(v_end.y - mouse_y )
if x_len < 20 and y_len < 20
DrawEllipse(v_end.x, v_end.y, 20, 20, C_Red, C_Red, 0)
if GetPointerPressed()
mouse_offset_x=x_len
mouse_offset_y=y_len
end_move=1
endif
endif
x_len = Abs(v_control_start.x - mouse_x)
y_len = Abs(v_control_start.y - mouse_y )
if x_len < 20 and y_len < 20
DrawEllipse(v_control_start.x, v_control_start.y, 20, 20, C_Red, C_Red, 0)
if GetPointerPressed()
mouse_offset_x=x_len
mouse_offset_y=y_len
control_start_move=1
endif
endif
x_len = Abs(v_control_end.x - mouse_x)
y_len = Abs(v_control_end.y - mouse_y )
if x_len < 20 and y_len < 20
DrawEllipse(v_control_end.x, v_control_end.y, 20, 20, C_Red, C_Red, 0)
if GetPointerPressed()
mouse_offset_x=x_len
mouse_offset_y=y_len
control_end_move=1
endif
endif
if GetPointerReleased()
start_move=0
end_move=0
control_start_move=0
control_end_move=0
endif
if GetPointerState()
if start_move
v_start.x = mouse_x-mouse_offset_x
v_start.y = mouse_y-mouse_offset_y
endif
if end_move
v_end.x = mouse_x-mouse_offset_x
v_end.y = mouse_y-mouse_offset_y
endif
if control_start_move
v_control_start.x = mouse_x-mouse_offset_x
v_control_start.y = mouse_y-mouse_offset_y
endif
if control_end_move
v_control_end.x = mouse_x-mouse_offset_x
v_control_end.y = mouse_y-mouse_offset_y
endif
endif
Print("Click N Drag point to move it!")
Sync()
loop
Type tVec2
x as float
y as float
EndType
function Lerp( startValue as float, endValue as float , value as float )
result# = ((1.0 - value) * startValue) + (value * endValue)
endfunction result#
Function Quadratic(a as tVec2, b as tVec2, c as tVec2, t as float)
p as tVec2
x1#=Lerp( a.x, b.x , t)
y1#=Lerp( a.y, b.y , t)
x2#=Lerp(b.x, c.x, t)
y2#=Lerp(b.y, c.y, t)
p.x=Lerp(x1#, x2#, t)
p.y=Lerp(y1#, y2#, t)
EndFunction p
Function Cubic(a as tVec2, b as tVec2, c as tVec2, d as tVec2, t as float)
p as tVec2
aa as tVec2
bb as tVec2
aa=Quadratic(a,b,c, t)
bb=Quadratic(b,c,d, t)
p.x=Lerp(aa.x, bb.x, t)
p.y=Lerp(aa.y, bb.y, t)
EndFunction p