Imprecision in the trig functions Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Awesome Quest
Posts: 7
Joined: 18 Jun 2020, 17:16

Imprecision in the trig functions

29 Nov 2021, 09:55

Hi everyone,
I'm writing a tool to draw shapes in OneNote 2016 and having trouble with the sin and cos functions.

I'm using this code to draw a right angle triangle and a rotated square.

Code: Select all

!q::{
	pi := 3.14159265358979323846
	MouseGetPos(x, y)
	; Draw a square rotated by 120 degrees from mouse position
	Scale := 150
	rad:=120*pi/180
	cosRad := cos(rad)
	sinRad := sin(rad)
	; This is set up so that when rad = 0 the mouse is in the top left corner
	; rad then rotates it counter clockwise
	MouseClickDrag("LButton",x,y,x+Scale*cosRad,y-Scale*sinRad,1)
	MouseClickDrag("LButton",0,0, Scale*sinRad, Scale*cosRad,1,"R")
	MouseClickDrag("LButton",0,0,-Scale*cosRad, Scale*sinRad,1,"R")
	MouseClickDrag("LButton",0,0,-Scale*sinRad,-Scale*cosRad,1,"R")

	; Draw a right angle triangle with 30 degree angle and mouse position
	Scale := 500
	rad:=30*pi/180
	MouseClickDrag("LButton",x,y,x+scale,y,1)
	MouseClickDrag("LButton",x+scale,y,x+scale,y-scale*sin(rad),1)
	MouseClickDrag("LButton",x+scale,y-scale*sin(rad),x,y,1)
}
And it produces this when put into OneNote 2016
Screenshot 2021-11-29 143749.png
Screenshot 2021-11-29 143749.png (23.69 KiB) Viewed 916 times

As we can see, they are not parallel. And if you look really close at the square we can see
Screenshot 2021-11-29 142627.png
Screenshot 2021-11-29 142627.png (3.38 KiB) Viewed 916 times
The corners don't line up.

I'm wondering if there's a simple solution to this or if there is just a more precise sin function I could use?

What method does autohotkey use for sin?
Last edited by gregster on 30 Nov 2021, 14:09, edited 1 time in total.
Reason: Topic moved from regular 'Ask For Help' (v1).
Awesome Quest
Posts: 7
Joined: 18 Jun 2020, 17:16

Re: Imprecision in the trig functions

30 Nov 2021, 09:22

Update:
I tried rewriting the square code so that it no longer uses relative mode.

Code: Select all

!q::{
	pi := 3.14159265358979323846
	MouseGetPos(x, y)
	; Draw a square rotated by 120 degrees from mouse position
	Scale := 150
	rad:=120*pi/180
	cosRad := cos(rad)
	sinRad := sin(rad)
	; This is set up so that when rad = 0 the mouse is in the top left corner
	; rad then rotates it counter clockwise
	; MouseClickDrag("LButton",x,y,x+Scale*cosRad,y-Scale*sinRad,1)
	; MouseClickDrag("LButton",0,0, Scale*sinRad, Scale*cosRad,1,"R")
	; MouseClickDrag("LButton",0,0,-Scale*cosRad, Scale*sinRad,1,"R")
	; MouseClickDrag("LButton",0,0,-Scale*sinRad,-Scale*cosRad,1,"R")
	
	MouseClickDrag("LButton",x,y,x+Scale*cosRad,y-Scale*sinRad,1)
	x:=x+Scale*cosRad
	y:=y-Scale*sinRad
	MouseClickDrag("LButton",x,y,x+Scale*sinRad,y+Scale*cosRad,1)
	x:=x+Scale*sinRad
	y:=y+Scale*cosRad
	MouseClickDrag("LButton",x,y,x-Scale*cosRad,y+Scale*sinRad,1)
	x:=x-Scale*cosRad
	y:=y+Scale*sinRad
	MouseClickDrag("LButton",x,y,x-Scale*sinRad,y-Scale*cosRad,1)


	MouseGetPos(x, y)
	; Draw a right angle triangle with 30 degree angle and mouse position
	Scale := 500
	rad:=30*pi/180
	MouseClickDrag("LButton",x,y,x+scale,y,1)
	MouseClickDrag("LButton",x+scale,y,x+scale,y-scale*sin(rad),1)
	MouseClickDrag("LButton",x+scale,y-scale*sin(rad),x,y,1)
}
And it seems to have fixed the miss aligned kroners of the square
fixedsquare.png
fixedsquare.png (53.21 KiB) Viewed 839 times
not fixed paralell.png
not fixed paralell.png (49.91 KiB) Viewed 839 times
but it has not fixed the two shapes alignment.
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Imprecision in the trig functions

30 Nov 2021, 09:33

It could also be OneNote being part of the imprecision, no?
I suggest you print out all the values and disprove that either ahk is imprecise or oneNote is. :think:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Rohwedder
Posts: 7615
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Imprecision in the trig functions

30 Nov 2021, 12:17

Hallo,
Error: Call to nonexistent function.
Specifically: MouseGetPos(x, y)
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Imprecision in the trig functions

30 Nov 2021, 12:27

Rohwedder wrote:
30 Nov 2021, 12:17
Hallo,
Error: Call to nonexistent function.
Specifically: MouseGetPos(x, y)
I think this is AHK v2 beta maybe? or some command-as-functions kind of thing?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: Imprecision in the trig functions  Topic is solved

30 Nov 2021, 13:25

@Awesome Quest, I am not a geometer by any means but it's fairly easy to prove that the bottom edge of the square you drew is not parallel to the hypotenuse of the triangle. The slopes differ by 15%. See my additional lines in the code below:

Code: Select all

!q::{
   pi := 3.14159265358979323846
   MouseGetPos(x, y)
   ; Draw a square rotated by 120 degrees from mouse position
   Scale := 150
   rad:=120*pi/180
   cosRad := cos(rad)
   sinRad := sin(rad)
   ; This is set up so that when rad = 0 the mouse is in the top left corner
   ; rad then rotates it counter clockwise
   ; MouseClickDrag("LButton",x,y,x+Scale*cosRad,y-Scale*sinRad,1)
   ; MouseClickDrag("LButton",0,0, Scale*sinRad, Scale*cosRad,1,"R")
   ; MouseClickDrag("LButton",0,0,-Scale*cosRad, Scale*sinRad,1,"R")
   ; MouseClickDrag("LButton",0,0,-Scale*sinRad,-Scale*cosRad,1,"R")
   
   MouseClickDrag("LButton",x,y,x+Scale*cosRad,y-Scale*sinRad,1)  ; Left edge (bottom to top)
   x:=x+Scale*cosRad
   y:=y-Scale*sinRad
   MouseClickDrag("LButton",x,y,x+Scale*sinRad,y+Scale*cosRad,1)  ; Top edge (left to right)
   x:=x+Scale*sinRad
   y:=y+Scale*cosRad
   MouseClickDrag("LButton",x,y,x-Scale*cosRad,y+Scale*sinRad,1)  ; Right edge (top to bottom)
   x:=x-Scale*cosRad
   y:=y+Scale*sinRad
   MouseClickDrag("LButton",x,y,x-Scale*sinRad,y-Scale*cosRad,1)  ; Bottom edge (right to left)
   SlopeOfSquareBottomEdge := -(y - (y-Scale*cosRad)) / (x - (x-Scale*sinRad))
   SlopeOfSquareBottomEdge := -cosRad / sinRad
   ; SlopeOfSquareBottomEdge := -cos(120) / sin(120)
   ; SlopeOfSquareBottomEdge := sin(30) / cos(30)
   ; SlopeOfSquareBottomEdge := tan(30)  ; 0.577


   MouseGetPos(x, y)
   ; Draw a right angle triangle with 30 degree angle and mouse position
   Scale := 500
   rad:=30*pi/180
   MouseClickDrag("LButton",x,y,x+scale,y,1)  ; Horizontal edge (left to right)
   MouseClickDrag("LButton",x+scale,y,x+scale,y-scale*sin(rad),1)  ; Vertical edge (bottom to top)
   MouseClickDrag("LButton",x+scale,y-scale*sin(rad),x,y,1)  ; Hypotenuse (top-right to bottom-left)
   SlopeOfHypotenuse := -(y-scale*sin(rad) - y) / (x+scale - x)
   SlopeOfHypotenuse := (scale*sin(rad)) / (scale)
   SlopeOfHypotenuse := sin(rad)
   ; SlopeOfHypotenuse := sin(30)  ; 0.5
}
My intuition is that your calculation of the coordinates of the right edge of the square is incorrect.

I hope this helps.

- iPhilip
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Archimede, CraigM, KazNaka, MiM and 29 guests