Sign in to follow this  
Followers 0
AlmarM

Calculate clock hand end angle

6 posts in this topic

Hiya,

im having some problems about calculating the clock hand end angle's.

I searched a bit around the internet, but I didnt found any formula wich I could get working.

This is a simple test example.

#include <GDIPlus.au3>
#include <Math.au3>

$GUI = GUICreate("")
GUISetState()

;~ $endX = 
;~ $endY = 

_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($GUI)
_GDIPlus_GraphicsDrawLine($hGraphic, 200, 200, $endX, $endY)

_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()

While 1
    Switch GUIGetMsg()
    Case -3
        Exit
    EndSwitch
WEnd

Halp! ;)


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Might want to look at this:

http://www.autoitscript.com/forum/index.php?showtopic=62779&st=0&p=469946&hl=_initDigChar&fromsearch=1&#entry469946

Edited by JoHanatCent

Share this post


Link to post
Share on other sites

Hi,

You can also have a look in AutoIts installed Example folder Examples\GUI\Advanced\Clock.au3

Cos an Sin make my head spin ;)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I made a very Maybe it is helpful for you how the angles are calculated.;)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

I made a very simple GDI+ clock! Maybe it is helpful for you how the angles are calculated.;)

;coded by UEZ 2010 build 2010-08-24
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

Local $hGUI, $hGraphics, $hBackbuffer, $hBitmap, $hPen1, $hPen2
Local $iX = 600, $iY = 600
Local $newY = $iX, $newX = $iY
Local $min = $iX * 0.25
Local $max = $iX * 1.5
Local $cX = $iX * 0.5, $cY = $iY * 0.5
Local $deg = ACos(-1) / 180
Local $radius = 500, $cR = $radius * 0.50
Local $cR1 = $cR * 0.90, $cR2 = $cR * 0.20
Local $cR3 = $cR * 0.80, $cR4 = $cR * 0.15
Local $cR5 = $cR * 0.50, $cR6 = $cR * 0.10
Local $sek = @SEC * 6 - 90
Local $min = @MIN * 6 + (@SEC / 10) - 90
Local $std = @HOUR * 30 + (@MIN / 2) - 90

Local $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4, $x5, $x6, $y5, $y6
; Initialize GDI+
_GDIPlus_Startup()

$hGUI = GUICreate("GDI+ Simple Clock by UEZ 2010", $iX, $iY, -1, -1, $WS_SIZEBOX)
GUISetState()

$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($iX, $iY, $hGraphics)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

; Using antialiasing
_GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2)

; Create a Pen object
$hPen1 = _GDIPlus_PenCreate(0xFF101010, 4)
$hPen2 = _GDIPlus_PenCreate(0x901010F0, 6)

Local $ws = WinGetPos($hGUI)
Local $ratio = $ws[3] / $ws[2]

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

AdlibRegister("Ticker", 50)

GUIRegisterMsg($WM_SIZE, "WM_SIZE")
GUIRegisterMsg($WM_SIZING, "WM_SIZING")
GUIRegisterMsg($WM_ERASEBKGND, "WM_ERASEBKGND")
GUIRegisterMsg($WM_GETMINMAXINFO, "WM_GETMINMAXINFO")

Local $timer = 100
GUIRegisterMsg($WM_TIMER, "Draw") ;$WM_TIMER = 0x0113
DllCall("User32.dll", "int", "SetTimer", "hwnd", $hGUI, "int", 0, "int", $timer, "int", 0)

While Sleep(100000000)
WEnd

Func Draw()
    _GDIPlus_GraphicsClear($hBackbuffer, 0xFFFFFFFF)

    _GDIPlus_GraphicsDrawLine($hBackbuffer, $cX - $cR, $cY, $cX - $cR + 35, $cY, $hPen1)
    _GDIPlus_GraphicsDrawLine($hBackbuffer, $cX + $cR, $cY, $cX + $cR - 35, $cY, $hPen1)
    _GDIPlus_GraphicsDrawLine($hBackbuffer, $cX, $cY - $cR, $cX, $cY - $cR + 35, $hPen1)
    _GDIPlus_GraphicsDrawLine($hBackbuffer, $cX, $cY + $cR, $cX, $cY + $cR - 35, $hPen1)

    _GDIPlus_GraphicsDrawEllipse($hBackbuffer, $cX - $cR, $cY - $cR, $radius, $radius, $hPen1)

    $x1 = $cX + Cos($sek * $deg) * $cR1
    $y1 = $cY + Sin($sek * $deg) * $cR1
    $x2 = $cX + Cos(($sek + 180) * $deg) * $cR2
    $y2 = $cY + Sin(($sek + 180) * $deg) * $cR2
    _GDIPlus_GraphicsDrawLine($hBackbuffer, Floor($x1), Floor($y1), Floor($x2), Floor($y2), $hPen2)

    $x3 = $cX + Cos($min * $deg) * $cR3
    $y3 = $cY + Sin($min * $deg) * $cR3
    $x4 = $cX; + Cos(($min + 180) * $deg) * $cR4
    $y4 = $cY; + Sin(($min + 180) * $deg) * $cR4
    _GDIPlus_GraphicsDrawLine($hBackbuffer, $x3, $y3, $x4, $y4, $hPen2)

    $x5 = $cX + Cos($std * $deg) * $cR5
    $y5 = $cY + Sin($std * $deg) * $cR5
    $x6 = $cX; + Cos(($std + 180) * $deg) * $cR6
    $y6 = $cY; + Sin(($std + 180) * $deg) * $cR6
    _GDIPlus_GraphicsDrawLine($hBackbuffer, $x5, $y5, $x6, $y6, $hPen2)

    _GDIPlus_GraphicsDrawEllipse($hBackbuffer, $cX - 3, $cY - 3, 6, 6, $hPen1)

    _GDIPlus_GraphicsDrawString($hBackbuffer, @HOUR & ":" & @MIN & ":" & @SEC, 0, 0)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $iX, $iY)
    _WinAPI_RedrawWindow($hGUI)
EndFunc

Func WM_SIZE($hWnd, $Msg, $wParam, $lParam)
    $iX = BitAND($lParam, 0x0000FFFF)
    $iY = BitShift(BitAND($lParam, 0xFFFF0000), 16)
;~  ConsoleWrite("x: " & $iX & ", y: " & $iY & @CRLF)
    _WinAPI_RedrawWindow($hGUI)
    Return "GUI_RUNDEFMSG"
EndFunc

Func WM_SIZING($hWnd, $iMsg, $wParam, $lParam)
;~  Local Const $WMSZ_LEFT = 1
;~  Local Const $WMSZ_RIGHT = 2
    Local $sRect = DllStructCreate("Int[4]", $lParam)
    Local $left = DllStructGetData($sRect, 1, 1)
    Local $top = DllStructGetData($sRect, 1, 2)
    Local $right = DllStructGetData($sRect, 1, 3)
    Local $bottom = DllStructGetData($sRect, 1, 4)

    Switch $wParam ;drag side or corner
    Case 1, 2
    $newY = ($right - $left) * $ratio
    DllStructSetData($sRect, 1, DllStructGetData($sRect, 1, 2) + $newY, 4)
    Case Else
    $newX = ($bottom - $top) / $ratio
    DllStructSetData($sRect, 1, DllStructGetData($sRect, 1, 1) + $newX, 3)
    EndSwitch
    Return "GUI_RUNDEFMSG"
EndFunc ;==>WM_SIZING

Func WM_ERASEBKGND($hWnd, $Msg, $wParam, $lParam)
    Local $hGraphicsTemp = _GDIPlus_GraphicsCreateFromHDC($wParam)
    _GDIPlus_GraphicsDrawImageRect($hGraphicsTemp, $hBitmap, 0, 0, $iX, $iY)
    _GDIPlus_GraphicsDispose($hGraphicsTemp)
    Return True
EndFunc ;==>_WM_ERASEBKGND

Func WM_GETMINMAXINFO($hWnd, $Msg, $wParam, $lParam)
    Local $minmaxinfo = DllStructCreate("int;int;int;int;int;int;int;int;int;int", $lParam)
    DllStructSetData($minmaxinfo, 7, 150) ; min X
    DllStructSetData($minmaxinfo, 8, 150) ; min Y
    DllStructSetData($minmaxinfo, 9, 768) ; max X
    DllStructSetData($minmaxinfo, 10, 768) ; max Y
    Return "GUI_RUNDEFMSG"
EndFunc ;==>WM_GETMINMAXINFO

Func Ticker()
    $sek = @SEC * 6 - 90
    $min = @MIN * 6 + (@SEC * 0.10) - 90
    $std = @HOUR * 30 + (@MIN * 0.50) - 90
EndFunc

Func _Exit()
    AdlibUnRegister("Ticker")
    GUIRegisterMsg($WM_TIMER, "")
    GUIRegisterMsg($WM_GETMINMAXINFO, "")
    GUIRegisterMsg($WM_SIZE, "")
    GUIRegisterMsg($WM_ERASEBKGND, "")
    GUIRegisterMsg($WM_SIZING, "")

    ; Clean up GDI+ resources
    _GDIPlus_PenDispose($hPen1)
    _GDIPlus_PenDispose($hPen2)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hBackbuffer)
    _GDIPlus_GraphicsDispose($hGraphics)

    ; Uninitialize GDI+
    _GDIPlus_Shutdown()
    Exit
EndFunc

Br,

UEZ

Could you split the code from it's math and explain the math to me? :)

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

Hi,

You can also have a look in AutoIts installed Example folder Examples\GUI\Advanced\Clock.au3

Cos an Sin make my head spin :graduated:

The hour hand doesn't seem to update correctly. It kind of bleeds as it goes around the clock face. Any fix for this?

Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0