Drehen einer Linie per GDI+

  • Hallo allerseits!

    Ich frage mich, ob jemand den Dreh raus hat, um mir hierbei zu helfen: :)

    Spoiler anzeigen
    [autoit]

    #include <WindowsConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <GDIPlus.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt("GUIOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    Global $Gui = GUICreate("Labyrinths-Frontend", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUISetBkColor(0xFFFFFF)
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    Global $HGraphic = _GDIPlus_GraphicsCreateFromHWND($Gui)

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 0 To 360
    _Draw($i)
    Sleep(1000)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    Sleep(1000)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    _GDIPlus_GraphicsDispose($HGraphic)
    _GDIPlus_Shutdown()

    [/autoit] [autoit][/autoit] [autoit]

    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Draw($_iAngle)
    _GDIPlus_GraphicsClear($HGraphic, 0xFFFFFFFF)
    Local $iHyp = 200

    [/autoit] [autoit][/autoit] [autoit]

    Local $iDeltaX = $iHyp * sin(- $_iAngle)
    Local $iDeltaY = $iHyp * cos(- $_iAngle)
    Local $iCenterX = Round(@DesktopWidth / 2, 0)
    Local $iCenterY = Round(@DesktopHeight / 2, 0)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_GraphicsDrawLine($HGraphic, $iCenterX - $iDeltaX, $iCenterY - $iDeltaY, $iCenterX + $iDeltaX, $iCenterY + $iDeltaY)
    EndFunc

    [/autoit]

    Die Linie soll sich drehen, und zwar Grad für Grad. Leider tut sie das nicht - was mache ich falsch? $iHyp gibt die Hälfte der Länge von der Linie an und bildet die Hypothenuse für Sinus und Cosinus.

    Vielen Dank bereits im Voraus,
    Matthias

    Einmal editiert, zuletzt von MatthiasG. (29. Januar 2011 um 09:20)

  • AutoIt rechnet immer im Bogenmaß.
    Man muss also erst alle Gradzahlen umwandeln.
    (daran bin ich auch schonmal verzweifelt^^)

    [autoit]

    #include <WindowsConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <GDIPlus.au3>

    [/autoit][autoit][/autoit][autoit]

    Opt("GUIOnEventMode", 1)

    [/autoit][autoit][/autoit][autoit]

    Global $Gui = GUICreate("Labyrinths-Frontend", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUISetBkColor(0xFFFFFF)
    GUISetState()

    [/autoit][autoit][/autoit][autoit]

    _GDIPlus_Startup()
    Global $HGraphic = _GDIPlus_GraphicsCreateFromHWND($Gui)

    [/autoit][autoit][/autoit][autoit]

    For $i = 0 To 360
    _Draw($i)
    Sleep(100)
    Next

    [/autoit][autoit][/autoit][autoit]

    While 1
    Sleep(1000)
    WEnd

    [/autoit][autoit][/autoit][autoit]

    Func _Exit()
    _GDIPlus_GraphicsDispose($HGraphic)
    _GDIPlus_Shutdown()

    [/autoit][autoit][/autoit][autoit]

    Exit
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    Func _Draw($_iAngle)
    _GDIPlus_GraphicsClear($HGraphic, 0xFFFFFFFF)
    Local $iHyp = 200
    Local $pi = 3.14159265358979
    Local $degToRad = $pi / 180

    [/autoit][autoit][/autoit][autoit]

    Local $iDeltaX = $iHyp * sin(- $_iAngle*$degToRad)
    Local $iDeltaY = $iHyp * cos(- $_iAngle*$degToRad)
    Local $iCenterX = Round(@DesktopWidth / 2, 0)
    Local $iCenterY = Round(@DesktopHeight / 2, 0)

    [/autoit][autoit][/autoit][autoit]

    _GDIPlus_GraphicsDrawLine($HGraphic, $iCenterX - $iDeltaX, $iCenterY - $iDeltaY, $iCenterX + $iDeltaX, $iCenterY + $iDeltaY)
    EndFunc

    [/autoit]
  • Ich mag Vektoren lieber :P.

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <GUIConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt("GUIOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    $FPS = 40
    $nRotationSpeed = 2

    [/autoit] [autoit][/autoit] [autoit]

    $aV_RotationPoint = _Vector_Create(200, 200)
    $aV_LinePoint1 = _Vector_Create(-100, 0)
    $aV_LinePoint2 = _Vector_Create(100, 0)

    [/autoit] [autoit][/autoit] [autoit]

    $Pi_Div_180 = ACos(-1) / 180

    [/autoit] [autoit][/autoit] [autoit]

    $hWnd = GUICreate("Line Rotation", 400, 400)
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics(400, 400, $hGraphic)
    $hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)

    [/autoit] [autoit][/autoit] [autoit]

    $hPen_Line = _GDIPlus_PenCreate(0xFF000000, 2)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Close", $hWnd)
    AdlibRegister("_Draw", Round(1000/ $FPS))

    [/autoit] [autoit][/autoit] [autoit]

    While Sleep(1000)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _Draw()
    _GDIPlus_GraphicsClear($hBuffer, 0xFFFFFFFF)
    _GDIPlus_GraphicsDrawLine($hBuffer, $aV_LinePoint1[0] + $aV_RotationPoint[0], $aV_LinePoint1[1] + $aV_RotationPoint[1], $aV_LinePoint2[0] + $aV_RotationPoint[0], $aV_LinePoint2[1] + $aV_RotationPoint[1])
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, 400, 400)

    [/autoit] [autoit][/autoit] [autoit]

    $aV_LinePoint1 = _Vector_RotateOnZAxis($aV_LinePoint1, $nRotationSpeed)
    $aV_LinePoint2 = _Vector_RotateOnZAxis($aV_LinePoint2, $nRotationSpeed)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Close()
    AdlibUnRegister()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hBuffer)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_PenDispose($hPen_Line)
    _GDIPlus_Shutdown()
    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Vector_RotateOnZAxis($aV1, $iDegree)
    Local $aV2[2]

    [/autoit] [autoit][/autoit] [autoit]

    $aV2[0] = $aV1[0] * _Cos($iDegree) - $aV1[1] * _Sin($iDegree)
    $aV2[1] = $aV1[1] * _Cos($iDegree) + $aV1[0] * _Sin($iDegree)

    [/autoit] [autoit][/autoit] [autoit]

    Return $aV2
    EndFunc ;==>_Vector_RotateOnZAxis

    [/autoit] [autoit][/autoit] [autoit]

    Func _Vector_Create($iXValue, $iYValue)
    Local $aV1[2]
    $aV1[0] = $iXValue
    $aV1[1] = $iYValue

    [/autoit] [autoit][/autoit] [autoit]

    Return $aV1
    EndFunc ;==>_Vector_Create

    [/autoit] [autoit][/autoit] [autoit]

    Func _Cos($iValue)
    Return Cos($iValue * $Pi_Div_180)
    EndFunc ;==>_Cos

    [/autoit] [autoit][/autoit] [autoit]

    Func _Sin($iValue)
    Return Sin($iValue * $Pi_Div_180)
    EndFunc ;==>_Sin

    [/autoit] [autoit][/autoit] [autoit]

    Func _Tan($iValue)
    Return Tan($iValue * $Pi_Div_180)
    EndFunc ;==>_Tan

    [/autoit]
  • Hallo ihr beiden!

    Vielen Dank, jetzt funktioniert es! Hätte ich aber auch von selbst drauf kommen sollen :S

    Erneut vielen Dank!

    Viele Grüße,
    Matthias

    P.S.: Sorry name22, aber ich habe Marsis Lösung genutzt, da sie platzsparender ist :P

  • Zitat

    P.S.: Sorry name22, aber ich habe Marsis Lösung genutzt, da sie platzsparender ist :P


    Das hab ich auch nicht erwartet ;). Ich musste das Script hier einfach posten, auch wenn es in diesem Fall komplizierter/länger als das Original war ^^. Zumindest wisst ihr jetzt alle wie man das mit Vektoren macht :D.