DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Drawing Shapes in AHK (Proof-of-Concept)  (Read 8,230 times)

Masonjar13

  • Participant
  • Joined in 2014
  • *
  • Posts: 35
Drawing Shapes in AHK (Proof-of-Concept)
« on: August 24, 2014, 07:51 AM »
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:

Instructions:
  •  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
#Persistent
CoordMode, Mouse, Screen
SetBatchLines -1
Process,Priority,,R

; Make Gui's
Gui,1:Add,Button,gCircle w130 h50,Circle
Gui,1:Add,Button,gPolygon w130 h50,Polygon
Gui,1:Add,Button,gSpiral w130 h50,Spiral
Gui,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,,Draw
return

Circle: ; circle gui
Gui,1:Submit
Gui,2:Show,,Draw Circle
return
CircleOK: ; draw circle
Gui,2:Submit
BlockInput 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 MouseMoveOff
return

Spiral: ; spiral gui
Gui,1:Submit
Gui,3:Show,,Draw Spiral
return
SpiralOK: ; draw spiral
Gui,3:Submit
ind:=0
BlockInput MouseMove
MouseMove,% A_ScreenWidth/2,% A_ScreenHeight/2,0
Send {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 MouseMoveOff
return

Polygon: ; polygon gui
Gui,1:Submit
Gui,4:Show,,Draw Polygon
return
PolygonOk: ; draw polygon
Gui,4:Submit
if(Ovec+Ivec > 360){
    msgbox,,Error,Can not have more than 360 vectors.
    return
}
ind:=0
BlockInput MouseMove

GetPosFromAngle(mx,my,A_ScreenWidth/2,A_ScreenHeight/2,Orad,Pang)
MouseMove,% mx,% my,0
Send {LButton down}
Lpc:= Ovec + Ivec
Ovec--
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 MouseMoveOff
return

+2:: ; stop drawing
if(GetKeyState("LButton"))
    Send {LButton up}
reload

+3:: ; kill
if(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)
Tox ID: 065459DFC23CA5D238F2334168150B8727B1D0826C80DD2F49F35B612B401A0837F89E7C9C86
« Last Edit: August 25, 2014, 03:51 AM by Masonjar13 »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Drawing Shapes in AHK (Proof-of-Concept)
« Reply #1 on: August 25, 2014, 12:59 PM »
neat.  :up:

Original address: https://www.donationcoder.com/forum/index.php?topic=38787.0