Code: Alles auswählen
Structure POINTF
x.f : y.f
EndStructure
Procedure RotatePoint(*center.POINTF, *point.POINTF, angle.f, *result.POINTF)
*result\x = (*point\x-*center\x) * Cos(angle) - (*point\y-*center\y) * Sin(angle) + *center\x
*result\y = (*point\x-*center\x) * Sin(angle) + (*point\y-*center\y) * Cos(angle) + *center\y
EndProcedure
Procedure DrawRotatedRect(*center.POINTF,*rect.RECT,angle.f,color=0)
Protected.POINTF p1,p2,p3,p4
Dim points.POINTF(4)
points(1)\x = *rect\left
points(1)\y = *rect\top
points(2)\x = *rect\right
points(2)\y = *rect\top
points(3)\x = *rect\right
points(3)\y = *rect\bottom
points(4)\x = *rect\left
points(4)\y = *rect\bottom
RotatePoint(*center,@points(1),angle,p1)
RotatePoint(*center,@points(2),angle,p2)
RotatePoint(*center,@points(3),angle,p3)
RotatePoint(*center,@points(4),angle,p4)
LineXY(p1\x,p1\y,p2\x,p2\y,color)
LineXY(p2\x,p2\y,p3\x,p3\y,color)
LineXY(p3\x,p3\y,p4\x,p4\y,color)
LineXY(p4\x,p4\y,p1\x,p1\y,color)
EndProcedure
degree = 45
OpenWindow(0,0,0,800,600,"Drehung",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(0,0,0,800,600)
If StartDrawing(CanvasOutput(0))
Define.POINTF center
Define.RECT rect
center\x = 400
center\y = 300
rect\left = 300
rect\right= 500
rect\top = 200
rect\bottom = 400
Box(0,0,rect\right-rect\left,rect\bottom-rect\top,0)
DrawRotatedRect(center,rect,Radian(degree))
StopDrawing()
EndIf
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow : End
Case #PB_Event_Gadget
If EventGadget()=0
If EventType()=#PB_EventType_MouseMove
Define.POINTF mouse, result
; Mauskoordinaten holen
mouse\x = GetGadgetAttribute(0,#PB_Canvas_MouseX)
mouse\y = GetGadgetAttribute(0,#PB_Canvas_MouseY)
; Mauskoordinaten zurück rotieren
RotatePoint(center,mouse,Radian(-degree),result)
; checken ob (zurück-rotierte) Mauskoordinaten in der (unrotierten!) Box
If result\x >= rect\left And result\x <= rect\right
If result\y >= rect\top And result\y <= rect\bottom
If StartDrawing(CanvasOutput(0))
; Box bei 0,0
Box(0,0,rect\right-rect\left,rect\bottom-rect\top,0)
; Mauskoordinate auf die Box bei 0,0
Plot(result\x - rect\left, result\y - rect\top, RGB($FF,$FF,$00))
StopDrawing()
EndIf
EndIf
EndIf
EndIf
EndIf
EndSelect
ForEver