It is currently April 19th, 2024, 9:48 pm

Attempting to spin and elongate/shorten a rectangle with transformation matrices

Get help with creating, editing & fixing problems with skins
AndrewBot88
Posts: 14
Joined: June 23rd, 2012, 11:28 pm

Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by AndrewBot88 »

Me again! I've got a slightly more complex problem this time. I'm trying to have a rectangle spin around its bottom point and change size as it does so such that its length is always equal to L + L*cos(θ) (where L is the original length). The rotation is simple enough and I've got that working; the scale is a bit more complicated and I'm not entirely sure if it's working (I can't really test it without having both work together). To figure out the X and Y scaling I created this triangle:

Image

I used the values for the legs as the X and Y scale values in my matrix. If you see an error here just let me know and go ahead and ignore the rest of this post.

Otherwise, that gives me this matrix (A) for the rotation:

Code: Select all

cos(θ)  -sin(θ)  Rtx
sin(θ)   cos(θ)  Rty
0        0       1
Where Rtx and Rty are the X and Y translation for the rotation. As mentioned before, this works fine. The scaling matrix (B) is:

Code: Select all

Sx  0  Stx
0   Sy Sty
0   0   1
(I think you can figure out what the variables stand for)

As best I can tell this works; using it without the rotation matrix makes the rectangle stay in place and change its dimensions in a way that seems plausible. Getting the product of A*B gives us:

Code: Select all

Sx*cos(θ)  -Sy*sin(θ)  Stx*cos(θ)-Sty*sin(θ)+Rtx
Sx*sin(θ)   Sy*cos(θ)  Stx*cos(θ)+Sty*cos(θ)+Rty
0           0          1
But using this matrix causes the rectangle to fly all over the place and just generally not do what I would like it to. Can anybody help?

In case it helps, here is the complete code for the skin:
[Rainmeter]
Update=50

[MeasureAngle]
Measure=Calc
Formula=(MeasureAngle % (2*Pi)) + 0.2

[MeasureRotateTX]
Measure=Calc
Formula=150-Cos(MeasureAngle)*150-Sin(MeasureAngle)*150
[MeasureRotateTY]
Measure=Calc
Formula=150+Sin(MeasureAngle)*150-Cos(MeasureAngle)*150

[MeasureScaleX]
Measure=Calc
Formula=Clamp(Cos(MeasureAngle)+Cos(MeasureAngle)**2, 1, 2)
[MeasureScaleY]
Measure=Calc
Formula=Clamp(Sin(MeasureAngle)+Sin(MeasureAngle)*Cos(MeasureAngle), 1, 2)
[MeasureScaleTX]
Measure=Calc
Formula=150-150*MeasureScaleX
[MeasureScaleTY]
Measure=Calc
Formula=150-150*MeasureScaleY

[MeasureFinalTX]
Measure=Calc
Formula=MeasureScaleTX*Cos(MeasureAngle)-MeasureScaleTY*Sin(MeasureAngle)+MeasureRotateTX
[MeasureFinalTY]
Measure=Calc
Formula=MeasureScaleTX*Sin(MeasureAngle)+MeasureScaleTY*Cos(MeasureAngle)+MeasureRotateTY

[MeterBG]
Meter=IMAGE
w=300
h=300
SolidColor=200,200,255,255
[MeterRectangle]
Meter=IMAGE
w=50
h=10
x=150
y=145
SolidColor=0,0,0
;TransformationMatrix=(Cos([MeasureAngle]));(-1*Sin([MeasureAngle]));(Sin([MeasureAngle]));(Cos([MeasureAngle]));[MeasureRotateTX];[MeasureRotateTY]
;TransformationMatrix=[MeasureScaleX];0;0;1;[MeasureScaleTX];0
;TransformationMatrix=([MeasureScaleX]*Cos([MeasureAngle]));(-1*[MeasureScaleY]*Sin([MeasureAngle]));([MeasureScaleX]*Sin([MeasureAngle]));([MeasureScaleY]*Cos([MeasureAngle]));[MeasureFinalTX];[MeasureFinalTY]
AntiAlias=1
DynamicVariables=1
Last edited by AndrewBot88 on January 21st, 2017, 8:02 pm, edited 1 time in total.
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by balala »

First of all, again you've messed up the rotation matrix. You have to be careful, because according to the manual (the third frame in the Transformations section):

Code: Select all

TransformationMatrix
This defines a 3x2 matrix which can be used to transform the meter. There must be exactly 6 values separated by ';'-character (e.g. "1;0;0;1;0;0") which define the matrix in order of "m11;m12;m21;m22;m31;m32".

            |m11 m21 m31|
            |m12 m22 m32|
which means that your rotation matrix (the first matrix you've posted) is a bit wrong: the negative term should be the other sinus, from the second row, first column, not from the first row, second column.
According to this, the matrix would be:

Code: Select all

cos(θ)    sin(θ)    Rtx
-sin(θ)   cos(θ)    Rty
0         0         1
This will affect the calculations made to get the final matrix. This matrix would be:

Code: Select all

Sx*cos(θ)    Sx*sin(θ)    Sx*Rtx+Stx
-Sy*sin(θ)   Sy*cos(θ)    Sy*Rty+Sty
0            0            1
According to this result, you have to replace the Formula options of the [MeasureRotateTX], [MeasureRotateTY], [MeasureFinalTX] and [MeasureFinalTY] measures, as it follows (leave untouched the other measures):

Code: Select all

[MeasureRotateTX]
Measure=Calc
Formula=( 150 - ( 150 * Cos ( MeasureAngle )) - ( 150 * Sin ( MeasureAngle )))

[MeasureRotateTY]
Measure=Calc
Formula=( 150 + ( 150 * Sin ( MeasureAngle )) - ( 150 * Cos ( MeasureAngle )))

[MeasureFinalTX]
Measure=Calc
Formula=( MeasureScaleX * MeasureRotateTX + MeasureScaleTX )

[MeasureFinalTY]
Measure=Calc
Formula=( MeasureScaleY * MeasureRotateTY + MeasureScaleTY )
I know that's not yet perfect, but in the followings, I'll try to figure out better formulas.
AndrewBot88
Posts: 14
Joined: June 23rd, 2012, 11:28 pm

Re: Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by AndrewBot88 »

According to the documentation and the guide (Transformations section, where it specifies the letters) the syntax of the following matrix:

Code: Select all

a  b  c
d  e  f
0  0  1
should be

Code: Select all

TransformationMatrix=a;b;d;e;c;f
unless I'm horribly misreading something.
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by balala »

AndrewBot88 wrote:unless I'm horribly misreading something.
Yeah, I think you're "horribly misreading something"!
I can't find the following form of the matrix in the Transformation Matrix Guide:
AndrewBot88 wrote:

Code: Select all

a  b  c
d  e  f
0  0  1
which you've posted.
Everywhere I see the following form:
AndrewBot88 wrote:

Code: Select all

a  c  tx
b  d  ty
0  0  1
and the corresponding TransformationMatrix option: TransformationMatrix=a;b;c;d;tx;ty.
Also read the third frame of the Transformations section of the TransformationMatrix Guide (you've also talked about this section), which says:

Code: Select all

TransformationMatrix
This defines a 3x2 matrix which can be used to transform the meter. There must be exactly 6 values separated by ';'-character (e.g. "1;0;0;1;0;0") which define the matrix in order of "m11;m12;m21;m22;m31;m32".

            |m11 m21 m31|
            |m12 m22 m32|
Follow the order of terms in the matrix and in the text. You can see that the order you're using is totally wrong. You've interchanged the place of more terms. If you'll place them properly, I think your problems, at least partially, will be fixed.
In the meantime, I think I fixed the code, here is it:

Code: Select all

[Rainmeter]
Update=50

[MeasureAngle]
Measure=Calc
Formula=(MeasureAngle % (2*Pi)) + 0.2

[MeasureRotateTX]
Measure=Calc
Formula=( 150 - ( 150 * Cos ( MeasureAngle )) - ( 150 * Sin ( MeasureAngle )))

[MeasureRotateTY]
Measure=Calc
Formula=( 150 + ( 150 * Sin ( MeasureAngle )) - ( 150 * Cos ( MeasureAngle )))

[MeasureScaleX]
Measure=Calc
Formula=Clamp(Cos(MeasureAngle)+Cos(MeasureAngle)**2, 1, 2)

[MeasureScaleY]
Measure=Calc
Formula=Clamp(Sin(MeasureAngle)+Sin(MeasureAngle)*Cos(MeasureAngle), 1, 2)

[MeasureScaleTX]
Measure=Calc
Formula=150-150*MeasureScaleX

[MeasureScaleTY]
Measure=Calc
Formula=150-150*MeasureScaleY

[MeasureFinalTX]
Measure=Calc
Formula=( MeasureScaleTX * Cos(MeasureAngle) + MeasureRotateTX )

[MeasureFinalTY]
Measure=Calc
Formula=( MeasureRotateTY - MeasureScaleTX * Sin(MeasureAngle))

[MeterBG]
Meter=IMAGE
w=300
h=300
SolidColor=200,200,255,255
LeftMouseUpAction=[!TogglePauseMeasure "MeasureAngle"]

[MeterRectangle]
Meter=IMAGE
w=50
h=10
x=150
y=145
SolidColor=0,0,0
TransformationMatrix=([MeasureScaleX]*Cos([MeasureAngle]));(-1*[MeasureScaleX]*Sin([MeasureAngle]));(Sin([MeasureAngle]));(Cos([MeasureAngle]));[MeasureFinalTX];[MeasureFinalTY]
AntiAlias=1
DynamicVariables=1
I rewrote more measures, please take a look at the above code and tell me if it is what you need.
AndrewBot88
Posts: 14
Joined: June 23rd, 2012, 11:28 pm

Re: Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by AndrewBot88 »

I'm using the documentation because I assume that is the most recently updated. According to it, the syntax is

Code: Select all

TransformationMatrix={scale x};{skew x};{skew y};{scale y};{move x};{move y}
Then I take a look at the guide, which uses this matrix (as mentioned)

Code: Select all

a  c  tx
b  d  ty
0  0  1
and says

Code: Select all

a is used to scale the x value
d is used to scale the y value
c is used to skew the x value
b is used to skew the y value
Matched up to the documentation syntax: a=scale x;c=skew x;b=skew y;d=scale y;tx=move x;ty=move y.

So it would be a;c;b;d;tx;ty not a;b;c;d;tx;ty as you've said.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5397
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by eclectic-tech »

The 'layman' term has a typo :( ... that needs to be corrected ...

It should be

Code: Select all

TransformationMatrix={scale x};{skew y};{skew x};{scale y};{move x};{move y}
I apologize for this because I inadvertently switch the terms when I suggested this ... my bad! :x
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by balala »

AndrewBot88 wrote:

Code: Select all

a  c  tx
b  d  ty
0  0  1
With this matrix, the order of the elements should be TransformationMatrix=a;b;c;d;tx;ty. I can assure you this is the correct order.
But finally does my code work?
AndrewBot88
Posts: 14
Joined: June 23rd, 2012, 11:28 pm

Re: Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by AndrewBot88 »

eclectic-tech wrote:The 'layman' term has a typo :( ... that needs to be corrected ...

It should be

Code: Select all

TransformationMatrix={scale x};{skew y};{skew x};{scale y};{move x};{move y}
I apologize for this because I inadvertently switch the terms when I suggested this ... my bad! :x
That would explain it!
balala wrote:But finally does my code work?
Yes it does, thank you!
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Attempting to spin and elongate/shorten a rectangle with transformation matrices

Post by balala »

You're welcome.