Andreik Posted December 27, 2011 Share Posted December 27, 2011 (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: expandcollapse popup#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 December 27, 2011 by Andreik When the words fail... music speaks. Link to comment Share on other sites More sharing options...
kylomas Posted December 27, 2011 Share Posted December 27, 2011 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 Link to comment Share on other sites More sharing options...
Yashied Posted December 28, 2011 Share Posted December 28, 2011 (edited) expandcollapse popup#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 December 28, 2011 by Yashied My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
Andreik Posted December 30, 2011 Author Share Posted December 30, 2011 Very nice example Yashied! Thank you very much! When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now