Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: Shockwave on September 11, 2006

Title: Beziers.
Post by: Shockwave on September 11, 2006
Looking at the electricity thread, I can see some great examples but they could be better with some sort of cubic curve equation so I coded this for you to have a play with.


'
' Bezier Curve Routine By Shockwave ^ Dbf ^ S!P 2006
' Now I want to see some real Electricity :P
'
'
OPTION STATIC
OPTION EXPLICIT

    #define PTC_WIN       
    #Include Once "tinyptc.bi"

    CONST XRES = 640
    CONST YRES = 480
   
    dim shared buffer (xres * yres ) as uinteger
   
    declare sub BEZIER(BYVAL X0 AS INTEGER ,BYVAL Y0 AS INTEGER, BYVAL X3 AS INTEGER ,BYVAL Y3 AS INTEGER ,BYVAL X1 AS INTEGER ,BYVAL Y1 AS INTEGER ,BYVAL X2 AS INTEGER ,BYVAL Y2 AS INTEGER )
    If( ptc_open( "BEZIER CURVE EXAMPLE BY SHOCKWAVE OF DBF AND S!P", XRES, YRES ) = 0 ) Then
    End -1
End If
dim as integer xx0,yy0,xx1,yy1,xx2,yy2,xx3,yy3,gadd
DO
    gadd=gadd+3
   
    xx0=100+50*sin(gadd/120)
    yy0=240+50*cos(gadd/323)

    xx3=540+50*sin(gadd/193)
    yy3=240+50*cos(gadd/223)

    xx1=320+250*sin(gadd/403)
    yy1=240+150*cos(gadd/523)

    xx2=320+250*sin(gadd/203)
    yy2=240+150*cos(gadd/123)



        BEZIER( xx0,yy0, xx1,yy1 , xx2,yy2 , xx3,yy3)
       
       
    PTC_UPDATE@BUFFER(0)
    erase buffer
LOOP UNTIL INKEY$=CHR$(27)
END

'
' USAGE, BEZIER(SOURCE X,SOURCE Y , DEST X , DEST Y, CONTROL X1 , CONTROL Y1 , CONTROL X2, CONTROL Y2)
'

SUB BEZIER(BYVAL X0 AS INTEGER ,BYVAL Y0 AS INTEGER, BYVAL X3 AS INTEGER ,BYVAL Y3 AS INTEGER ,BYVAL X1 AS INTEGER ,BYVAL Y1 AS INTEGER ,BYVAL X2 AS INTEGER ,BYVAL Y2 AS INTEGER )
dim as double T
DIM AS INTEGER L,X,Y,cx,bx,ax,cy,by,ay

CX = 3*(X1-X0)
BX = 3*(X2-X1)-CX
AX = X3-X0-CX-BX
CY=3*(Y1-Y0)
BY=3*(Y2-Y1)-CY
AY=Y3-Y0-CY-BY

for t = 0 to 1 step .0001
        x = ax * t ^ 3 + bx * t ^ 2 + cx * t + X0
        y = ay * t ^ 3 + by * t ^ 2 + cy * t + Y0
        buffer(X+(Y*XRES)) = &HFFFFFF
next

END SUB
Title: Re: Beziers.
Post by: Shockwave on September 11, 2006
I should explain how to use the source above.
You need to call the sub Bezier and you supply it 4 pairs of co-ordinates, the first two pairs are the start and end. The other two are control points which will pull your curve so it is better than a simple parabolic curve as you can change direction twice with this routine.

Also the loop, change the step size for t as the smaller it is, the more iterations it will use to work out your curve.
Title: Re: Beziers.
Post by: ninogenio on September 11, 2006
cool and actually much simpler than i thought to do :cheers:
Title: Re: Beziers.
Post by: psygate on September 11, 2006
Nice work! Keep it up! (Dont know if this is the right phrase  ::))