Page 1 of 1

Bspline

Posted: Sat May 17, 2025 6:56 pm
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)



Re: Bspline

Posted: Sat May 17, 2025 7:36 pm
by threedslider
Your program doesn't work and crashed directly :( :|

Re: Bspline

Posted: Sat May 17, 2025 7:49 pm
by SPH
Good on PB 6.21b9

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

Re: Bspline

Posted: Sat May 17, 2025 8:20 pm
by Caronte3D
Works on beta 6 as well

Re: Bspline

Posted: Sat May 17, 2025 8:58 pm
by SPH
The curious thing is that it works on another computer running PB 6.12(64b).
Could anyone find this bug?

Re: Bspline

Posted: Sat May 17, 2025 10:14 pm
by RASHAD
It works fine with Subsystem directx9 or directx11

Re: Bspline

Posted: Sun May 18, 2025 7:48 am
by SPH
The code is so simple that I don't see where there would be a bug.

@RASHAD : thx for your test :idea:

Re: Bspline

Posted: Tue May 20, 2025 7:07 pm
by SPH
Now : No bugs on post 1

See it :idea:

Re: Bspline

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

macOS no exit -> Change to Until KeyboardPushed(27)

Re: Bspline

Posted: Tue May 20, 2025 9:32 pm
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?

Re: Bspline

Posted: Tue May 20, 2025 10:09 pm
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)



Re: Bspline

Posted: Wed May 21, 2025 7:33 am
by pjay
Thanks for posting.

KeyboardPushed(1) & KeyboardPushed(27) will be problematic on different OS, use KeyboardPushed(#PB_Key_Escape) instead.

Re: Bspline

Posted: Wed May 21, 2025 8:51 am
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.... :?

Re: Bspline

Posted: Thu May 22, 2025 9:15 pm
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 !

Re: Bspline

Posted: Thu May 22, 2025 9:31 pm
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 !