Code : Tout sélectionner
; DashDraw PB 4.4x
EnableExplicit
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
MessageRequester("Error", "Can't open DirectX 7 or later", 0)
End
EndIf
Structure iBox
P1.POINT
P2.POINT
P3.POINT
P4.POINT
EndStructure
Procedure DrawBox(x1.i, y1.i, x2.i, y2.i, hoehe.f, icolorout.i=#White, icolorin=#White)
Protected Distance.i, Winkel.i, myBox.iBox, DiffX.i
Protected fWinkelM.f, fWinkelP.f, PickPoint.POINT
PickPoint\x = (x1+x2)/2: PickPoint\y = (y1+y2)/2 ; Punkt in der Mitte für Fillarea
hoehe = hoehe/2 ;die Höhe wird zu beiden Seiten zugerechnet, also hat eine Seite nur die halbe Höhe
DiffX = x2-x1
Distance = Sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
Winkel = ACos(DiffX/Distance)*57.29577 +90
If y1<y2 : Winkel=360-Winkel : EndIf
fWinkelP =(Winkel +90) *(2*3.14159265/360);positiver Winkel
fWinkelM =(Winkel -90) *(2*3.14159265/360);negativer Winkel
myBox\P1\x = Round(x1 + (hoehe * Sin(fWinkelP)),0)
myBox\P1\y = Round(y1 + (hoehe * Cos(fWinkelP)),0)
myBox\P2\x = Round(x1 + (hoehe * Sin(fWinkelM)),0)
myBox\P2\y = Round(y1 + (hoehe * Cos(fWinkelM)),0)
myBox\P3\x = Round(x2 + (hoehe * Sin(fWinkelM)),0)
myBox\P3\y = Round(y2 + (hoehe * Cos(fWinkelM)),0)
myBox\P4\x = Round(x2 + (hoehe * Sin(fWinkelP)),0)
myBox\P4\y = Round(y2 + (hoehe * Cos(fWinkelP)),0)
LineXY(myBox\P1\x,myBox\P1\y,myBox\P2\x,myBox\P2\y,icolorout)
LineXY(myBox\P2\x,myBox\P2\y,myBox\P3\x,myBox\P3\y,icolorout)
LineXY(myBox\P3\x,myBox\P3\y,myBox\P4\x,myBox\P4\y,icolorout)
LineXY(myBox\P4\x,myBox\P4\y,myBox\P1\x,myBox\P1\y,icolorout)
If Distance >= 1
FillArea(PickPoint\x,PickPoint\y,-1,icolorin)
EndIf
EndProcedure
Procedure SlimLine(x1.i,y1.i,x2.i,y2.i,color.i=#White)
Protected Distance.i, DiffX.i, DiffY.i, iStep.f, a.f, b.f, i.i
Protected NextPoint.POINT, PlotPoint.POINT, fWinkelM.f, fWinkelP.f,Winkel.i
a = 1: b = 1 - a
DiffX = x2-x1
DiffY = y2 -y1
Distance = Sqr(DiffX*DiffX+DiffY*DiffY)
iStep = 1/Distance
Winkel = ACos(DiffX/Distance)*57.29577 +90
If y1<y2 : Winkel=360-Winkel : EndIf
fWinkelP =(Winkel +90) *(2*3.14159265/360);positiver Winkel
fWinkelM =(Winkel -90) *(2*3.14159265/360);negativer Winkel
For i = 1 To Distance
NextPoint\x = x1*a+x2*b
NextPoint\y = y1*a+y2*b
Plot(NextPoint\x,NextPoint\y,Color)
PlotPoint\x = Round(NextPoint\x + (Sin(fWinkelP)),0)
PlotPoint\y = Round(NextPoint\y + (Cos(fWinkelP)),0)
If PlotPoint\x<0 Or PlotPoint\y<0
Else
Plot(PlotPoint\x,PlotPoint\y,Color)
EndIf
PlotPoint\x = Round(NextPoint\x + (Sin(fWinkelM)),0)
PlotPoint\y = Round(NextPoint\y + (Cos(fWinkelM)),0)
If PlotPoint\x<0 Or PlotPoint\y<0
Else
Plot(PlotPoint\x,PlotPoint\y,Color)
EndIf
a = a - iStep
b = 1 - a
Next
EndProcedure
Procedure DashDraw(x1.i,y1.i,x2.i,y2.i,DashValue.i=5,DashWidth.i=3,DashColor.i=#White)
Protected Distance.i, DiffX.i, DiffY.i, a.f, b.f
Protected DashStepMax.f, DashCount.f, PrevPoint.POINT, NextPoint.POINT
Protected ZahlStr.s, Zahl2.i, Odd.i, OddZahl.f
DiffX = x2-x1
DiffY = y2 -y1
Distance = Sqr(DiffX*DiffX+DiffY*DiffY)
DashCount = Distance/(DashValue*2)
;Prüfen auf Nachkommastellen
ZahlStr = StrF(DashCount)
Zahl2 = Val(ZahlStr)
If Zahl2 <> DashCount
Odd = #True
OddZahl = DashCount - Zahl2
EndIf
DashStepMax = 1/(DashCount*2)
If Odd = #True
a = 1 - (DashStepMax*OddZahl)
b = 1 - a
NextPoint\x = x1*a+x2*b
NextPoint\y = y1*a+y2*b
If DashWidth > 2
DrawBox(x1,y1,NextPoint\x,NextPoint\y,DashWidth,DashColor,DashColor)
ElseIf DashWidth = 2
SlimLine(x1,y1,NextPoint\x,NextPoint\y,DashColor)
Else
LineXY(x1,y1,NextPoint\x,NextPoint\y,DashColor)
EndIf
a = a - DashStepMax: b = 1 - a
Else
a=1:b=1-a
EndIf
While a > DashStepMax
PrevPoint\x = x1*a+x2*b
PrevPoint\y = y1*a+y2*b
a = a - DashStepMax: b = 1 - a
NextPoint\x = x1*a+x2*b
NextPoint\y = y1*a+y2*b
If DashWidth > 2
DrawBox(PrevPoint\x,PrevPoint\y,NextPoint\x,NextPoint\y,DashWidth,DashColor,DashColor)
ElseIf DashWidth = 2
SlimLine(PrevPoint\x,PrevPoint\y,NextPoint\x,NextPoint\y,DashColor)
Else
LineXY(PrevPoint\x,PrevPoint\y,NextPoint\x,NextPoint\y,DashColor)
EndIf
a = a - (DashStepMax): b = 1 - a
Wend
If Odd = #False And a>0
NextPoint\x = x1*a+x2*b
NextPoint\y = y1*a+y2*b
If DashWidth > 2
DrawBox(NextPoint\x,NextPoint\y,x2,y2,DashWidth,DashColor,DashColor)
ElseIf DashWidth = 2
SlimLine(NextPoint\x,NextPoint\y,x2,y2,DashColor)
Else
LineXY(NextPoint\x,NextPoint\y,x2,y2,DashColor)
EndIf
EndIf
EndProcedure
; Test
Define POINTA.POINT, POINTB.POINT
Define Hoehe.i, hwnd.i, Event.i
Define WinX.i, WinY.i, WinWidth.i, WinHeight.i
Define ScreenX.i, ScreenY.i, ScreenWidth.i, ScreenHeight.i
WinX = 0: WinY = 0: WinWidth = 300: WinHeight = 300
ScreenX = 0: ScreenY = 0: ScreenWidth = WinWidth: ScreenHeight = WinHeight
POINTA\x = 98: POINTA\y = 150
POINTB\x = ScreenWidth/2: POINTB\y = ScreenHeight/2
hwnd = OpenWindow(0, WinX, WinY, WinWidth, WinHeight, "test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
If OpenWindowedScreen(hwnd,ScreenX,ScreenY,ScreenWidth,ScreenHeight,0,0,0)
Repeat
Repeat
Select WindowEvent ()
Case #PB_Event_CloseWindow
End
Case #Null
Break
EndSelect
ForEver
ClearScreen($000000)
ExamineKeyboard()
ExamineMouse()
POINTA\x = MouseX(): POINTA\y = MouseY()
StartDrawing(ScreenOutput())
DashDraw(POINTA\x,POINTA\y,POINTB\x,POINTB\y,20,4)
StopDrawing()
FlipBuffers()
Until Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
End
Else
MessageRequester("Fehler","Konnte Screen nicht öffnen")
End
EndIf