ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE. Software > Coding Snacks

Drawing Shapes in AHK (Proof-of-Concept)


I wrote a function for someone on one of the AHK forums to get coords based off known coords, length, and angle. I then decided to play into it a bit and draw some shapes. Here's a pretty good demo I made, although, please note that for polygons, it will never have more inner-vectors compared to the outer-vectors, because I wanted it that way  :Thmbsup:


*  Run script
*  Open a new paint window, full-screen
*  The rest should be self-explanatory (starting coords are always the center of the screen)AHK-Draw Demo
--- Code: Autohotkey ---#SingleInstance Force#PersistentCoordMode, Mouse, ScreenSetBatchLines -1Process,Priority,,R ; Make Gui'sGui,1:Add,Button,gCircle w130 h50,CircleGui,1:Add,Button,gPolygon w130 h50,PolygonGui,1:Add,Button,gSpiral w130 h50,SpiralGui,2:Add,Text,,Radius:Gui,2:Add,Edit,vlen Limit4,Gui,2:Add,Button,gCircleOK,Draw!Gui,3:Add,Text,,Layer count:Gui,3:Add,Edit,vLcnt Limit5,Gui,3:Add,Text,ym,Narrowness (1 - 100):Gui,3:Add,Edit,vNarrow Limit3,Gui,3:Add,Button,gSpiralOk,Draw!Gui,4:Add,Text,,Outward radius:Gui,4:Add,Edit,vOrad Limit4,Gui,4:Add,Text,,`nInward radius:Gui,4:Add,Edit,vIrad Limit4,Gui,4:Add,Text,ym,Outward vectors (count):Gui,4:Add,Edit,vOvec Limit3,Gui,4:Add,Text,,`nInward vectors (count):Gui,4:Add,Edit,vIvec Limit3,Gui,4:Add,Text,ym,Angle:Gui,4:Add,Edit,vPang Limit4,Gui,4:Add,Button,gPolygonOk,Draw! ; main gui+1::Gui,1:Show,,Drawreturn Circle: ; circle guiGui,1:SubmitGui,2:Show,,Draw CirclereturnCircleOK: ; draw circleGui,2:SubmitBlockInput MouseMove Loop 361{    GetPosFromAngle(mx,my,A_ScreenWidth/2,A_ScreenHeight/2,len,A_Index)    MouseMove,% mx,% my,0    if(A_Index=1)        Send {LButton down}}Send {LButton up}BlockInput MouseMoveOffreturn Spiral: ; spiral guiGui,1:SubmitGui,3:Show,,Draw SpiralreturnSpiralOK: ; draw spiralGui,3:Submitind:=0BlockInput MouseMoveMouseMove,% A_ScreenWidth/2,% A_ScreenHeight/2,0Send {LButton down} Loop %Lcnt%{    Loop 360    {        ind++        GetPosFromAngle(mx,my,A_ScreenWidth/2,A_ScreenHeight/2,ind*(Narrow*0.01),A_Index)        MouseMove,% mx,% my,0    }}Send {LButton up}BlockInput MouseMoveOffreturn Polygon: ; polygon guiGui,1:SubmitGui,4:Show,,Draw PolygonreturnPolygonOk: ; draw polygonGui,4:Submitif(Ovec+Ivec > 360){    msgbox,,Error,Can not have more than 360 vectors.    return}ind:=0BlockInput MouseMove GetPosFromAngle(mx,my,A_ScreenWidth/2,A_ScreenHeight/2,Orad,Pang)MouseMove,% mx,% my,0Send {LButton down}Lpc:= Ovec + IvecOvec--iys:=1 Loop % Lpc{    ind+=360/Lpc    GetPosFromAngle(mx,my,A_ScreenWidth/2,A_ScreenHeight/2,iys?Irad:Orad,ind+Pang)    MouseMove,% mx,% my,0    Ivec-=iys ? 1 : 0    Ovec-=!iys ? 1 : 0    iys:=Ivec < 1 ? 0 : !iys    Sleep 15 ; seems to be inconsistent during multiple runs without delay}Send {LButton up}BlockInput MouseMoveOffreturn +2:: ; stop drawingif(GetKeyState("LButton"))    Send {LButton up}reload +3:: ; killif(GetKeyState("LButton"))    Send {LButton up}ExitApp ; ***function(s)*** GetPosFromAngle(ByRef x2,ByRef y2,x1,y1,len,ang){    ang := (ang-90) * 0.0174532925    x2 := x1 + len * cos(ang)    y2 := y1 + len * sin(ang)}
Demo pics

The hotkeys are Shift+1 to bring up the main gui, Shift+2 to instantly interrupt the drawing, Shift+3 to kill the script.

I encourage modification of this code, but request acknowledgement if my function is used elsewhere, thanks!  8)

neat.  :up:


[0] Message Index

Go to full version