It is currently March 28th, 2024, 9:52 pm

Atan2 math function

Changes made during the Rainmeter 3.2 beta cycle.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Atan2 math function

Post by balala »

eclectic-tech , FreeRaider is right, that's not an issue of the Atan2 function, with your modification, the length of the line (through the LineLength option of the [MeterLineAtoB] meter) is always equal with 0. I'm not sure this approach is good (to be honest I had no time yet to study the Atan2 function, which I didn't used for lately).
Instead probably the following LineLength option will work: LineLength=(Sqrt((([MeterPointA:X]-[MeterPointB:X])**2)+(([MeterPointA:Y]-[MeterPointB:Y])**2))). Here I used the Pythagorean theorem.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Atan2 math function

Post by eclectic-tech »

balala wrote:eclectic-tech , FreeRaider is right, that's not an issue of the Atan2 function, with your modification, the length of the line (through the LineLength option of the [MeterLineAtoB] meter) is always equal with 0. I'm not sure this approach is good (to be honest I had no time yet to study the Atan2 function, which I didn't used for lately).
Instead probably the following LineLength option will work: LineLength=(Sqrt((([MeterPointA:X]-[MeterPointB:X])**2)+(([MeterPointA:Y]-[MeterPointB:Y])**2))). Here I used the Pythagorean theorem.
Well, I used the example that was provided, for the LineLength value. :D

I was wondering what need to be changed in the formula for LineLength (which uses Atan2) to eliminate the zero value, rather than replace it use the Pythagorean theorem; or is that the only solution? (I am not a mathematician, and I'm just getting older!) ;-)

If so, then in the example, Atan2 is only used to determine the Rotation angle, and the LineLength formula should be changed to use the Pythagorean theorem.

Thanks balala and freeraider for the insight! :thumbup:
User avatar
FreeRaider
Posts: 826
Joined: November 20th, 2012, 11:58 pm

Re: Atan2 math function

Post by FreeRaider »

eclectic-tech wrote:Well, I used the example that was provided, for the LineLength value. :D

I was wondering what need to be changed in the formula for LineLength (which uses Atan2) to eliminate the zero value, rather than replace it use the Pythagorean theorem; or is that the only solution? (I am not a mathematician, and I'm just getting older!) ;-)

If so, then in the example, Atan2 is only used to determine the Rotation angle, and the LineLength formula should be changed to use the Pythagorean theorem.

Thanks balala and freeraider for the insight! :thumbup:
Yes, because in your case, you use the polar coordinate system for the LineLength (x=r*cos(theta) and y=r*sin(theta) where theta=atan2(y,x) and r is the radius or the ray or the hypotenuse).
When you use the inverse formula r=x/cos(theta), the problem is in x and in cos(theta) because x=0 and theta=atan2(y,0)=PI()/2 or -PI()/2 (it depends if y > 0 or y < 0), thus cos(PI()/2)=0

So, for the line length, I would use the Pythagorean theorem.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Atan2 math function

Post by balala »

For sure this is not the most elegant and precise solution, but a trick could be used to can use the initial formula.
According to this idea, you'll have to add a very - very small numerical value, to each difference in the LineLength formula of the [MeterLineAtoB] meter. Something like this: LineLength=(([MeterPointB:X]-[MeterPointA:X]+0.0001)/Cos(Atan2([MeterPointB:Y]-[MeterPointA:Y]+0.0001, [MeterPointB:X]-[MeterPointA:X]+0.0001))). The added 0.0001 in reality don't change anything in the result, but the 0 result will be avoided.
Theoretically this added value should be the possible smallest, but in reality (due to the screen resolution), not really matter if you use 0.0001 or 1, both (or any other value, smaller than 1) will give the same result.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Atan2 math function

Post by eclectic-tech »

Thanks for the expanded info FreeRaider (you are more of a mathematician than I am) :)

Balala's trick is also an option, Thanks!

I think it is 'clearer' to just use the theorem in this case.

Guess I need to clean the dust of my 50 year old math skills... Nah! :p
Thanks again for the answers!
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Atan2 math function

Post by balala »

eclectic-tech wrote:I think it is 'clearer' to just use the theorem in this case.
For sure that's the best approach.
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Atan2 math function

Post by mistic100 »

Hey, could you update the first message with the Pythagore version ?

I spend too much time trying to understand why my vertical line didn't show up :-)
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Atan2 math function

Post by balala »

mistic100 wrote:Hey, could you update the first message with the Pythagore version ?

I spend too much time trying to understand why my vertical line didn't show up :-)
It is, by eclectic-tech himself. He edited the initial code, replacing the initial LineLength option of the [MeterLineAtoB] meter. Now his code uses the Pythagorean theorem (see his Edit).
On the other hand, another approach (and solution) would be this. You have to add some small numerical values to each difference in the initial LineLength option (if you want to try this, just replace in eclectic-tech's code, the LineLength option with that posted here).
mistic100
Posts: 35
Joined: October 12th, 2014, 5:27 pm

Re: Atan2 math function

Post by mistic100 »

Ok but it's not the first message, someone (like me) seing an apparently working guide/solution at the beginning of the thread won't read everything.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Atan2 math function

Post by eclectic-tech »

mistic100 wrote:Ok but it's not the first message, someone (like me) seing an apparently working guide/solution at the beginning of the thread won't read everything.
Here is the code from JSMorley's example with the change. (Only JSMorley can edit his post) :D

Code: Select all

[Rainmeter]
Update=1000
BackgroundMode=2
SolidColor=20,30,40,255

[MeasureCounterX]
Measure=Calc
Formula=Random
UpdateRandom=1
LowBound=100
HighBound=300

[MeasureCounterY]
Measure=Calc
Formula=Random
UpdateRandom=1
LowBound=50
HighBound=150

[MeterPointA]
Meter=Image
X=0
Y=[MeasureCounterY]
W=6
H=6
SolidColor=255,0,0,255
DynamicVariables=1

[MeterPointB]
Meter=Image
X=[MeasureCounterX]
Y=0
W=6
H=6
SolidColor=191,235,255,255
DynamicVariables=1

[MeterLineAtoB]
Meter=Roundline
X=([MeterPointA:X]+([MeterPointA:W]/2))
Y=([MeterPointA:Y]+([MeterPointA:H]/2))
LineColor=255,255,255,255
LineWidth=2
LineLength=(Sqrt((([MeterPointA:X]-[MeterPointB:X])**2)+(([MeterPointA:Y]-[MeterPointB:Y])**2)))
StartAngle=Rad(360)
RotationAngle=(Atan2([MeterPointB:Y]-[MeterPointA:Y], [MeterPointB:X]-[MeterPointA:X]))
AntiAlias=1
DynamicVariables=1