Post by Nicky Peter Hollyoake on Aug 11, 2016 18:21:38 GMT -5
I'm not sure if this has already been done, but any how I made my version for a bezier curve.
The code I've done makes a circle without using the functions sin/cos (actually, its kind of a failed circle ;/). I am going to be writing a program using thisd method to show some useful ways to use it, so expect a post when I get around to it.
Here is what I have.
Any suggestions or you think any of my Math is off, let me know as I did try to do as much research as I could on it before doing this.
Thank you
Nicky
EDIT:
Forgot to mention, click on the points and move around to watch the shape change.
The code I've done makes a circle without using the functions sin/cos (actually, its kind of a failed circle ;/). I am going to be writing a program using thisd method to show some useful ways to use it, so expect a post when I get around to it.
Here is what I have.
'BEZIER CURVE
'screen setup
dim w=800, h=600
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluOrTho2d(0, w, h, 0)
glMatrixMode(GL_MODELVIEW)
glLoadidentity()
glDisable(GL_DEPTH_TEST)
'declare sub/funcs
declare sub drawBezier(s#(), e#(), c#(), iterations)
'setup
const MAX_B=1000
'bezier stuff
struc sBezier
dim s#(1) 'start point
dim e#(1) 'end point
dim c#(1) 'control point
endstruc
dim sBezier b(MAX_B)
dim bCount=3
'mouse stuff
dim m#(1) 'x/y
dim m_c=false 'mouse clicked
dim m_p_c=0 'control point clicked
'other
dim j, i
b(0).s# = vec2(50, 300) : b(0).e# = vec2(250, 100): b(0).c# = vec2(50, 100)
b(1).s# = vec2(250, 100): b(1).e# = vec2(450, 300): b(1).c# = vec2(450, 100)
b(2).s# = vec2(450, 300): b(2).e# = vec2(250, 500): b(2).c# = vec2(450, 500)
b(3).s# = vec2(250, 500): b(3).e# = vec2(50, 300): b(3).c# = vec2(50, 500)
textmode(TEXT_OVERLAID)
while true
glClear(GL_COLOR_BUFFER_BIT)
m#=vec2(mouse_x()*w, mouse_y()*h)
'draw control points
for i = 0 to bCount
if m_c=false then
if sqr(pow(m#(0)-b(i).c#(0), 2) + pow(m#(1)-b(i).c#(1), 2)) <= 3 and mouse_button (MOUSE_LBUTTON) then
m_c=true
m_p_c=i
endif
else
if not mouse_button (MOUSE_LBUTTON) then
m_c=false
endif
endif
if m_c then
b(m_p_c).c#=m#
endif
glBegin(GL_TRIANGLE_FAN)
glVertex2fv(b(i).c#)
for j = 0 to 360 step 20
glVertex2fv(b(i).c# + vec2(sind(j)*3, cosd(j)*3))
next
glEnd()
drawBezier(b(i).s#, b(i).e#, b(i).c#, 12)
next
drawtext()
swapbuffers()
wend
sub drawBezier(s#(), e#(), c#(), iterations)
dim t1#(1), t2#(1), t3#(1)
dim i#
dim n=0
glBegin(GL_LINE_STRIP)
while n < iterations + 1
i# = (n*1.0)/iterations
t1#= s# + (c#-s#) * i#
t2#= c# + (e#-c#) * i#
t3#= t1# + (t2#-t1#) * i#
glVertex2fv(t3#)
n=n+1
wend
glEnd()
endsub
Any suggestions or you think any of my Math is off, let me know as I did try to do as much research as I could on it before doing this.
Thank you
Nicky
EDIT:
Forgot to mention, click on the points and move around to watch the shape change.