Lines Anti-Aliased
Posted: Thu Sep 16, 2010 7:09 pm
; Thanks to 'Le Soldat Inconnu' For introducing Wu Anti-Alias Algorithm to PB
; Thanks to 'Kenmo' for his arrow procedure
; Thanks to 'einander' ,'Demevic' and 'MV' for their arrow lines code
; Thanks to 'galah' for Bresenham line drawing Algorithm
; That was the real HELP for this piece of code
; Todo list :
; 1- Fix some bugs
; 2- Optimizing the code
; 3- Add More Lines Style
; 4- Add at Least 3 Arrow Head styles
Tested on Windows 7 x64
; Thanks to 'Kenmo' for his arrow procedure
; Thanks to 'einander' ,'Demevic' and 'MV' for their arrow lines code
; Thanks to 'galah' for Bresenham line drawing Algorithm
; That was the real HELP for this piece of code
; Todo list :
; 1- Fix some bugs
; 2- Optimizing the code
; 3- Add More Lines Style
; 4- Add at Least 3 Arrow Head styles
Code: Select all
Global x1,y1,x2,y2
ProcedureDLL.l ColorBlending(Color1.l, Color2.l, Blend.f)
Protected Red, Green, Blue, Red2, Green2, Blue2
Red = Color1 & $FF
Green = Color1 >> 8 & $FF
Blue = Color1 >> 16
Red2 = Color2 & $FF
Green2 = Color2 >> 8 & $FF
Blue2 = Color2 >> 16
Red = Red * Blend + Red2 * (1-Blend)
Green = Green * Blend + Green2 * (1-Blend)
Blue = Blue * Blend + Blue2 * (1-Blend)
ProcedureReturn (Red | Green <<8 | Blue << 16)
EndProcedure
Procedure NormalL(X,Y, x3, y3, Color, Thickness = 1)
Width = x3-X
Hight = y3-Y
Protected SignX, SignY, n, nn, Thick.f, x2.f, y2.f, Color_Found.l, Application.f, Hypo.f
If Width >= 0
SignX = 1
Else
SignX = -1
Width = - Width
EndIf
If Hight >= 0
SignY = 1
Else
SignY = -1
Hight = - Hight
EndIf
Thick.f = Thickness / 2
Hypo.f = Sqr(Width * Width + Hight * Hight)
CosPhi.f = Width / Hypo
SinPhi.f = -Sin(ACos(CosPhi))
For n = -Thickness To Width + Thickness
For nn = -Thickness To Hight + Thickness
x2 = n * CosPhi - nn * SinPhi
y2 = Abs(n * SinPhi + nn * CosPhi)
If y2 <= Thick + 0.5
Application = 0.5 + Thick - y2
If Application > 1
Application = 1
EndIf
If x2 > -1 And x2 < Hypo + 1
If x2 < 0
Application * (1 + x2)
ElseIf x2 > Hypo
Application * (1 - x2 + Hypo)
EndIf
Else
Application = 0
EndIf
If Application > 0
If Application < 1
Color_Found = Point(X + n * SignX, Y + nn * SignY)
Plot(X + n * SignX, Y + nn * SignY, ColorBlending(Color, Color_Found, Application))
Else
Plot(X + n * SignX, Y + nn * SignY, Color)
EndIf
EndIf
EndIf
Next
Next
EndProcedure
Procedure ArrowL(x1.l, y1.l, x2.l, y2.l, Color.l,Thick.f, Llength.f, Angle.f)
If Llength = 0
Llength = 0.05*(Sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)))
EndIf
Llength = Abs(Llength) : Angle = Abs(Angle*#PI/180.0)
NormalL(x1, y1,x2, y2,Color,Thick)
If (Llength > 0.0) And (Abs(x2 - x1) + Abs(y2 - y1) > Llength)
x33.f = -1*Cos(Angle) * Llength
y33.f = Sin(Angle) * Llength
x44.f = -1*Cos(Angle) * Llength
y44.f = -1*Sin(Angle) * Llength
SAngle.f = ATan((y1 - y2)/(x2 - x1))
If (x2 < x1)
SAngle = SAngle + #PI
EndIf
x3 = Cos(SAngle)*x33 + Sin(SAngle)*y33
y3 = Cos(SAngle)*y33 - Sin(SAngle)*x33
x4 = Cos(SAngle)*x44 + Sin(SAngle)*y44
y4 = Cos(SAngle)*y44 - Sin(SAngle)*x44
NormalL(x2, y2,x2+x3,y2+y3, Color,Thick)
NormalL(x2, y2,x2+x4,y2+y4, Color,Thick)
If Thick > 4
Circle(x2,y2,Thick/2,Color)
Circle(x1,y1,Thick/2,Color)
EndIf
EndIf
EndProcedure
Procedure DashL(x1.i, y1.i, x2.i, y2.i, Color.i,Thick.f, Lstep.f)
scale.f = (y2-y1)/(x2-x1)
For i = 0 To 10
NormalL(x1,y1,x1+Lstep,y1+Lstep*scale,Color,Thick)
x1 = x1+Lstep + Lstep
y1 = y1+2*Lstep*scale
Next
EndProcedure
Procedure DashDotL(x1.i, y1.i, x2.i, y2.i, Color.i,Thick.f,Lstep.f)
scale.f = (y2-y1)/(x2-x1)
For i = 0 To 10
NormalL(x1,y1,x1+Lstep,y1+Lstep*scale,Color, Thick)
If Thick = 1
Circle(x1+Lstep + Lstep/2,y1+1.5*Lstep*scale,Thick,Color)
Else
Circle(x1+Lstep + Lstep/2,y1+1.5*Lstep*scale,Thick/2,Color)
EndIf
x1 = x1+Lstep + Lstep
y1 = y1+2*Lstep*scale
Next
EndProcedure
OpenWindow(0, 0, 0, 500, 500, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
CreateImage(0, 500, 500)
StartDrawing(ImageOutput(0))
ArrowL(10,400,400,10,$01FFFE,3,40,30) ;ArrowL(x1,y1,x2,y2,Color,Line Thickness,Arrow Leg Length,Inclined Angle)
ArrowL(100,200,280,350,$06BCF9,2,30,35)
ArrowL(60,20,420,350,$6FF690,16,50,30)
ArrowL(480,480,10,10,$0104FE,6,50,30)
ArrowL(10,480,30,450,$FFFFFF,1,5,30)
NormalL(5,5,400,100,$01FFFE,4) ;NormalL(x1,y1,x2,y2,Color,Line Thickness)
DashL(10,10,400,150,$01FFFE,4,20) ;DashL(x1,y1,x2,y2,Color,Line Thickness,Interval)
DashDotL(40,60,400,280,$FC0503,8,20) ;DashDotL(x1,y1,x2,y2,Color,Line Thickness,Interval)
DashDotL(40,60,400,380,$FFFFFF,1,20)
StopDrawing()
ImageGadget(0, 0, 0, 500, 500, ImageID(0))
Repeat
Event = WaitWindowEvent()
Select Event
EndSelect
Until Event = #PB_Event_CloseWindow
End