Bspline

Share your advanced PureBasic knowledge/code with the community.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 566
Joined: Tue Jan 04, 2011 6:21 pm

Bspline

Post by SPH »

Hi,
All about Bspline.

Enjoy (PB6.21b9)

Code: Select all

; Real BSpline code by Crisot / SPH
sph_nb=2000

Dim x.f(sph_nb)
Dim y.f(sph_nb)


InitSprite() 
InitKeyboard() 
InitMouse() 

ExamineDesktops()

ddw = DesktopWidth(0)
ddh = DesktopHeight(0)

OpenScreen(ddw,ddh,32,"Bezier") 

cw = RGB(255,255,255) 
cr = RGB(255,0,0) 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Exemple=Random(12)+1
nombre=4+Random(10)
synchro=Random(1)

For i=0 To sph_nb
  x(i)=Random(ddw-1)
  y(i)=Random(ddh-1)
Next

;Exemple 1: 4 points de base (comme le mec), qui tracent une vrai bspline
;Exemple 2: Pareil que le 1, mais la courbe passe par les points début et fin
;  grace au triplage des points début et fin
;Exemple 3: 4 points en carré, qui tracent un quart de cercle
;Exemple 4: 7 points en carré, qui tracent cercle complet
;Exemple 5: Plein de points au pif
;Exemple 6: Plein de points au pif, courbe qui passe par les points début et fin

;synchro = 1: sert juste aux courbes qui passent par les points début et fin, car
;  pour ces courbes, les points sont répétés 3 fois, et il faut pouvoir déplacer les 3
;  en même temps, sinon ça nique tout :)

Procedure bspline(x0,y0,x1,y1,x2,y2,x3,y3,color) 

  For i = 0 To 100

    t1.f = i/100
    t2.f = t1*t1
    t3.f = t1*t1*t1

  	k1.f = 1 - 3*t1 + 3*t2 - t3
    k2.f = 4 - 6*t2 + 3*t3
  	k3.f = 1 + 3*t1 + 3*t2 - 3*t3

    xfinal.f = (x0 * k1 + x1 * k2 + x2 * k3 + x3 * t3) / 6
		yfinal.f = (y0 * k1 + y1 * k2 + y2 * k3 + y3 * t3) / 6

    Plot (xfinal,yfinal,color)   

  Next I


EndProcedure 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Repeat 
  ClearScreen(0) 
  
  ExamineKeyboard() 
  ExamineMouse() 

  If MouseButton(1) And mousepush=0
    key=key+1 : key=key%nombre
    MouseLocate(x(key),y(key))
  EndIf 
  mousepush=MouseButton(1) 

  x(key)=MouseX() : y(key)=MouseY() ; Déplacement du point

  If synchro=1            ; Juste une gruge pour bouger les points, rien à voir avec la courbe.
    x(1)=x(0) : y(1)=y(0)
    x(2)=x(0) : y(2)=y(0)
    x(nombre-1)=x(nombre-3) : y(nombre-1)=y(nombre-3)
    x(nombre-2)=x(nombre-3) : y(nombre-2)=y(nombre-3)
  EndIf

  StartDrawing(ScreenOutput())
  DrawingMode(4)
  For n=0 To nombre-1   ; On trace les cercles...
    If n=key : Circle(x(n),y(n),5,cr) : Else : Circle(x(n),y(n),5,cw) : EndIf
  Next
  For n=0 To nombre-4   ; On trace la bspline
    bspline(x(n),y(n),x(n+1),y(n+1),x(n+2),y(n+2),x(n+3),y(n+3),cw)
  Next
  StopDrawing() 

  FlipBuffers() 
  
Until KeyboardPushed(1)


Last edited by SPH on Tue May 20, 2025 7:06 pm, edited 2 times in total.

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Re: Bspline

Post by threedslider »

Your program doesn't work and crashed directly :( :|
User avatar
SPH
Enthusiast
Enthusiast
Posts: 566
Joined: Tue Jan 04, 2011 6:21 pm

Re: Bspline

Post by SPH »

Good on PB 6.21b9

but bad on others versions... I don't know why :idea:

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Bspline

Post by Caronte3D »

Works on beta 6 as well
User avatar
SPH
Enthusiast
Enthusiast
Posts: 566
Joined: Tue Jan 04, 2011 6:21 pm

Re: Bspline

Post by SPH »

The curious thing is that it works on another computer running PB 6.12(64b).
Could anyone find this bug?

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Bspline

Post by RASHAD »

It works fine with Subsystem directx9 or directx11
Egypt my love
User avatar
SPH
Enthusiast
Enthusiast
Posts: 566
Joined: Tue Jan 04, 2011 6:21 pm

Re: Bspline

Post by SPH »

The code is so simple that I don't see where there would be a bug.

@RASHAD : thx for your test :idea:

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
SPH
Enthusiast
Enthusiast
Posts: 566
Joined: Tue Jan 04, 2011 6:21 pm

Re: Bspline

Post by SPH »

Now : No bugs on post 1

See it :idea:

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
mk-soft
Always Here
Always Here
Posts: 6242
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Bspline

Post by mk-soft »

Attention!

macOS no exit -> Change to Until KeyboardPushed(27)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
SPH
Enthusiast
Enthusiast
Posts: 566
Joined: Tue Jan 04, 2011 6:21 pm

Re: Bspline

Post by SPH »

mk-soft wrote: Tue May 20, 2025 8:53 pm Attention!

macOS no exit -> Change to Until KeyboardPushed(27)
Since I don't have MacOS, I don't know what you mean specifically...
If you'd like, could you post the code adapted for Mac?

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
mk-soft
Always Here
Always Here
Posts: 6242
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Bspline

Post by mk-soft »

And my Monitor not work -> Out of Range!

Update code with exit over ESC

Code: Select all

; Real BSpline code by Crisot / SPH
sph_nb=2000

Dim x.f(sph_nb)
Dim y.f(sph_nb)


InitSprite() 
InitKeyboard() 
InitMouse() 

ExamineDesktops()

ddw = DesktopWidth(0)
ddh = DesktopHeight(0)

OpenScreen(ddw,ddh,32,"Bezier", 0, 60) 

cw = RGB(255,255,255) 
cr = RGB(255,0,0) 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Exemple=Random(12)+1
nombre=4+Random(10)
synchro=Random(1)

For i=0 To sph_nb
  x(i)=Random(ddw-1)
  y(i)=Random(ddh-1)
Next

;Exemple 1: 4 points de base (comme le mec), qui tracent une vrai bspline
;Exemple 2: Pareil que le 1, mais la courbe passe par les points début et fin
;  grace au triplage des points début et fin
;Exemple 3: 4 points en carré, qui tracent un quart de cercle
;Exemple 4: 7 points en carré, qui tracent cercle complet
;Exemple 5: Plein de points au pif
;Exemple 6: Plein de points au pif, courbe qui passe par les points début et fin

;synchro = 1: sert juste aux courbes qui passent par les points début et fin, car
;  pour ces courbes, les points sont répétés 3 fois, et il faut pouvoir déplacer les 3
;  en même temps, sinon ça nique tout :)

Procedure bspline(x0,y0,x1,y1,x2,y2,x3,y3,color) 

  For i = 0 To 100

    t1.f = i/100
    t2.f = t1*t1
    t3.f = t1*t1*t1

  	k1.f = 1 - 3*t1 + 3*t2 - t3
    k2.f = 4 - 6*t2 + 3*t3
  	k3.f = 1 + 3*t1 + 3*t2 - 3*t3

    xfinal.f = (x0 * k1 + x1 * k2 + x2 * k3 + x3 * t3) / 6
		yfinal.f = (y0 * k1 + y1 * k2 + y2 * k3 + y3 * t3) / 6

    Plot (xfinal,yfinal,color)   

  Next I


EndProcedure 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Repeat 
  ClearScreen(0) 
  
  ExamineKeyboard() 
  ExamineMouse() 

  If MouseButton(1) And mousepush=0
    key=key+1 : key=key%nombre
    MouseLocate(x(key),y(key))
  EndIf 
  mousepush=MouseButton(1) 

  x(key)=MouseX() : y(key)=MouseY() ; Déplacement du point

  If synchro=1            ; Juste une gruge pour bouger les points, rien à voir avec la courbe.
    x(1)=x(0) : y(1)=y(0)
    x(2)=x(0) : y(2)=y(0)
    x(nombre-1)=x(nombre-3) : y(nombre-1)=y(nombre-3)
    x(nombre-2)=x(nombre-3) : y(nombre-2)=y(nombre-3)
  EndIf

  StartDrawing(ScreenOutput())
  DrawingMode(4)
  For n=0 To nombre-1   ; On trace les cercles...
    If n=key : Circle(x(n),y(n),5,cr) : Else : Circle(x(n),y(n),5,cw) : EndIf
  Next
  For n=0 To nombre-4   ; On trace la bspline
    bspline(x(n),y(n),x(n+1),y(n+1),x(n+2),y(n+2),x(n+3),y(n+3),cw)
  Next
  StopDrawing() 

  FlipBuffers() 
  
Until KeyboardPushed(27)


My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
pjay
Enthusiast
Enthusiast
Posts: 251
Joined: Thu Mar 30, 2006 11:14 am

Re: Bspline

Post by pjay »

Thanks for posting.

KeyboardPushed(1) & KeyboardPushed(27) will be problematic on different OS, use KeyboardPushed(#PB_Key_Escape) instead.
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Re: Bspline

Post by threedslider »

@pjay : thanks but it doesn't fix the whole program... :|

@SPH : Your code doesn't take the behavior of B-Spline, it acts as a random position of circles but they don't make good position for B-spline, I can't move the circles it crash when clicked from the mouse... :| For this to see your B-Spline I have fixed partially your program in visual :mrgreen:

here the code :

Code: Select all

; Real BSpline code by Crisot / SPH
sph_nb=2000

Dim x.f(sph_nb)
Dim y.f(sph_nb)


InitSprite() 
InitKeyboard() 
InitMouse() 

ExamineDesktops()

ddw = DesktopWidth(0)
ddh = DesktopHeight(0)

;OpenScreen(ddw,ddh,32,"Bezier") 

OpenWindow(1, 0,0,1920/DesktopResolutionX(),1080/DesktopResolutionY(),"Bezier", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(1),0,0,1920,1080,0,0,0)

cw = RGB(255,255,255) 
cr = RGB(255,0,0) 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Exemple=Random(12)+1
nombre=4+Random(10)
synchro=Random(1)

For i=0 To sph_nb
  x(i)=Random(ddw-1)
  y(i)=Random(ddh-1)
Next

;Exemple 1: 4 points de base (comme le mec), qui tracent une vrai bspline
;Exemple 2: Pareil que le 1, mais la courbe passe par les points début et fin
;  grace au triplage des points début et fin
;Exemple 3: 4 points en carré, qui tracent un quart de cercle
;Exemple 4: 7 points en carré, qui tracent cercle complet
;Exemple 5: Plein de points au pif
;Exemple 6: Plein de points au pif, courbe qui passe par les points début et fin

;synchro = 1: sert juste aux courbes qui passent par les points début et fin, car
;  pour ces courbes, les points sont répétés 3 fois, et il faut pouvoir déplacer les 3
;  en même temps, sinon ça nique tout :)

Procedure bspline(x0,y0,x1,y1,x2,y2,x3,y3,color) 

  For i = 0 To 100

    t1.f = i/100
    t2.f = t1*t1
    t3.f = t1*t1*t1

  	k1.f = 1 - 3*t1 + 3*t2 - t3
    k2.f = 4 - 6*t2 + 3*t3
  	k3.f = 1 + 3*t1 + 3*t2 - 3*t3

    xfinal.f = (x0 * k1 + x1 * k2 + x2 * k3 + x3 * t3) / 6
		yfinal.f = (y0 * k1 + y1 * k2 + y2 * k3 + y3 * t3) / 6

    Plot (xfinal,yfinal,color)   

  Next I


EndProcedure 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Repeat 
  
  ClearScreen(0) 
  
  ExamineKeyboard() 
  ExamineMouse() 

  If MouseButton(1) And mousepush=0
    key=key+1 : key=key%nombre
    MouseLocate(x(key),y(key))
  EndIf 
  mousepush=MouseButton(1) 

  x(key)=MouseX() : y(key)=MouseY() ; Déplacement du point

  If synchro=1            ; Juste une gruge pour bouger les points, rien à voir avec la courbe.
    x(1)=x(0) : y(1)=y(0)
    x(2)=x(0) : y(2)=y(0)
    x(nombre-1)=x(nombre-3) : y(nombre-1)=y(nombre-3)
    x(nombre-2)=x(nombre-3) : y(nombre-2)=y(nombre-3)
  EndIf

  StartDrawing(ScreenOutput())
  DrawingMode(4)
  For n=0 To nombre-1   ; On trace les cercles...
    If n=key : Circle(x(n),y(n),5,cr) : Else : Circle(x(n),y(n),5,cw) : EndIf
  Next
  For n=0 To nombre-4   ; On trace la bspline
    bspline(x(n),y(n),x(n+1),y(n+1),x(n+2),y(n+2),x(n+3),y(n+3),cw)
  Next
  StopDrawing() 
  
  FlipBuffers() 
  
Until KeyboardPushed(#PB_Key_Escape)
But watch out ! Sometimes work and sometime doesn't work due of outside from plot.... :?
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Re: Bspline

Post by threedslider »

@SPH : For your help, here the review of math for B-Spline :shock: :wink:

https://en.wikipedia.org/wiki/B-spline

Good luck !
threedslider
Enthusiast
Enthusiast
Posts: 396
Joined: Sat Feb 12, 2022 7:15 pm

Re: Bspline

Post by threedslider »

@SPH : If you need a simple code from that here is you can be inspired by javascript code :mrgreen: : https://github.com/thibauts/b-spline/bl ... r/index.js

Good luck !
Post Reply