Math Functions
Posted: 20 Aug 2018, 15:49
Hello!
I would like the following functions to be added: Map, ATan2, Radians (degrees to radians), Degrees (radians to degrees), Factorial, Log(x,base=10), Hypot, ACosh, ASinh, ATanh, Cosh, Sinh, Tanh.
Constants: PI (also PI2?), E (_E?).
Here is an example using the Map function (AHKv1).
Original here: https://autohotkey.com/boards/viewtopic ... 20#p102945.
I consider that these are the basic functions (and constants) that we should have built-in.
These functions are very useful when working with graphics (eg. gdip) and performance is important.
I would like the following functions to be added: Map, ATan2, Radians (degrees to radians), Degrees (radians to degrees), Factorial, Log(x,base=10), Hypot, ACosh, ASinh, ATanh, Cosh, Sinh, Tanh.
Constants: PI (also PI2?), E (_E?).
Here is an example using the Map function (AHKv1).
Original here: https://autohotkey.com/boards/viewtopic ... 20#p102945.
Code: Select all
#NoEnv
#Warn All, Off
#SingleInstance Force
SetBatchLines -1
; ===============================================================================================================================
global w := 800
global h := 600
global a := 0
global b := 0
global hue := 0
; ===============================================================================================================================
hSHLWAPI := DllCall("LoadLibrary", "Str", "shlwapi.dll", "Ptr")
hGDIPLUS := DllCall("LoadLibrary", "Str", "GdiPlus.dll", "Ptr")
VarSetCapacity(SI, 24, 0), Numput(1, SI, 0, "Int")
DllCall("GdiPlus.dll\GdiplusStartup", "UPtr*", pToken, "Ptr", &SI, "Ptr", 0)
; ===============================================================================================================================
Gui, +LastFound +hwndhGDIPFun
Gui, Show, w%w% h%h%
DllCall("GdiPlus.dll\GdipCreateFromHWND", "Ptr", hGDIPFun, "Ptr*", pGraphics)
DllCall("GdiPlus.dll\GdipCreateBitmapFromGraphics", "Int", w, "Int", h, "Ptr", pGraphics, "Ptr*", pBitmap)
DllCall("Gdiplus.dll\GdipGetImageGraphicsContext", "Ptr", pBitmap, "Ptr*", pGraphicsCtxt)
DllCall("Gdiplus.dll\GdipSetSmoothingMode", "Ptr", pGraphicsCtxt, "Int", 2)
DllCall("Gdiplus.dll\GdipCreatePen1", "UInt", 0xFFFFFFFF, "Float", 1, "Int", 2, "Ptr*", pPen)
DllCall("GdiPlus.dll\GdipGraphicsClear", "Ptr", pGraphicsCtxt, "UInt", 0x7F000000)
loop
{
DllCall("GdiPlus.dll\GdipSetPenColor", "Ptr", pPen, "UInt", "0xCC" ColorHLSToRGB(hue, 120, 240))
, X0 := map( sin(a) , -1, 1, W/5, W - W/5 )
, Y0 := map( cos(a)*1.5, -1, 1, H/5, H - H/5 )
, X1 := map( sin(b) , -1, 1, W/5, W - W/5 )
, Y1 := map( cos(b) , -1, 1, H/5, H - H/5 )
, DllCall("GdiPlus.dll\GdipDrawLine", "ptr", pGraphicsCtxt, "ptr", pPen, "float", x0, "float", y0, "float", x1, "float", y1)
, a += 0.04, b += 0.05, hue += 1, hue := (hue > 239) ? mod(hue, 239) : hue
, DllCall("Gdiplus.dll\GdipDrawImage", "Ptr", pGraphics, "Ptr", pBitmap, "Float", 0, "Float", 0)
} until (a > 24)
return
; ===============================================================================================================================
ColorHLSToRGB(hue, lum, sat)
{
clr := DllCall("shlwapi.dll\ColorHLSToRGB", "UShort", hue, "UShort", lum, "UShort", sat)
return Format("{:06X}", ((clr & 0xff00) | ((clr & 0xff) << 16) | ((clr & 0xff0000) >> 16)))
}
Map(Value, Start1, Stop1, Start2, Stop2, ConstrainMin := FALSE, ConstrainMax := FALSE)
{
return ( ConstrainMin && Value < Start1 ) ? Start2 : ( ConstrainMax && Value > Stop1 ) ? Stop2
: ( ( ( Value - Start1 ) / (Stop1 - Start1) ) * ( Stop2 - Start2 ) ) + Start2
}
; ===============================================================================================================================
GuiClose:
GuiEscape:
DllCall("GdiPlus.dll\GdipDeletePen", "Ptr", pPen)
DllCall("GdiPlus.dll\GdipDeleteGraphics", "Ptr", pGraphicsCtxt)
DllCall("Gdiplus.dll\GdipDisposeImage", "Ptr", pBitmap)
DllCall("GdiPlus.dll\GdipDeleteGraphics", "Ptr", pGraphics)
DllCall("GdiPlus.dll\GdiplusShutdown", "Ptr", pToken)
DllCall("FreeLibrary", "Ptr", hGDIPLUS)
DllCall("FreeLibrary", "Ptr", hSHLWAPI)
ExitApp
return
These functions are very useful when working with graphics (eg. gdip) and performance is important.