A priori on dirait un problème de "copier-coller", les lignes sont "coupées", ce qui génère des commentaires sans ";", et des calculs sur 2 lignes
Code : Tout sélectionner
DeclareModule DrawingAA
; Original Author : Le Soldat Inconnu
; Modified by: Eddy
; supported PB version : 5.30+
;
; Program purpose : Smooth drawing
Declare DrawingModeAA(Thickness=1, Mode=#PB_Ignore)
Declare LineAA(X, Y, Width, Height, Color)
Declare CircleAA(X, Y, Radius, Color)
Declare EllipseAA(X, Y, RadiusX, RadiusY, Color)
Declare TriangleAA(x1, y1, x2, y2, x3, y3, Color)
EndDeclareModule
Module DrawingAA
EnableExplicit
Global AA_Thickness=1
Global AA_Mode=#PB_2DDrawing_Default
Procedure DrawingModeAA(Thickness=1, Mode=#PB_Ignore)
If Mode<>#PB_Ignore
DrawingMode(Mode)
AA_Mode=Mode
EndIf
AA_Thickness=Thickness
EndProcedure
Procedure.i BlendColors(FrontColor, BackColor, Factor.f) ;
;Blend two colors (using AlphaBlend If enabled)
Protected R=Red(FrontColor)
Protected G=Green(FrontColor)
Protected B=Blue(FrontColor)
If AA_Mode & #PB_2DDrawing_AlphaBlend
ProcedureReturn RGBA(R, G, B, Alpha(FrontColor)*Factor)
Else
R=R * Factor + (1-Factor) * Red(BackColor)
G=G * Factor + (1-Factor) * Green(BackColor)
B=B * Factor + (1-Factor) * Blue(BackColor)
ProcedureReturn RGB(R, G, B)
EndIf
EndProcedure
Procedure CircleAA(X, Y, Radius, Color)
Protected n, nn, Distance.f, Factor.f, Background
If AA_Mode & #PB_2DDrawing_Outlined ; Cercle vide
; on dessine 1/4 du
;cercle et on duplique pour le reste
For n=0 To Radius
For nn=0 To Radius
Distance.f=Sqr(n * n + nn * nn)
If Distance<=Radius And Distance>Radius - 1
Factor.f=1-Abs(Radius - 1 - Distance)
Background=Point(X + n, Y + nn) : Plot(X + n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y + nn) : Plot(X - n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X + n, Y - nn) : Plot(X + n,
Y - nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y - nn) : Plot(X - n,
Y - nn, BlendColors(Color, Background, Factor))
ElseIf Distance<=Radius - AA_Thickness And
Distance>Radius - AA_Thickness - 1
Factor.f=1-Abs(Radius - AA_Thickness -Distance)
Background=Point(X + n, Y + nn) : Plot(X + n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y + nn) : Plot(X - n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X + n, Y - nn) : Plot(X + n,
Y - nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y - nn) : Plot(X - n,
Y - nn, BlendColors(Color, Background, Factor))
ElseIf Distance<=Radius - 1 And Distance>Radius -AA_Thickness
Plot(X + n, Y + nn, Color)
Plot(X - n, Y + nn, Color)
Plot(X + n, Y - nn, Color)
Plot(X - n, Y - nn, Color)
EndIf
Next
Next
Else ; Cercle plein
; on dessine 1/4 du cercle et on duplique pour lereste
For n=0 To Radius
For nn=0 To Radius
Distance.f=Sqr(n * n + nn * nn)
If Distance<=Radius And Distance>Radius - 1
Factor.f=(Radius - Distance)
Background=Point(X + n, Y + nn) : Plot(X + n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y + nn) : Plot(X - n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X + n, Y - nn) : Plot(X + n,
Y - nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y - nn) : Plot(X - n,
Y - nn, BlendColors(Color, Background, Factor))
ElseIf Distance<=Radius - 1
Plot(X + n, Y + nn, Color)
Plot(X - n, Y + nn, Color)
Plot(X + n, Y - nn, Color)
Plot(X - n, Y - nn, Color)
EndIf
Next
Next
EndIf
EndProcedure
Procedure EllipseAA(X, Y, RadiusX, RadiusY, Color)
Protected n, nn, Distance.f, Factor.f, Background
Protected Ellipse_CosAngle.f, Ellispse_Rayon.f, Ellipse_E.f
; Précacul de l'équation de l'ellipse
If RadiusX>RadiusY
Ellipse_E=Sqr(RadiusX * RadiusX - RadiusY * RadiusY) /RadiusX
Else
Ellipse_E=Sqr(RadiusY * RadiusY - RadiusX * RadiusX) /RadiusY
EndIf
Ellipse_E * Ellipse_E
If AA_Mode & #PB_2DDrawing_Outlined ; ellipse vide
; on dessine 1/4 de
;l'ellipse et on duplique pour le reste
For n=0 To RadiusX
For nn=0 To RadiusY
Distance.f=Sqr(n * n + nn * nn)
If RadiusX>RadiusY
If n
Ellipse_CosAngle.f=n / Distance
Ellispse_Rayon=Sqr(RadiusY * RadiusY / (1 -Ellipse_E * Ellipse_CosAngle * Ellipse_CosAngle))
Else
Ellispse_Rayon=RadiusY
EndIf
Else
If nn
Ellipse_CosAngle.f=nn / Distance
Ellispse_Rayon=Sqr(RadiusX * RadiusX / (1 -Ellipse_E * Ellipse_CosAngle * Ellipse_CosAngle))
Else
Ellispse_Rayon=RadiusX
EndIf
EndIf
If Distance<=Ellispse_Rayon And
Distance>Ellispse_Rayon - 1
Factor.f=1-Abs(Ellispse_Rayon - 1 - Distance)
Background=Point(X + n, Y + nn) : Plot(X + n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y + nn) : Plot(X - n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X + n, Y - nn) : Plot(X + n,
Y - nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y - nn) : Plot(X - n,
Y - nn, BlendColors(Color, Background, Factor))
ElseIf Distance<=Ellispse_Rayon - AA_Thickness And
Distance>Ellispse_Rayon - AA_Thickness - 1
Factor.f=1-Abs(Ellispse_Rayon - AA_Thickness -Distance)
Background=Point(X + n, Y + nn) : Plot(X + n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y + nn) : Plot(X - n,
Y + nn, BlendColors(Color, Background, Factor))
Background=Point(X + n, Y - nn) : Plot(X + n,
Y - nn, BlendColors(Color, Background, Factor))
Background=Point(X - n, Y - nn) : Plot(X - n,
Y - nn, BlendColors(Color, Background, Factor))
ElseIf Distance<=Ellispse_Rayon - 1 And
Distance>Ellispse_Rayon - AA_Thickness
Plot(X + n, Y + nn, Color)
Plot(X - n, Y + nn, Color)
Plot(X + n, Y - nn, Color)
Plot(X - n, Y - nn, Color)
EndIf
Next
Next
Else ; ellipse pleine
; on dessine 1/4 de l'ellipse et on duplique pour le reste
For n=0 To RadiusX
For nn=0 To RadiusY
Distance.f=Sqr(n * n + nn * nn)
If RadiusX>RadiusY
If n
Ellipse_CosAngle.f=n / Distance
Ellispse_Rayon=Sqr(RadiusY * RadiusY / (1 -Ellipse_E * Ellipse_CosAngle * Ellipse_CosAngle))
Else
Ellispse_Rayon=RadiusY
EndIf
Else
If nn
Ellipse_CosAngle.f=nn / Distance
Ellispse_Rayon=Sqr(RadiusX * RadiusX / (1 -Ellipse_E * Ellipse_CosAngle * Ellipse_CosAngle))
Else
Ellispse_Rayon=RadiusX
EndIf
EndIf
If Distance<=Ellispse_Rayon And
Distance>Ellispse_Rayon - 1
Factor.f=1 - (Ellispse_Rayon - Distance)
Background=Point(X + n, Y + nn) : Plot(X + n,
Y + nn, BlendColors(Background, Color, Factor))
Background=Point(X - n, Y + nn) : Plot(X - n,
Y + nn, BlendColors(Background, Color, Factor))
Background=Point(X + n, Y - nn) : Plot(X + n,
Y - nn, BlendColors(Background, Color, Factor))
Background=Point(X - n, Y - nn) : Plot(X - n,
Y - nn, BlendColors(Background, Color, Factor))
ElseIf Distance<=Ellispse_Rayon - 1
Plot(X + n, Y + nn, Color)
Plot(X - n, Y + nn, Color)
Plot(X + n, Y - nn, Color)
Plot(X - n, Y - nn, Color)
EndIf
Next
Next
EndIf
EndProcedure
Procedure LineAA(X, Y, Width, Height, Color)
Protected n, nn, Distance.f, Background, Factor.f
Protected SensX, SensY, Epaisseur.f, x2.f, y2.f,
CosAngle.f, SinAngle.f
; On mets la droite toujours dans le même sens pour l'analyse
; La sauvegarde du sens permettra de dessiner la droite
;ensuite dans le bon sens
If Width>=0
SensX=1
Else
SensX=-1
Width=- Width
EndIf
If Height>=0
SensY=1
Else
SensY=-1
Height=- Height
EndIf
; Demi épaisseur de la ligne
Epaisseur.f=AA_Thickness / 2
; calcul pour le changement de repère qui permet de
;connaitre l'épaisseur du trait et de gérer l'AA
Distance.f=Sqr(Width * Width + Height * Height)
CosAngle.f=Width / Distance
SinAngle.f=-Sin(ACos(CosAngle))
; Dessin de la ligne
For n=-AA_Thickness To Width + AA_Thickness
For nn=-AA_Thickness To Height + AA_Thickness
; changement de base
; les y représentent l'épaisseur de la ligne
x2=n * CosAngle - nn * SinAngle
y2=Abs(n * SinAngle + nn * CosAngle)
If y2<=Epaisseur + 0.5
Factor=0.5 + Epaisseur - y2
If Factor>1
Factor=1
EndIf
If x2>-1 And x2<Distance + 1
If x2<0
Factor * (1 + x2)
ElseIf x2>Distance
Factor * (1 - x2 + Distance)
EndIf
Else
Factor=0
EndIf
If Factor>0
If Factor<1
Background=Point(X + n * SensX, Y + nn *SensY)
Plot(X + n * SensX, Y + nn * SensY,
BlendColors(Color, Background, Factor))
Else
Plot(X + n * SensX, Y + nn * SensY, Color)
EndIf
EndIf
EndIf
Next
Next
EndProcedure
Procedure TriangleAA(x1, y1, x2, y2, x3, y3, Color)
Protected n, nn, Background, Factor.f, Epaisseur.f, Zone_G,
Zone_D, Zone_H, Zone_B
Protected Width12, Hight12, Distance12.f, CosAngle12.f,
SinAngle12.f, Sens12, y1_r2.f, Application1.f, Interieur1
Protected Width23, Hight23, Distance23.f, CosAngle23.f,
SinAngle23.f, Sens23, y2_r2.f, Application2.f, Interieur2
Protected Width31, Hight31, Distance31.f, CosAngle31.f,
SinAngle31.f, Sens31, y3_r2.f, Application3.f, Interieur3
; Demi épaisseur de la ligne
Epaisseur.f=AA_Thickness / 2
; calcul pour le changement de repère qui permet de
;connaitre l'épaisseur du trait et de gérer l'AA
; Pour la ligne 12
; Le point de départ du repère est le point x1, y1
Width12=x2 - x1
Hight12=y2 - y1
Distance12.f=Sqr(Width12 * Width12 + Hight12 * Hight12)
CosAngle12.f=Width12 / Distance12
SinAngle12.f=Sin(ACos(CosAngle12))
If Hight12>0
SinAngle12=-SinAngle12
EndIf
; changement de base
y3_r2=(x3 - x1) * SinAngle12 + (y3 - y1) * CosAngle12
; on regarde de quel coté de la ligne se trouve le triangle
;en regardant la position du point 3
If y3_r2>0
Sens12=1
Else
Sens12=-1
EndIf
; Pour la ligne 23
; Le point de départ du repère est le point x2, y2
Width23=x3 - x2
Hight23=y3 - y2
Distance23.f=Sqr(Width23 * Width23 + Hight23 * Hight23)
CosAngle23.f=Width23 / Distance23
SinAngle23.f=Sin(ACos(CosAngle23))
If Hight23>0
SinAngle23=-SinAngle23
EndIf
; changement de base
y1_r2=(x1 - x2) * SinAngle23 + (y1 - y2) * CosAngle23
; on regarde de quel coté de la ligne se trouve le triangle
;en regardant la position du point 3
If y1_r2>0
Sens23=1
Else
Sens23=-1
EndIf
; Pour la ligne 31
; Le point de départ du repère est le point x3, y3
Width31=x1 - x3
Hight31=y1 - y3
Distance31.f=Sqr(Width31 * Width31 + Hight31 * Hight31)
CosAngle31.f=Width31 / Distance31
SinAngle31.f=Sin(ACos(CosAngle31))
If Hight31>0
SinAngle31=-SinAngle31
EndIf
; changement de base
y2_r2=(x2 - x3) * SinAngle31 + (y2 - y3) * CosAngle31
; on regarde de quel coté de la ligne se trouve le triangle
;en regardant la position du point 3
If y2_r2>0
Sens31=1
Else
Sens31=-1
EndIf
; Détermination de la zone de dessin
Zone_G=x1
Zone_D=x1
Zone_B=y1
Zone_H=y1
If x2<Zone_G
Zone_G=x2
EndIf
If x3<Zone_G
Zone_G=x3
EndIf
If x2>Zone_D
Zone_D=x2
EndIf
If x3>Zone_D
Zone_D=x3
EndIf
If y2<Zone_B
Zone_B=y2
EndIf
If y3<Zone_B
Zone_B=y3
EndIf
If y2>Zone_H
Zone_H=y2
EndIf
If y3>Zone_H
Zone_H=y3
EndIf
Zone_B - AA_Thickness
Zone_H + AA_Thickness
Zone_G - AA_Thickness
Zone_D + AA_Thickness
; Dessin du triangle
If AA_Mode & #PB_2DDrawing_Outlined ; Triangle vide
For n=Zone_G To Zone_D
For nn=Zone_B To Zone_H
y1_r2=(n - x1) * SinAngle12 + (nn - y1) *CosAngle12
y1_r2 * Sens12
If y1_r2>=-0.5 - Epaisseur
Application1.f=0.5 + Epaisseur + y1_r2
If Application1>1
Application1=1 + 0.5 + Epaisseur -Application1
If Application1<0
Application1=0
EndIf
Interieur1=1
Else
Interieur1=0
EndIf
y2_r2=(n - x2) * SinAngle23 + (nn - y2) *CosAngle23
y2_r2 * Sens23
If y2_r2>=-0.5 - Epaisseur
Application2.f=0.5 + Epaisseur + y2_r2
If Application2>1
Application2=1 + 0.5 + Epaisseur -Application2
If Application2<0
Application2=0
EndIf
Interieur2=1
Else
Interieur2=0
EndIf
y3_r2=(n - x3) * SinAngle31 + (nn - y3) *CosAngle31
y3_r2 * Sens31
If y3_r2>=-0.5 - Epaisseur
Application3.f=0.5 + Epaisseur + y3_r2
If Application3>1
Application3=1 + 0.5 + Epaisseur -Application3
If Application3<0
Application3=0
EndIf
Interieur3=1
Else
Interieur3=0
EndIf
If Interieur1 And Interieur2 And
Interieur3
Factor=Application1 + Application2 +
Application3
Else
Factor=1
If Interieur1=0
Factor * Application1
EndIf
If Interieur2=0
Factor * Application2
EndIf
If Interieur3=0
Factor * Application3
EndIf
EndIf
If Factor>0
If Factor<1
Background=Point(n, nn)
Plot(n, nn, BlendColors(Color,
Background, Factor))
Else
Plot(n, nn, Color)
EndIf
EndIf
EndIf
EndIf
EndIf
Next
Next
Else ; Triangle plein
For n=Zone_G To Zone_D
For nn=Zone_B To Zone_H
y1_r2=(n - x1) * SinAngle12 + (nn - y1) *CosAngle12
y1_r2 * Sens12
If y1_r2>=-0.5 - Epaisseur
Application1.f=0.5 + Epaisseur + y1_r2
If Application1>1
Application1=1
EndIf
y2_r2=(n - x2) * SinAngle23 + (nn - y2) *CosAngle23
y2_r2 * Sens23
If y2_r2>=-0.5 - Epaisseur
Application2.f=0.5 + Epaisseur + y2_r2
If Application2>1
Application2=1
EndIf
y3_r2=(n - x3) * SinAngle31 + (nn - y3) *CosAngle31
y3_r2 * Sens31
If y3_r2>=-0.5 - Epaisseur
Application3.f=0.5 + Epaisseur + y3_r2
If Application3>1
Application3=1
EndIf
Factor=Application1 * Application2 *Application3
If Factor<1
Background=Point(n, nn)
Plot(n, nn, BlendColors(Color,
Background, Factor))
Else
Plot(n, nn, Color)
EndIf
EndIf
EndIf
EndIf
Next
Next
EndIf
EndProcedure
EndModule
CompilerIf #PB_Compiler_IsMainFile
UseModule DrawingAA
; ******************************************************
; EXAMPLE
; Test functions to draw with anti-aliasing ( AA )
; ******************************************************
If OpenWindow(0, 0, 0, 500, 500, "Test", #PB_Window_SystemMenu| #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
If CanvasGadget(0, 0, 0, 500, 500)
StartDrawing(CanvasOutput(0))
blackColor=RGBA(0, 0, 0, 255)
; Our fancy background
#Carreau=25
For n=0 To 500 Step #Carreau
For nn=0 To 500 Step #Carreau
If ((n + nn) / #Carreau) & %1=%1
Box(n, nn, #Carreau, #Carreau, $FFFFFF)
Else
Box(n, nn, #Carreau, #Carreau, $0000FF)
EndIf
Next
Next
; Drawing Circles
DrawingModeAA(1, #PB_2DDrawing_AlphaBlend)
CircleAA(100, 100, 70, blackColor)
DrawingModeAA(1, #PB_2DDrawing_AlphaBlend |
#PB_2DDrawing_Outlined)
CircleAA(250, 100, 50, blackColor)
DrawingModeAA(3)
CircleAA(250, 100, 70, blackColor)
; Drawing Ellipses
DrawingModeAA(2, #PB_2DDrawing_AlphaBlend)
EllipseAA(400, 100, 30, 20, blackColor)
DrawingModeAA(2, #PB_2DDrawing_AlphaBlend |
#PB_2DDrawing_Outlined)
EllipseAA(400, 100, 40, 60, blackColor)
DrawingModeAA(1)
EllipseAA(400, 100, 50, 70, blackColor)
; Drawing Lines
DrawingModeAA(1);, #PB_2DDrawing_AlphaBlend)
For n=0 To 150 Step 30
LineAA(20, 200, 150, n, blackColor)
Next
For n=0 To 120 Step 30
LineAA(20, 200, n, 150, blackColor)
Next
Thickness=2
For n=0 To 150 Step 30
DrawingModeAA(Thickness) : Thickness + 1
LineAA(200, 200, 150, n, blackColor)
Next
Thickness=2
For n=0 To 120 Step 30
DrawingModeAA(Thickness) : Thickness + 1
LineAA(200, 200, n, 150, blackColor)
Next
; Drawing Triangles
DrawingModeAA(1, #PB_2DDrawing_AlphaBlend)
TriangleAA(420, 200, 480, 250, 370, 350, blackColor)
Thickness=1
DrawingModeAA(1, #PB_2DDrawing_AlphaBlend |
#PB_2DDrawing_Outlined)
For n=0 To 30 Step 10
DrawingModeAA(Thickness) : Thickness + 1
TriangleAA(490 - n, 260 + n, 390 + n, 350, 460, 400- n, blackColor)
Next
StopDrawing()
EndIf
Repeat: Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
End
CompilerEndIf