It is currently April 25th, 2024, 5:37 pm

TransformationMatrix functions

Discuss the use of Lua in Script measures.
User avatar
XANCI
Posts: 104
Joined: September 18th, 2011, 6:37 am
Location: Nanjing, China

TransformationMatrix functions

Post by XANCI »

Scale x by a, y by b based point (x,y)

Code: Select all

function Scale(x,y,a,b)
	local t={a,0,0,b,(1-a)*x,(1-b)*y}
	return t
end
Rotate r(radian) around point (x,y)

Code: Select all

function Rotate(x,y,r)
	local t={math.cos(r),math.sin(r),-math.sin(r),math.cos(r),x*(1-math.cos(r))+y*math.sin(r),y*(1-math.cos(r))-x*math.sin(r)}
	return t
end
Multiply matrix a by b

Code: Select all

function Multiply(a,b)
	local t={}
	t[1]=a[1]*b[1]+a[2]*b[3]
	t[2]=a[1]*b[2]+a[2]*b[4]
	t[3]=a[3]*b[1]+a[4]*b[3]
	t[4]=a[3]*b[2]+a[4]*b[4]
	t[5]=a[5]*b[1]+a[6]*b[3]+b[5]
	t[6]=a[5]*b[2]+a[6]*b[4]+b[6]
	return t
end
Output a matrix as a string used by TransformationMatrix

Code: Select all

function Ouput(t)
	local s=t[1]..';'..t[2]..';'..t[3]..';'..t[4]..';'..t[5]..';'..t[6]
	return s
end
Example:
Scale both x&y by 2 and rotate 90 degree around (100,100)

Code: Select all

TM0={1,0,0,1,0,0}
TM1=Scale(100,100,2,2)
TM2=Rotate(100,100,math.pi/4)
TM=Multiply(Multiply(iTM0,iTM1),iTM2)
SKIN:Bang('!SetOption','Meter','TransformationMatrix',Output(TM))
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: TransformationMatrix functions

Post by smurfier »

XANCI wrote: Output a matrix as a string used by TransformationMatrix

Code: Select all

function Ouput(t)
	local s=t[1]..';'..t[2]..';'..t[3]..';'..t[4]..';'..t[5]..';'..t[6]
	return s
end

Code: Select all

function Ouput(t)
	return table.concat(t, ';')
end
Also, if you set your matrices up a bit different, this function will multiply them regardless of size as long as the columns in matrixA equals the rows in matrixB.

Code: Select all

matrixA = {
	{1,0,0},
	{0,1,0},
	{0,0,1},
}

matrixB = {
	{0,0,1},
	{0,1,0},
	{1,0,0},
}

function Multiply(tbl1, tbl2)
	local temp = {}
	for row = 1, #tbl1 do
		temp[row] = {}
		for col = 1, #tbl2[1] do
			local num = 0
			for row2 = 1, #tbl1[1] do
				num = num + tbl1[row][row2] * tbl2[row][col]
			end
			temp[row][col] = num
		end
	end
	return temp
end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
MerlinTheRed
Rainmeter Sage
Posts: 889
Joined: September 6th, 2011, 6:34 am

Re: TransformationMatrix functions

Post by MerlinTheRed »

Can you give an example of how one might use this in a skin? I imagine the uses cases are rather complex, but probably I'm wrong.

I think it might be even more useful to have a Lua script that creates a TransformationMatrix from simple, user-understandable parameters like rotation angle, translation and scale. Of course the space of all possible transformations would not be completely represented by this but it will enable not-so-math-savvy users to use TransformationMatrix.
Have more fun creating skins with Sublime Text 2 and the Rainmeter Package!
Unight
Posts: 19
Joined: November 20th, 2012, 5:12 am

Re: TransformationMatrix functions

Post by Unight »

smurfier wrote:

Code: Select all

function Ouput(t)
	return table.concat(t, ';')
end
Also, if you set your matrices up a bit different, this function will multiply them regardless of size as long as the columns in matrixA equals the rows in matrixB.

Code: Select all

matrixA = {
	{1,0,0},
	{0,1,0},
	{0,0,1},
}

matrixB = {
	{0,0,1},
	{0,1,0},
	{1,0,0},
}

function Multiply(tbl1, tbl2)
	local temp = {}
	for row = 1, #tbl1 do
		temp[row] = {}
		for col = 1, #tbl2[1] do
			local num = 0
			for row2 = 1, #tbl1[1] do
				num = num + tbl1[row][row2] * tbl2[row][col]
			end
			temp[row][col] = num
		end
	end
	return temp
end
This function is more Interesting. :)