voilà ou j'en suis :
Code : Tout sélectionner
; Auteur : Le Soldat Inconnu
; Version de PB : 3.81
;
; Explication du programme :
; Dessiner un engrenage
Procedure.l Signe(nb.f)
If Abs(nb) < 0.01
Resultat.l = 0
Else
Resultat.l = nb / Abs(nb)
EndIf
ProcedureReturn Resultat
EndProcedure
Procedure Engrenage(x.l, y.l, Rayon.l, RayonAlesage.l, NbDents.l, HauteurDent.l, Decalage.f, Couleur.l)
; x, y : position duentre de l'engrenage
; Rayon : rayon de l'engrenage
; RayonAlesage : rayon du trou au centre de l'engrenage
; NbDents : nombre de dents
; HauteurDent : Hauteur des dents
; Decalage : norbre de dents de décalage par rapport à l'origine, utilise pour faire tourner l'engrenage
; Couleur : couleur de l'engrenage
; on crée une carte à la taille de l'image
Dim CarteImage.l(ImageWidth(), ImageHeight())
Dim CarteLissage.f(ImageWidth(), ImageHeight())
StartDrawing(ImageOutput())
; on copie l'image de base pour le lissage
For n = 0 To ImageWidth() - 1
For nn = 0 To ImageHeight() - 1
CarteImage(n, nn) = Point(n, nn)
Next
Next
DrawingMode(4)
Circle(x, y, Rayon - HauteurDent, Couleur) ; on dessine l'engrenage
Circle(x, y, RayonAlesage, Couleur) ; on dessine le trou de l'engrenage
DrawingMode(0) ; commenter ceci pour ne pas remplir l'engrenage
FillArea(x, y + RayonAlesage + 3, Couleur, Couleur) ; on rempli l'engrenage
LargeurDent = Int(Rayon * 3 / 5 * Sin(#PI / NbDents) + 0.5) ; on détermine la largeur d'une dents
For n = 1 To NbDents ; on passe en revue toutes les dents
Cos.f = Cos((n + Decalage) * 2 * #PI / NbDents) ; Calcul du cos de l'angle
Sin.f = Sin((n + Decalage) * 2 * #PI / NbDents) ; Calcul du sin de l'angle
; Point haut gauche de la dent
PosX1.f = x + Rayon * Cos + LargeurDent / 2 * Sin
PosY1.f = y + Rayon * Sin - LargeurDent / 2 * Cos
; Point haut droit de la dent
PosX2.f = x + Rayon * Cos - LargeurDent / 2 * Sin
PosY2.f = y + Rayon * Sin + LargeurDent / 2 * Cos
; Point bas gauche de la dent
PosX3.f = x + (Rayon - HauteurDent) * Cos + LargeurDent * Sin
PosY3.f = y + (Rayon - HauteurDent) * Sin - LargeurDent * Cos
; Point bas droit de la dent
PosX4.f = x + (Rayon - HauteurDent) * Cos - LargeurDent * Sin
PosY4.f = y + (Rayon - HauteurDent) * Sin + LargeurDent * Cos
; Point vers le centre
PosX5.f = x + (RayonAlesage + 5) * Cos
PosY5.f = y + (RayonAlesage + 5) * Sin
; Dessin du contour de la dent
LineXY(PosX1, PosY1, PosX2, PosY2, Couleur)
LineXY(PosX1, PosY1, PosX3, PosY3, Couleur)
LineXY(PosX2, PosY2, PosX4, PosY4, Couleur)
LineXY(PosX3, PosY3, PosX5, PosY5, Couleur)
LineXY(PosX4, PosY4, PosX5, PosY5, Couleur)
; Remplissage de la dent
FillArea(Int(x + (Rayon - HauteurDent / 2) * Cos + 0.5), Int(y + (Rayon - HauteurDent / 2) * Sin + 0.5), Couleur, Couleur)
; On calcul le lissage de la dent
PasX.f = (PosX2 - PosX1) / (5 * (LargeurDent + HauteurDent))
PasY.f = (PosY2 - PosY1) / (5 * (LargeurDent + HauteurDent))
For nn = 0 To (5 * (LargeurDent + HauteurDent))
PosX = Int(PosX1 + PasX * nn + 0.5)
PosY = Int(PosY1 + PasY * nn + 0.5)
For i = -1 To 1
For ii = -1 To 1
CoefX.f = (PosX1 + PasX * nn - PosX - i)
CoefY.f = (PosY1 + PasY * nn - PosY - ii)
Coef.f = 1 - Sqr(Pow(CoefX, 2) + Pow(CoefY, 2))
If Coef > CarteLissage(PosX + i, PosY + ii) And Signe(CoefX) = Signe(-Cos) And Signe(CoefY) = Signe(-Sin)
CarteLissage(PosX + i, PosY + ii) = Coef
EndIf
Next
Next
Next
PasX.f = (PosX3 - PosX1) / (5 * (LargeurDent + HauteurDent))
PasY.f = (PosY3 - PosY1) / (5 * (LargeurDent + HauteurDent))
For nn = 0 To (5 * (LargeurDent + HauteurDent))
PosX = Int(PosX1 + PasX * nn + 0.5)
PosY = Int(PosY1 + PasY * nn + 0.5)
For i = -1 To 1
For ii = -1 To 1
CoefX.f = (PosX1 + PasX * nn - PosX - i)
CoefY.f = (PosY1 + PasY * nn - PosY - ii)
Coef.f = 1 - Sqr(Pow(CoefX, 2) + Pow(CoefY, 2))
If Coef > CarteLissage(PosX + i, PosY + ii) And Signe(CoefX) = Signe(-Sin) And Signe(CoefY) = Signe(Cos)
CarteLissage(PosX + i, PosY + ii) = Coef
EndIf
Next
Next
Next
PasX.f = (PosX4 - PosX2) / (5 * (LargeurDent + HauteurDent))
PasY.f = (PosY4 - PosY2) / (5 * (LargeurDent + HauteurDent))
For nn = 0 To (5 * (LargeurDent + HauteurDent))
PosX = Int(PosX2 + PasX * nn + 0.5)
PosY = Int(PosY2 + PasY * nn + 0.5)
For i = -1 To 1
For ii = -1 To 1
CoefX.f = (PosX2 + PasX * nn - PosX - i)
CoefY.f = (PosY2 + PasY * nn - PosY - ii)
Coef.f = 1 - Sqr(Pow(CoefX, 2) + Pow(CoefY, 2))
If Coef > CarteLissage(PosX + i, PosY + ii) And Signe(CoefX) = Signe(Sin) And Signe(CoefY) = Signe(-Cos)
CarteLissage(PosX + i, PosY + ii) = Coef
If n = NbDents
Debug Str(PosX) + " + " + Str(i)
Debug Str(PosY) + " + " + Str(ii)
Debug StrF(CoefX, 3) + " > " + Str(Signe(CoefX))
Debug StrF(Sin, 3) + " > " + Str(Signe(Sin))
Debug StrF(CoefY, 3) + " > " + Str(Signe(CoefY))
Debug StrF(-Cos, 3) + " > " + Str(Signe(-Cos))
Debug ""
EndIf
EndIf
Next
Next
Next
Next
; On affiche le lissage
For i = 0 To ImageWidth() - 1
For ii = 0 To ImageHeight() - 1
If CarteLissage(i, ii) > 0
Rouge = Int(Red(Couleur) * CarteLissage(i, ii) + Red(CarteImage(i, ii)) * (1 - CarteLissage(i, ii)) + 0.5)
Vert = Int(Green(Couleur) * CarteLissage(i, ii) + Green(CarteImage(i, ii)) * (1 - CarteLissage(i, ii)) + 0.5)
Bleu = Int(Blue(Couleur) * CarteLissage(i, ii) + Blue(CarteImage(i, ii)) * (1 - CarteLissage(i, ii)) + 0.5)
Plot(i, ii, RGB(Rouge, Vert, Bleu))
EndIf
Next
Next
StopDrawing()
EndProcedure
;- Exemple
; Création de la fenêtre et dela GadgetList
If OpenWindow(0, 0, 0, 300, 300, #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget, "Test") = 0 Or CreateGadgetList(WindowID()) = 0
End
EndIf
; Création de l'image
CreateImage(0, 300, 300)
StartDrawing(ImageOutput())
For n = 0 To 149
Box(0, 2 * n, 300, 2, RGB(0, 0, 255 - n))
Next
StopDrawing()
; On dessine l'engrenage
Engrenage(150, 150, 120, 40, 16, 30, 0.1, RGB(255, 255, 255))
; On affiche l'image
ImageGadget(0, 0, 0, 300, 300, UseImage(0))
; Sauvegarde de l'image
; UsePNGImageEncoder()
; SaveImage(0, "Engrenage.png", #PB_ImagePlugin_PNG)
Repeat
Event = WaitWindowEvent()
Until Event = #PB_EventCloseWindow
End