Page 1 of 1

Functions that use lists

Posted: Sat Apr 21, 2018 4:25 pm
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?

Re: Functions that use lists

Posted: Sun Apr 22, 2018 12:44 am
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])