[ok] Vector drawing : formes arrondies

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

[ok] Vector drawing : formes arrondies

Message par blendman »

salut

savez-vous comment on peut créer des formes arrondies avec la lib vector drawing ?
Une image pour expliquer ce que j'essaie d'obtenir :
Image

J'ai toujours 2 points qui sont "pointus" (le points de départ de chaque courbe), je voudrais que tout soit lissé ^^.

Code : Tout sélectionner

Global Dim pt.point(10)
t$="217,7,244,91,147,162,51,75,51,10,140,66,"

u = 1
For i = 0 To 10
    pt(i)\x = Val(StringField(t$,u,",")) : u+1
    pt(i)\y = Val(StringField(t$,u,",")) : u+1
Next

Global color.i
Color = -12577347 ; RGBA(0, 0, 255, 255)

Procedure Drawcanvas()
   
    If StartVectorDrawing(CanvasVectorOutput(0))
       
        AddPathBox(0,0, GadgetWidth(0),GadgetHeight(0))
        VectorSourceColor(RGBA(255,255,255,255))
        FillPath()
       
        a = 3
       
        MovePathCursor(pt(a+2)\x,pt(a+2)\y)
        AddPathCurve(pt(0)\x, pt(0)\y,pt(1)\x,pt(1)\y,pt(2)\x,pt(2)\y)
        AddPathCurve(pt(a)\x, pt(a)\y,pt(a+1)\x,pt(a+1)\y,pt(a+2)\x,pt(a+2)\y)
       
        ; remplir le chemin
        VectorSourceColor(color)
        FillPath()
       
       
        ; les points d'encrage
        For i =0 To 5
            AddPathBox(pt(i)\x-5, pt(i)\y-5, 10, 10)
        Next
        VectorSourceColor(RGBA(250, 0, 0, 120))
        FillPath()
       
       
        StopVectorDrawing()
    EndIf
EndProcedure



If OpenWindow(0, 0, 0, 400, 300, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 400, 300)
   
   
    If CreateMenu(0,WindowID(0))
        MenuTitle("Edit")
        MenuItem(2,"Change color")
    EndIf
   
   
    Drawcanvas()
   
    Repeat
       
        Event = WaitWindowEvent()
       
       
        If event = #PB_Event_Menu
            Select EventMenu()
                   
                Case 2 ;color
                    col = ColorRequester(color)
                    Color=RGBA(Red(col),Green(col),Blue(col),255)
                    Drawcanvas()
            EndSelect
           
        ElseIf event = #PB_Event_Gadget
            Select EventGadget()
                   
                Case 0
                    If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
                        If StartVectorDrawing(CanvasVectorOutput(0))
                            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
                            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
                            If EventType() = #PB_EventType_LeftButtonDown
                                move = 0
                                For i=0 To 5
                                    If x >= pt(i)\x-5 And x<=pt(i)\x-5+10 And y>=pt(i)\y-5 And y<=pt(i)\y-5+10
                                        move = 1
                                        Break
                                    EndIf
                                Next
                            ElseIf EventType() = #PB_EventType_LeftButtonUp
                                move=0
                            EndIf
                           
                            If move = 1
                                pt(i)\x = x
                                pt(i)\y = y                               
                            EndIf
                           
                            StopVectorDrawing()
                        EndIf
                        If move = 1
                            Drawcanvas()
                        EndIf
                       
                    EndIf
                                       
            EndSelect
        EndIf
 
    Until Event = #PB_Event_CloseWindow
EndIf


Autre question :
savez-vous s'il est possible d'agir sur les "points des courbes de béziers (créés avec AddPathCurve()) ?
J'ai l'impression que c'est automatique pour la courbe (je voudrais par exemple faire comme un soft de dessin vectoriel, pouvoir tirer sur des sortes d'ancres à droite et à gauche du point pour grossir ou réduire la courbe.)

Merci.
Dernière modification par blendman le sam. 26/mars/2016 9:35, modifié 1 fois.
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

Re: Vector drawing : formes arrondies

Message par blendman »

Bon, j'ai trouvé.

En fait, dans la documentation, on trouve cette figure :
Image


Mais elle est incomplète, con devrait aussi trouver celle-ci afin de mieux comprendre ce que sont les curves de la lib vector drawing :
Image

J'ai compris ça cette nuit en dormant :D.

Et en modifiant mon code ça fonctionne du coup nickel :).

Code : Tout sélectionner

Global Dim pt.point(5)
t$="217,7,244,91,147,162,51,75,51,10,140,66,"

u = 1
For i = 0 To ArraySize(pt())
    pt(i)\x = Val(StringField(t$,u,",")) : u+1
    pt(i)\y = Val(StringField(t$,u,",")) : u+1
Next

Global color.i
Color = -12577347 ; RGBA(0, 0, 255, 255)
#G_canvasVector = 0

Procedure Drawcanvas()
    
    If StartVectorDrawing(CanvasVectorOutput(0))
        
        AddPathBox(0,0, GadgetWidth(0),GadgetHeight(0))
        VectorSourceColor(RGBA(255,255,255,255))
        FillPath()
        
        a = 4        
        MovePathCursor(pt(0)\x,pt(0)\y)
        AddPathCurve(pt(1)\x, pt(1)\y,pt(2)\x,pt(2)\y,pt(3)\x,pt(3)\y)
        AddPathCurve(pt(a)\x, pt(a)\y,pt(a+1)\x,pt(a+1)\y,pt(0)\x,pt(0)\y)
        
        ; remplir le chemin
        VectorSourceColor(color)
        FillPath()
        
        
;         ; les points d'encrage
;         For i =0 To 5
;             AddPathBox(pt(i)\x-5, pt(i)\y-5, 10, 10)
;         Next
;         VectorSourceColor(RGBA(250, 0, 0, 120))
;         FillPath()
                
        StopVectorDrawing()
    EndIf
    
    ; puis, on dessine les trucs utiles
    If StartDrawing(CanvasOutput(#G_canvasVector))
        
        ; Les points d'encrage        
            For i =0 To ArraySize(pt())
                If Mod(i,3)=0                   
                     Box(pt(i)\x-5, pt(i)\y-5, 10, 10,0)
                Else
                    If Mod(i,3)=1
                        u = i-1
                        If u<0
                            u = ArraySize(pt())
                        EndIf                     
                        LineXY(pt(u)\x,pt(u)\y,pt(i)\x,pt(i)\y,0)
                    Else
                        u = i+1
                        If u>ArraySize(pt())
                            u = 0
                        EndIf  
                        LineXY(pt(i)\x,pt(i)\y,pt(u)\x,pt(u)\y,0)
                    EndIf
                    
                    Circle(pt(i)\x,pt(i)\y,3,#Red)
                EndIf
                           
            Next
        
        StopDrawing()
    EndIf
        
EndProcedure


If OpenWindow(0, 0, 0, 400, 300, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 400, 300)
        
    If CreateMenu(0,WindowID(0))
        MenuTitle("Edit")
        MenuItem(2,"Change color")
    EndIf
        
    Drawcanvas()
    
    Repeat
        
        Event = WaitWindowEvent()
                
        If event = #PB_Event_Menu
            Select EventMenu()
                    
                Case 2 ;color 
                    col = ColorRequester(color)
                    Color=RGBA(Red(col),Green(col),Blue(col),255)
                    Drawcanvas()
            EndSelect
            
        ElseIf event = #PB_Event_Gadget
            Select EventGadget()
                    
                Case 0
                    If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
                        If StartVectorDrawing(CanvasVectorOutput(0))
                            x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
                            y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
                            If EventType() = #PB_EventType_LeftButtonDown
                                move = 0
                                For i=0 To 5
                                    If x >= pt(i)\x-5 And x<=pt(i)\x-5+10 And y>=pt(i)\y-5 And y<=pt(i)\y-5+10
                                        move = 1
                                        
                                        sx0 = pt(i)\x
                                        sy0 = pt(i)\y
                                        
                                        u = i+1
                                        If u > ArraySize(pt())
                                            u=0
                                        EndIf
                                        v =i-1
                                        If v <0
                                            v =ArraySize(pt())
                                        EndIf
                                        
                                        sx1 = pt(u)\x
                                        sy1 = pt(u)\y
                                        sx2 = pt(v)\x
                                        sy2 = pt(v)\y
                                        
                                        Break
                                    EndIf
                                Next
                            ElseIf EventType() = #PB_EventType_LeftButtonUp
                                move=0
                            EndIf
                            
                            If move = 1
                                pt(i)\x = x
                                pt(i)\y = y 
                                If Mod(i,3)=0 ; on est sur un point principal
                                    u = i+1
                                    If u > ArraySize(pt())
                                        u=0
                                    EndIf
                                    v =i-1
                                    If v <0
                                        v =ArraySize(pt())
                                    EndIf
                                    pt(u)\x = sx1+(x-sx0)
                                    pt(u)\y = sy1+(y-sy0)
                                    pt(v)\x = sx2+(x-sx0)
                                    pt(v)\y = sy2+(y-sy0)
                                EndIf
                                
                            EndIf
                            
                            StopVectorDrawing() 
                        EndIf
                        If move = 1
                            Drawcanvas()
                        EndIf
                        
                    EndIf
                                        
            EndSelect
        EndIf
        
    Until Event = #PB_Event_CloseWindow
EndIf

Comme quoi, elle cartonne cette librairie ! :D
Avatar de l’utilisateur
microdevweb
Messages : 1802
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: [ok] Vector drawing : formes arrondies

Message par microdevweb »

Merci Blendman,

Ton exemple pourra être très utile
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Bernie
Messages : 282
Inscription : mar. 22/mars/2016 10:12
Localisation : En France

Re: [ok] Vector drawing : formes arrondies

Message par Bernie »

Merci du partage blendman
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

Re: [ok] Vector drawing : formes arrondies

Message par blendman »

De rien, je vais poster ça dans trucs et astuces je crois bien :).

D'ailleurs n'hésitez pas à l'améliorer si vous le souhaitez ;)
Répondre