Functions that use lists

Official forum for open source FreePIE discussion and development.
Post Reply
WestleyTwain
One Eyed Hopeful
Posts: 13
Joined: Mon Jan 29, 2018 4:41 pm

Functions that use lists

Post by WestleyTwain »

Hello again! I'm currently working on my next wii remote mouse script, one that negates the roll of the wiimote, and I've found that thinking of the accelerations in terms of vectors work out really well. For example, the acceleration vector is set up as the following:

Code: Select all

accl = [wiimote[0].acceleration.x, wiimote[0].acceleration.y, wiimote[0].acceleration.z]
The problem is, I need a function that takes in a vector (list of 3 numbers/variables), and spits out another vector, in the same form of a list of 3 numbers/variables, like this:

Code: Select all

def unitVect(v):
	if math.sqrt(sum(n**2 for n in v)) != 0:
		return v/(math.sqrt(sum(n**2 for n in v)))
	else:
		return 0
Which, (in theory,) if given a vector v = [x,y,z], should spit out a different vector unitVect(v) = [a,b,c]

I know this code doesn't work because v here is a list and not a number, but is there a way to treat lists like this in python?
FrenchyKiel
One Eyed Hopeful
Posts: 47
Joined: Thu Oct 03, 2013 7:10 am

Re: Functions that use lists

Post by FrenchyKiel »

like that?

Code: Select all

def fct(x): return x/s


if Keyboard.getPressed(Key.S):
	s = math.sqrt(sum(n**2 for n in v))
	if s != 0:
		a = map(fct, v)
	else:
		a = [0,0,0]
		


if starting:
	v = [1,2,3]
	a = [0,0,0]
	s = 0.0

diagnostics.watch(a[0])
diagnostics.watch(a[1])
diagnostics.watch(a[2])
Post Reply

Return to “FreePIE”