VectorDrawing : Trouver un point dans une courbe (position de la souris))

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

VectorDrawing : Trouver un point dans une courbe (position de la souris))

Message par blendman »

salut

J'avais besoin de pouvoir ajouter des points dans des courbes (pour mon soft 2D vectoriel), donc de pouvoir connaitre la position d'un point dans une courbe par exemple pour savoir si je clique dessus ou autre.

donc, voilà un petit code (basé sur le sympathique exemple pathpointangle()) :

Code : Tout sélectionner

Structure sPoint
  x.w
  y.w
EndStructure
Global Dim CopyPathLength.SPoint(0)

Procedure EventCanvas(update=0)
  Static k
  
  If StartVectorDrawing(CanvasVectorOutput(0))
    
    If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
      mx = GetGadgetAttribute(0, #PB_Canvas_MouseX)
      my = GetGadgetAttribute(0, #PB_Canvas_MouseY)
    EndIf
    
    a = 5
    MovePathCursor(150, 125)
    AddPathCurve(0, 270, 0, -150, 350, 180)
    
    ; Debug Str(PathCursorX())+"/"+Str(PathCursorY())

    For i=0 To ArraySize(CopyPathLength())
      x1 = CopyPathLength(i)\x
      y1 = CopyPathLength(i)\y
      If x1>=mx-a And x1<=mx+a  And y1>=my-a And y1<=my+a
        k = i
        update=1
        Break
      EndIf
    Next
    
    StopVectorDrawing()
  EndIf
  
  
  If update = 1
    If StartVectorDrawing(CanvasVectorOutput(0))
      
      AddPathBox(0,0,GadgetWidth(0),GadgetHeight(0))
      VectorSourceColor(RGBA(255,255,255,255))
      FillPath()
      
      MovePathCursor(150, 125)
      AddPathCurve(0, 270, 0, -150, 350, 180)
      
      x = PathPointX(k)
      y = PathPointY(k)
      a = PathPointAngle(k)
      
      VectorSourceColor($FF0000FF)
      StrokePath(5)
      
      AddPathCircle(x, y, 10)
      VectorSourceColor($FFFF0000)
      FillPath()
      
      MovePathCursor(x, y)
      AddPathLine(30*Cos(Radian(a)), 30*Sin(Radian(a)), #PB_Path_Relative)
      StrokePath(5)
      StopVectorDrawing()
    EndIf
  EndIf
  
EndProcedure

If OpenWindow(0, 0, 0, 400, 200, "PathPointX", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 0, 0, 400, 200)
  EventCanvas(1)
  
  ; precalculate the path and save it in an array
  If StartVectorDrawing(CanvasVectorOutput(0))
    MovePathCursor(150, 125)
    AddPathCurve(0, 270, 0, -150, 350, 180)
    n = PathLength()
    ReDim  CopyPathLength.SPoint(n)
    For i=0 To n
      CopyPathLength(i)\x = PathPointX(i) 
      CopyPathLength(i)\y = PathPointY(i)
    Next
    StopVectorDrawing()
  EndIf
  
  Repeat
    Event = WaitWindowEvent(1)
    If event=#PB_Event_Gadget
      If EventGadget() =0
        EventCanvas()
      EndIf
      
    EndIf
    
  Until Event = #PB_Event_CloseWindow
EndIf
A+