python - How to compare two vectors in vPython -


i trying simulate rubik's cube. in order detect if user has solved cube remember initial position vectors , compare it.

however, when start program , mess cube, press 'k' solve can see in console values in fact same, have different precision. example z-value -0.99999 instead of -1. result of flaw though values quite same, program still won't consider cube solved. guess while rotating, when calculations performed on vector, precision changes , result in end values different. how can solve problem? print out initial position vectors , current vectors press 'a' key anytime wish :)

from visual import * import string import random  class cube:     def __init__(self):         self.surfaces = { 'r': (color.red, (1, 0, 0)),                                'o': (color.orange, (-1, 0, 0)),                               'y': (color.yellow, (0, 1, 0)),                                'b': (color.blue, (0, -1, 0)),                               'w': (color.white, (0, 0, 1)),                                'g': (color.green, (0, 0, -1))}          self.fps = 30         self.wholesurfaces = []         self.initialposition = []         self.commandslist = []      def createcube(self):          colour, axis in self.surfaces.itervalues():             x in (-1, 0, 1):                 y in (-1, 0, 1):                      # start powierzchniaboczna on top face, rotate them "down"                     # appropriate face.                     powboczna = box(color=colour, pos=(x, y, 1.5),                                   length=0.98, height=0.98, width=0.05)                      cos_kat = dot((0, 0, 1), axis)                      if cos_kat == 0: #alfa = 90 + kpi                         obliczonaosobrotu = cross((0, 0, 1), axis) #iloczyn wektorowy                     else:                          obliczonaosobrotu=(1, 0, 0)                      powboczna.rotate(angle=acos(cos_kat), axis=obliczonaosobrotu, origin=(0, 0, 0))                     self.wholesurfaces.append(powboczna)                      #remember initial position                     v = (float(powboczna.pos.x), float(powboczna.pos.y), float(powboczna.pos.z))                     self.initialposition.append(v)      def solvecube(self):         print self.commandslist          self.commandslist.reverse()          print self.commandslist         in self.commandslist:             self.rotatecube(self.reversecommand(i), 10000)         self.commandslist = []      def reversecommand(self, key):         if (key.islower()): return key.upper()         else: return key.lower()      def rotatecube(self, key, refreshrate):                 colour, axis = self.surfaces[key.lower()]                  if (key.isupper()): kat = (pi / 2.0)                  else: kat = -pi/2.0                   r in arange(0, kat, kat / self.fps):                     rate(refreshrate)                       surface in self.wholesurfaces:                         if dot(surface.pos, axis) > 0.5:                              surface.rotate(angle=kat / self.fps, axis=axis, origin=(0, 0, 0))      def beginloop(self):                while true:             key = scene.kb.getkey()              if (key.lower() in self.surfaces):                 self.commandslist.append(key)                 self.rotatecube(key, self.fps)             elif key == "k":                 self.solvecube()             elif key == "a":                 = 0                 print "================="                 surface in self.wholesurfaces:                         print "%s\n(%s,%s,%s)" % (self.initialposition[i], surface.pos.x, surface.pos.y, surface.pos.z)                         if self.initialposition[i][0] == float(surface.pos.x) , self.initialposition[i][1] == float(surface.pos.y) , self.initialposition[i][2] == float(surface.pos.z): print "equal"                         else: print "not equal"                         print ""                         i+=1  if __name__ == "__main__":     mycube = cube()     mycube.createcube()     mycube.beginloop() 

the solution simple, need use numpy.allclose method given precision.

for surface in self.wholesurfaces:     print "%s\n%s" % (self.initialposition[i], powierzchnia.pos)     if np.allclose(self.initialposition[i], surface.pos.astuple(), 1e-5, 1e-5): print "are equal"     else: print "arent equal"     i+=1 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -