It is currently Sun Nov 18, 2018 3:01 pm



Reply to topic  [ 2 posts ] 
 Functions that use lists 
Author Message
One Eyed Hopeful

Joined: Mon Jan 29, 2018 4:41 pm
Posts: 13
Reply with quote
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:
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:
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?


Sat Apr 21, 2018 4:25 pm
Profile
One Eyed Hopeful

Joined: Thu Oct 03, 2013 7:10 am
Posts: 45
Reply with quote
like that?

Code:
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])


Sun Apr 22, 2018 12:44 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by STSoftware.