Jump to content

Recommended Posts

Posted (edited)

Hey guys, I'm trying to create an analog clock but I don't know exactly how to make the hour, min & sec hands to rotate corectly (they are triangle shape - check the behaviour from 3 and 9). I have some ideas in my mind how to fix that but maybe this can be done with some math.

Here is my code:

#include <GDIPlus.au3>

Global Const $PI = 3.1415926535897932384626433832795
Global Const $Width = 10
Global $HourPoly[4][2], $MinPoly[4][2], $SecPoly[4][2]

$HourPoly[0][0] = 3
$HourPoly[1][0] = 512 - $Width
$HourPoly[1][1] = 384
$HourPoly[2][0] = 512 + $Width
$HourPoly[2][1] = 384

$MinPoly[0][0] = 3
$MinPoly[1][0] = 512 - $Width
$MinPoly[1][1] = 384
$MinPoly[2][0] = 512 + $Width
$MinPoly[2][1] = 384

$SecPoly[0][0] = 3
$SecPoly[1][0] = 512 - $Width
$SecPoly[1][1] = 384
$SecPoly[2][0] = 512 + $Width
$SecPoly[2][1] = 384

$hMain = GUICreate("Analog clock",1024,768,0,0,0x80000000,0x00000008)
GUISetState(@SW_SHOW,$hMain)

_GDIPlus_Startup()
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hMain)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics(1024,768,$hGraphics)
$hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "clock.bmp")
$hHourBrush = _GDIPlus_BrushCreateSolid(0xFF004080)
$hMinBrush = _GDIPlus_BrushCreateSolid(0xFF0080FF)
$hSecBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)

While True
    If GUIGetMsg() = -3 Then ExitLoop
    _GDIPlus_GraphicsClear($hBackBuffer)
    _GDIPlus_GraphicsDrawImage($hBackBuffer,$hImage,0,0)
    $HourPoly[3][0] = 512 + Cos(TimeToRadians("hour")) * 165
    $HourPoly[3][1] = 384 - Sin(TimeToRadians("hour")) * 165
    $MinPoly[3][0] = 512 + Cos(TimeToRadians("min")) * 220
    $MinPoly[3][1] = 384 - Sin(TimeToRadians("min")) * 220
    $SecPoly[3][0] = 512 + Cos(TimeToRadians("sec")) * 300
    $SecPoly[3][1] = 384 - Sin(TimeToRadians("sec")) * 300
    _GDIPlus_GraphicsFillPolygon($hBackBuffer,$HourPoly,$hHourBrush)
    _GDIPlus_GraphicsFillPolygon($hBackBuffer,$MinPoly,$hMinBrush)
    _GDIPlus_GraphicsFillPolygon($hBackBuffer,$SecPoly,$hSecBrush)
    _GDIPlus_GraphicsDrawImage($hGraphics,$hBitmap,0,0)
    Sleep(10)
WEnd

_GDIPlus_BrushDispose($hSecBrush)
_GDIPlus_BrushDispose($hMinBrush)
_GDIPlus_BrushDispose($hHourBrush)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_GraphicsDispose($hBackBuffer)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()

Func TimeToRadians($sTimeType)
    Local $Sec = @SEC, $Min = @MIN, $Hour = @HOUR
    Switch $sTimeType
        Case "sec"
            Return ($PI / 2) - ($Sec  * ($PI / 30))
        Case "min"
            Return ($PI / 2) - ($Min  * ($PI / 30)) - (Int($Sec / 10) * ($PI / 180))
        Case "hour"
            Return ($PI / 2) - ($Hour * ($PI / 6 )) - ($Min / 12) * ($PI / 30)
    EndSwitch
EndFunc

And the background image:

Edited by Andreik
Posted

andreik,

There are several threads about clocks, one of which is this thread dealing with hand positioning...

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted (edited)

#Include <GDIPlus.au3>
#Include <WinAPIEx.au3>

Global Const $Width = 10
Global $HourPoly[4][2], $MinPoly[4][2], $SecPoly[4][2], $TempPoly[3]
Global $Update = True

$HourPoly[0][0] = 3
$HourPoly[0][1] = -1 ; Previous angle
$HourPoly[1][0] = 512 - $Width
$HourPoly[1][1] = 384
$HourPoly[2][0] = 512 + $Width
$HourPoly[2][1] = 384
$HourPoly[3][0] = 512
$HourPoly[3][1] = 154
$TempPoly[0] = $HourPoly

$MinPoly[0][0] = 3
$MinPoly[0][1] = -1 ; Previous angle
$MinPoly[1][0] = 512 - $Width
$MinPoly[1][1] = 384
$MinPoly[2][0] = 512 + $Width
$MinPoly[2][1] = 384
$MinPoly[3][0] = 512
$MinPoly[3][1] = 104
$TempPoly[1] = $MinPoly

$SecPoly[0][0] = 3
$SecPoly[0][1] = -1 ; Previous angle
$SecPoly[1][0] = 512 - $Width
$SecPoly[1][1] = 384
$SecPoly[2][0] = 512 + $Width
$SecPoly[2][1] = 384
$SecPoly[3][0] = 512
$SecPoly[3][1] = 62
$TempPoly[2] = $SecPoly

$hMain = GUICreate("Analog clock", 1024, 768, 0, 0, 0x80000000, 0x00000008)
GUISetState(@SW_SHOW, $hMain)

_GDIPlus_Startup()
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hMain)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics(1024, 768, $hGraphics)
$hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBackBuffer, 2)
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "clock.bmp")
$hHourBrush = _GDIPlus_BrushCreateSolid(0xFF004080)
$hMinBrush = _GDIPlus_BrushCreateSolid(0xFF0080FF)
$hSecBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)

While True
    If GUIGetMsg() = -3 Then ExitLoop
    $Angle = Round(360 * @SEC / 60)
    If $SecPoly[0][1] <> $Angle Then
        $TempPoly[2] = $SecPoly
        $SecPoly[0][1] = $Angle
        _WinAPI_RotatePoints($TempPoly[2], $SecPoly[3][0], $SecPoly[1][1], $Angle, 1, 3)
        $Update = True
    EndIf
    $Angle = Round(360 * @MIN / 60)
    If $MinPoly[0][1] <> $Angle Then
        $TempPoly[1] = $MinPoly
        $MinPoly[0][1] = $Angle
        _WinAPI_RotatePoints($TempPoly[1], $MinPoly[3][0], $MinPoly[1][1], $Angle, 1, 3)
        $Update = True
    EndIf
    $Angle = Round(360 * (@HOUR - 12) / 24 + $MinPoly[0][1] / 72)
    If $HourPoly[0][1] <> $Angle Then
        $TempPoly[0] = $HourPoly
        $HourPoly[0][1] = $Angle
        _WinAPI_RotatePoints($TempPoly[0], $HourPoly[3][0], $HourPoly[1][1], $Angle, 1, 3)
        $Update = True
    EndIf
    If $Update Then
        _GDIPlus_GraphicsClear($hBackBuffer, 0)
        _GDIPlus_GraphicsDrawImage($hBackBuffer, $hImage, 0, 0)
        _GDIPlus_GraphicsFillPolygon($hBackBuffer, $TempPoly[0], $hHourBrush)
        _GDIPlus_GraphicsFillPolygon($hBackBuffer, $TempPoly[1], $hMinBrush)
        _GDIPlus_GraphicsFillPolygon($hBackBuffer, $TempPoly[2], $hSecBrush)
        _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
        $Update = False
    EndIf
WEnd

_GDIPlus_BrushDispose($hSecBrush)
_GDIPlus_BrushDispose($hMinBrush)
_GDIPlus_BrushDispose($hHourBrush)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_GraphicsDispose($hBackBuffer)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()

Edited by Yashied

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...