def collide(self, Object): results = false Position = None X = self.x Y = self.y if (Position != None): X = Position.x Y = Position.y #Figure out what tiles we need to check against selectionX = Math.floor((Object.x - X) / self._tileSize) selectionY = Math.floor((Object.y - Y) / self._tileSize) selectionWidth = selectionX + (Math.ceil( Object.width / self._tileSize)) + 1 selectionHeight = selectionY + Math.ceil( Object.height / self._tileSize) + 1 #Then bound these coordinates by the map edges if (selectionX < 0): selectionX = 0 if (selectionY < 0): selectionY = 0 if (selectionWidth > self.widthInTiles): selectionWidth = self.widthInTiles if (selectionHeight > self.heightInTiles): selectionHeight = self.heightInTiles #Then loop through this selection of tiles and call FlxObject.separate() accordingly rowStart = selectionY * self.widthInTiles row = selectionY overlapFound = false deltaX = X - self.last.x deltaY = Y - self.last.y #print "selection=====" #print selectionX,selectionY,selectionWidth,selectionHeight while (row <= selectionHeight): column = selectionX while (column <= selectionWidth): overlapFound = false #print "at="+str(int(rowStart+column)) tile = self._tileObjects[self._data[int(rowStart + column)]] #print "row,column",row,column,tile.allowCollisions #print "data="+str(self._data[int(rowStart+column)]) if (tile.allowCollisions): tile.x = X + column * self._tileSize tile.y = Y + row * self._tileSize tile.last.x = tile.x - deltaX tile.last.y = tile.y - deltaY #print "===========" #print tile.x,tile.y,self._tileSize #print Object.x,Object.y,Object.width,Object.height mycollide.collide(Object, tile) #raw_input() column += 1 rowStart += self.widthInTiles row += 1 return results
def canMoveTo(self, x, y, board): # This implementation isn't valid for the knight if type(self) == Knight: raise NotImplementedError vector = Math.getVectorFromCoordinates(self.x, self.y, x, y) #vector.prt() validPieces = [ pc for pc in board if pc.__name__ != "E" and not self == pc ] collinearPieces = [ pc for pc in validPieces if Math.areCollinear( Math.getVectorFromCoordinates(pc.x, pc.y, x, y), vector) ] onTheWay = [ pc for pc in collinearPieces if Math.isInTheInterval(self.x, x, pc.x) and Math.isInTheInterval(self.y, y, pc.y) ] return True if len(onTheWay) == 0 or len(onTheWay) == 1 and onTheWay[ 0].x == x and onTheWay[0].y == y else False
def deserialize(self, p_content): """ see Serializer.""" if len(p_content) < 12: return None msg1 = Temperature() msg2 = RelativeHumidity() msg3 = FluidPressure() org = struct.unpack('fff', p_content) msg1.header.stamp = rospy.Time.now() # round float value to double. msg1.temperature = Math.roundFloatToDouble(org[0]) msg1.variance = 0 msg2.header.stamp = rospy.Time.now() msg2.relative_humidity = Math.roundFloatToDouble(org[1]) msg2.variance = 0 msg3.header.stamp = rospy.Time.now() msg3.fluid_pressure = Math.roundFloatToDouble(org[2]) msg3.variance = 0 msgs = [msg1, msg2, msg3] if CommonConfig.DEBUG_FLAG is True: rospy.logdebug('temperature=' + str(msg1.temperature)) rospy.logdebug('humidity=' + str(msg2.relative_humidity)) rospy.logdebug('pressure=' + str(msg3.fluid_pressure)) return msgs
def __init__(self, MapData, TileGraphic, CollisionIndex=1, DrawIndex=1): #image = pyglet.resource.image("data/logo.png") FlxCore.__init__(self) self.CollideIndex = CollisionIndex self.DrawIndex = 1 self._ci = CollisionIndex self.widthInTiles = 0 self.heightInTiles = 0 self._data = FlxArray() #c; #cols:Array; rows = open(MapData).read().split("\n") rows.reverse() rows = rows[2:] self.heightInTiles = len(rows) for r in range(self.heightInTiles): cols = rows[r].split(",") if (len(cols) <= 1): self.heightInTiles -= 1 continue if (self.widthInTiles == 0): self.widthInTiles = len(cols) for c in range(self.widthInTiles): self._data.append(int(cols[c])) self._pixels = TileGraphic self._rects = FlxArray() self._p = FlxPoint() self._tileSize = self._pixels.height self.width = self.widthInTiles * self._tileSize self.height = self.heightInTiles * self._tileSize self.numTiles = self.widthInTiles * self.heightInTiles for i in range(self.numTiles): if (self._data[i] >= DrawIndex): self._rects.append( FlxRect(self._tileSize * self._data[i], 0, self._tileSize, self._tileSize)) else: self._rects.append(None) #self._block = FlxBlock(0,0,self._tileSize,self._tileSize,None); self._screenRows = int(Math.ceil(FlxG.height / self._tileSize) + 1) if (self._screenRows > self.heightInTiles): self._screenRows = self.heightInTiles self._screenCols = int(Math.ceil(FlxG.width / self._tileSize) + 1) if (self._screenCols > self.widthInTiles): self._screenCols = self.widthInTiles self._tileObjects = range(self._pixels.width / self._pixels.height) i = 0 while (i < self._pixels.width / self._pixels.height): collide = FlxCore.NONE if (i >= self.CollideIndex): collide = self.allowCollisions self._tileObjects[i] = FlxTile(self, i, self._tileSize, self._tileSize, (i >= self.DrawIndex), collide) i += 1
def overlapsWithCallback(self,Object,Callback=None,FlipCallbackParams=false,Position=None): results = false; X = self.x; Y = self.y; if(Position != None): X = Position.x; Y = Position.y; #Figure out what tiles we need to check against selectionX = Math.floor((Object.x - X)/self._tileSize) selectionY = Math.floor((Object.y - Y)/self._tileSize) selectionWidth = selectionX + (Math.ceil(Object.width/self._tileSize)) + 1; selectionHeight = selectionY + Math.ceil(Object.height/self._tileSize) + 1; #Then bound these coordinates by the map edges if(selectionX < 0): selectionX = 0; if(selectionY < 0): selectionY = 0; if(selectionWidth > self.widthInTiles): selectionWidth = self.widthInTiles; if(selectionHeight > self.heightInTiles): selectionHeight = self.heightInTiles; #Then loop through this selection of tiles and call FlxObject.separate() accordingly rowStart = selectionY*self.widthInTiles; row = selectionY; overlapFound=false; deltaX = X - self.last.x; deltaY = Y - self.last.y; while(row < selectionHeight): column = selectionX; while(column < selectionWidth): #print "row,column",row,column overlapFound = false; tile = self._tileObjects[self._data[int(rowStart+column)]]; if(tile.allowCollisions): tile.x = X+column*self._tileSize; tile.y = Y+row*self._tileSize; tile.last.x = tile.x - deltaX; tile.last.y = tile.y - deltaY; if(Callback != None): if(FlipCallbackParams): overlapFound = Callback(Object,tile); else: overlapFound = Callback(tile,Object); else: overlapFound = (Object.x + Object.width > tile.x) and (Object.x < tile.x + tile.width) and (Object.y + Object.height > tile.y) and (Object.y < tile.y + tile.height); if(overlapFound): if((tile.callback != None) and ((tile.filter == None) or (Object is tile.filter))): tile.mapIndex = rowStart+column; tile.callback(tile,Object); results = true; elif((tile.callback != None) and ((tile.filter == None) or (Object is tile.filter))): tile.mapIndex = rowStart+column; tile.callback(tile,Object); column+=1; rowStart += self.widthInTiles; row+=1; return results;
def collide(self,Object): results = false; Position=None X = self.x; Y = self.y; if(Position != None): X = Position.x; Y = Position.y; #Figure out what tiles we need to check against selectionX = Math.floor((Object.x - X)/self._tileSize) selectionY = Math.floor((Object.y - Y)/self._tileSize) selectionWidth = selectionX + (Math.ceil(Object.width/self._tileSize)) + 1; selectionHeight = selectionY + Math.ceil(Object.height/self._tileSize) + 1; #Then bound these coordinates by the map edges if(selectionX < 0): selectionX = 0; if(selectionY < 0): selectionY = 0; if(selectionWidth > self.widthInTiles): selectionWidth = self.widthInTiles; if(selectionHeight > self.heightInTiles): selectionHeight = self.heightInTiles; #Then loop through this selection of tiles and call FlxObject.separate() accordingly rowStart = selectionY*self.widthInTiles; row = selectionY; overlapFound=false; deltaX = X - self.last.x; deltaY = Y - self.last.y; #print "selection=====" #print selectionX,selectionY,selectionWidth,selectionHeight while(row <= selectionHeight): column = selectionX; while(column <=selectionWidth): overlapFound = false; #print "at="+str(int(rowStart+column)) tile = self._tileObjects[self._data[int(rowStart+column)]]; #print "row,column",row,column,tile.allowCollisions #print "data="+str(self._data[int(rowStart+column)]) if(tile.allowCollisions): tile.x = X+column*self._tileSize; tile.y = Y+row*self._tileSize; tile.last.x = tile.x - deltaX; tile.last.y = tile.y - deltaY; #print "===========" #print tile.x,tile.y,self._tileSize #print Object.x,Object.y,Object.width,Object.height mycollide.collide(Object,tile) #raw_input() column+=1; rowStart += self.widthInTiles; row+=1; return results;
def overlapsPoint(self,x,y,PerPixel = false): tx = x; ty = y; if((self.scrollFactor.x <> 1) or (self.scrollFactor.y <> 1)): tx -= Math.floor(FlxG.scroll.x*self.scrollFactor.x); ty -= Math.floor(FlxG.scroll.y*self.scrollFactor.y); if((self.x < tx+1) or (self.x+1 > tx+self.width) or (self.y < ty+1) or (self.y+1 > ty+self.height)): return false; return true;
def __init__(self, s=np.array([0, 0, 0]), v=np.array([0, 0, 1]), t=np.arange(0, 10, 1)): self._s = s self._v = v self._t = t self._light = Math.vector_trans(s) + Math.dot(t, v)
def __init__(self,MapData, TileGraphic, CollisionIndex=1, DrawIndex=1): #image = pyglet.resource.image("data/logo.png") FlxCore.__init__(self); self.CollideIndex = CollisionIndex;self.DrawIndex = 1; self._ci = CollisionIndex; self.widthInTiles = 0; self.heightInTiles = 0; self._data = FlxArray(); #c; #cols:Array; rows = open(MapData).read().split("\n"); rows.reverse() rows=rows[2:] self.heightInTiles = len(rows); for r in range(self.heightInTiles): cols = rows[r].split(","); if(len(cols) <= 1): self.heightInTiles-=1; continue; if(self.widthInTiles == 0): self.widthInTiles = len(cols); for c in range(self.widthInTiles): self._data.append(int(cols[c])); self._pixels = TileGraphic self._rects = FlxArray(); self._p = FlxPoint(); self._tileSize =self._pixels.height; self.width = self.widthInTiles*self._tileSize; self.height = self.heightInTiles*self._tileSize; self.numTiles = self.widthInTiles*self.heightInTiles; for i in range(self.numTiles): if(self._data[i] >= DrawIndex): self._rects.append(FlxRect(self._tileSize*self._data[i],0,self._tileSize,self._tileSize)); else: self._rects.append(None); #self._block = FlxBlock(0,0,self._tileSize,self._tileSize,None); self._screenRows =int( Math.ceil(FlxG.height/self._tileSize)+1); if(self._screenRows > self.heightInTiles): self._screenRows = self.heightInTiles; self._screenCols = int(Math.ceil(FlxG.width/self._tileSize)+1); if(self._screenCols > self.widthInTiles): self._screenCols = self.widthInTiles; self._tileObjects = range(self._pixels.width/self._pixels.height) i=0 while(i < self._pixels.width/self._pixels.height): collide=FlxCore.NONE if(i>= self.CollideIndex): collide=self.allowCollisions self._tileObjects[i] =FlxTile(self,i,self._tileSize,self._tileSize,(i >= self.DrawIndex),collide) i+=1;
def overlapsPoint(self,X,Y,PerPixel = false): tx = self.x; ty = self.y; if((self.scrollFactor.x != 1) or (self.scrollFactor.y != 1)): tx -= Math.floor(FlxG.scroll.x*self.scrollFactor.x); ty -= Math.floor(FlxG.scroll.y*self.scrollFactor.y); if(PerPixel): return self._pixels.hitTest(Point(0,0),0xFF,Point(X-tx,Y-ty)); elif((X <= tx) or (X >= tx+self.width) or (Y <= ty) or (Y >= ty+self.height)): return false; return true;
def __init__(self): self.Root = Node() self.Calculations = Math() self.Current = self.Root self.Elements = [] self.buf = "" self.Operations = [ "1.Example input", "2.User input", "3.Inorder", "4.Find node", "5.Current node", "6.Set Current Node as Root", "7.Math operations", "8.Menu", "9.Quit" ] self.Math_Operations = ["1.Sum", "2.Average", "3.Medium", "4.Back"]
def __init__(self, size, phisic_map): pygame.init() self.delta_x, self.delta_y = size[0] / 2, size[1] / 2 self.size = size self.screen = pygame.display.set_mode(size, flags=pygame.DOUBLEBUF) self.map = phisic_map self.cam_pos = (-2, 0, 0) self.cam_angles = (0, 0, 0) self.local_cam_angles = (0, 0, 0) self.math = Math() self.pygame = pygame self.scale = 500
def render_point(self, rel_x, rel_y, color=39): if not Math.in_range(rel_x, 0, 1) or not Math.in_range(rel_y, 0, 1): return x = rel_x * (self.resolution[0] - 1) y = rel_y * (self.resolution[1] - 1) x = round(x) y = round(y) # point_address = int(round(x) + self.resolution.y * round(y)) self.render_grid['%i;%i' % (x, y)] = '+' self.render_grid_color['%i;%i' % (x, y)] = "\e[%sm" % (color)
def overlaps(self,Core): tx= self.x; ty= self.y; if((self.scrollFactor.x <> 1) or (self.scrollFactor.y <> 1)): tx -= Math.floor(FlxG.scroll.x*self.scrollFactor.x); ty -= Math.floor(FlxG.scroll.y*self.scrollFactor.y); cx = Core.x; cy = Core.y; if((Core.scrollFactor.x <> 1) or (Core.scrollFactor.y <> 1)): cx -= Math.floor(FlxG.scroll.x*Core.scrollFactor.x); cy -= Math.floor(FlxG.scroll.y*Core.scrollFactor.y); if((cx <= tx-Core.width) or (cx >= tx+self.width) or (cy <= ty-Core.height) or (cy >= ty+self.height)): return false; return true;
def __init__(self): self.Math = Math() self.Object = Object self.pygame = pygame self.EventSystem = EventSystem self.cameras = [] self.Loader = Loader
def canMoveTo(self, x, y, board): # This implementation isn't valid for the knight if type(self) == Knight: raise NotImplementedError vector = Math.getVectorFromCoordinates(self.x, self.y, x, y) #vector.prt() validPieces = [pc for pc in board if pc.__name__ != "E" and not self == pc] collinearPieces = [pc for pc in validPieces if Math.areCollinear(Math.getVectorFromCoordinates(pc.x, pc.y, x, y), vector)] onTheWay = [pc for pc in collinearPieces if Math.isInTheInterval(self.x, x, pc.x) and Math.isInTheInterval(self.y, y, pc.y)] return True if len(onTheWay) == 0 or len(onTheWay) == 1 and onTheWay[0].x == x and onTheWay[0].y == y else False
def render(self): #NOTE: While this will only draw the tiles that are actually on screen, it will ALWAYS draw one screen's worth of tiles FlxCore.render(self) self.getScreenXY(self._p) #print self._p.x,self._p.y #self.position=(self._p.x,self._p.y) #raw_input() tx = Math.floor(-self._p.x / self._tileSize) ty = Math.floor(-self._p.y / self._tileSize) if (tx < 0): tx = 0 if (tx > self.widthInTiles - self._screenCols): tx = self.widthInTiles - self._screenCols if (ty < 0): ty = 0 if (ty > self.heightInTiles - self._screenRows): ty = self.heightInTiles - self._screenRows ri = int(ty * self.widthInTiles + tx) self._p.x += tx * self._tileSize self._p.y += ty * self._tileSize opx = self._p.x for r in range(self._screenRows): cri = ri for c in range(self._screenCols): #print self._rects[cri] #raw_input() if (self._rects[cri] != None): im = self._pixels.get_region( self._rects[cri].x, self._rects[cri].y, self._rects[cri].width, self._rects[cri].height ) #.x,self._rects[cri].y, self._rects[cri].width,self._rects[cri].height) #tmp=Sprite(im) #tmp.position=(self._p.x,self._p.y) #print r,c,"tile",self._p.x,self._p.y #self.add(tmp) r = FlxRect(self._p.x, self._p.y, self._rects[cri].width, self._rects[cri].height) im.blit(self._p.x, self._p.y) cri += 1 self._p.x += self._tileSize ri += self.widthInTiles self._p.x = opx self._p.y += self._tileSize
def __init__(self, bounds): self.count = len(bounds) sorted_names = sorted(bounds) hashed_names = map(lambda name: json.dumps(name), sorted_names) assigned_name_indices = list(range(self.count)) self.name_to_vertex_id_map = dict( zip(hashed_names, assigned_name_indices)) self.vertex_id_to_name_map = sorted_names self.vertices_for_values = Math.k_simplex(self.count)
def emit(self): s = self._sprites[self._particle] s.exists = true s.x = self.x - (s.width >> 1) if (self.width != 0): s.x += Math.random() * self.width s.y = self.y - (s.height >> 1) if (self.height != 0): s.y += Math.random() * self.height s.velocity.x = self.minVelocity.x if (self.minVelocity.x != self.maxVelocity.x): s.velocity.x += Math.random() * (self.maxVelocity.x - self.minVelocity.x) s.velocity.y = self.minVelocity.y if (self.minVelocity.y != self.maxVelocity.y): s.velocity.y += Math.random() * (self.maxVelocity.y - self.minVelocity.y) s.acceleration.y = self._gravity s.angularVelocity = self._minRotation if (self._minRotation != self._maxRotation): s.angularVelocity += Math.random() * (self._maxRotation - self._minRotation) if (s.angularVelocity != 0): s.angle = Math.random() * 360 - 180 s.drag.x = self._drag s.drag.y = self._drag self._particle += 1 if (self._particle >= len(self._sprites)): self._particle = 0 s.onEmit()
def emit(self): s = self._sprites[self._particle]; s.exists = true; s.x = self.x - (s.width>>1); if(self.width != 0): s.x += Math.random()*self.width; s.y = self.y - (s.height>>1); if(self.height != 0): s.y += Math.random()*self.height; s.velocity.x = self.minVelocity.x; if(self.minVelocity.x != self.maxVelocity.x): s.velocity.x += Math.random()*(self.maxVelocity.x-self.minVelocity.x); s.velocity.y = self.minVelocity.y; if(self.minVelocity.y != self.maxVelocity.y): s.velocity.y += Math.random()*(self.maxVelocity.y-self.minVelocity.y); s.acceleration.y = self._gravity; s.angularVelocity = self._minRotation; if(self._minRotation != self._maxRotation): s.angularVelocity += Math.random()*(self._maxRotation-self._minRotation); if(s.angularVelocity != 0): s.angle = Math.random()*360-180; s.drag.x = self._drag; s.drag.y = self._drag; self._particle+=1 if(self._particle >= len(self._sprites)): self._particle = 0; s.onEmit();
def update(self): self.last.x=self.x self.last.y=self.y #print "last",self,self.last.x,self.last.y FlxCore.update(self); if(not self.active): return; #animation if((self._curAnim != None) and (self._curAnim.delay > 0) and (self._curAnim.looped or not self.finished)): self._frameTimer += FlxG.elapsed; #print self._frameTimer,self._curAnim.delay,FlxG.elapsed #raw_input() if(self._frameTimer > self._curAnim.delay): self._frameTimer -= self._curAnim.delay; if(self._curFrame == len(self._curAnim.frames)-1): if(self._curAnim.looped): self._curFrame = 0; self.finished = true; else: self._curFrame+=1; self.calcFrame(); #print "curframe",self._curFrame #motion + physics self.angularVelocity = FlxG.computeVelocity(self.angularVelocity,self.angularAacceleration,self.angularDrag,self.maxAngular) self.angle += (self.angularVelocity)*FlxG.elapsed; self.thrustComponents=FlxPoint(); if(self.thrust != 0): self.thrustComponents = FlxG.rotatePoint(-self.thrust,0,0,0,self.angle); maxComponents = FlxG.rotatePoint(-maxself.thrust,0,0,0,self.angle); maxself.velocity.x = Math.abs(maxComponents.x); maxself.velocity.y = Math.abs(maxComponents.y); else: self.thrustComponents = self._pZero; self.velocity.x = FlxG.computeVelocity(self.velocity.x,self.acceleration.x+self.thrustComponents.x,self.drag.x,self.maxVelocity.x) self.x += (self.velocity.x)*FlxG.elapsed; self.velocity.y = FlxG.computeVelocity(self.velocity.y,self.acceleration.y+self.thrustComponents.y,self.drag.y,self.maxVelocity.y) self.y += (self.velocity.y)*FlxG.elapsed;
def render(self): #NOTE: While this will only draw the tiles that are actually on screen, it will ALWAYS draw one screen's worth of tiles FlxCore.render(self) self.getScreenXY(self._p); #print self._p.x,self._p.y #self.position=(self._p.x,self._p.y) #raw_input() tx = Math.floor(-self._p.x/self._tileSize); ty = Math.floor(-self._p.y/self._tileSize); if(tx < 0): tx = 0; if(tx > self.widthInTiles-self._screenCols): tx = self.widthInTiles-self._screenCols; if(ty < 0): ty = 0; if(ty > self.heightInTiles-self._screenRows): ty = self.heightInTiles-self._screenRows; ri =int(ty*self.widthInTiles+tx); self._p.x += tx*self._tileSize; self._p.y += ty*self._tileSize; opx = self._p.x; for r in range(self._screenRows): cri = ri; for c in range(self._screenCols): #print self._rects[cri] #raw_input() if(self._rects[cri] != None): im=self._pixels.get_region(self._rects[cri].x,self._rects[cri].y,self._rects[cri].width,self._rects[cri].height)#.x,self._rects[cri].y, self._rects[cri].width,self._rects[cri].height) #tmp=Sprite(im) #tmp.position=(self._p.x,self._p.y) #print r,c,"tile",self._p.x,self._p.y #self.add(tmp) r=FlxRect(self._p.x,self._p.y,self._rects[cri].width,self._rects[cri].height) im.blit(self._p.x,self._p.y); cri+=1; self._p.x += self._tileSize; ri += self.widthInTiles; self._p.x = opx; self._p.y += self._tileSize;
class Renderer: def __init__(self, size, phisic_map): pygame.init() self.delta_x, self.delta_y = size[0] / 2, size[1] / 2 self.size = size self.screen = pygame.display.set_mode(size, flags=pygame.DOUBLEBUF) self.map = phisic_map self.cam_pos = (-2, 0, 0) self.cam_angles = (0, 0, 0) self.local_cam_angles = (0, 0, 0) self.math = Math() self.pygame = pygame self.scale = 500 def calc_pre_render(self): plots, polygons = self.map[0] #plots, polygons = list, list polygons = polygons.copy() plots = plots.copy() for object in self.map[1:]: for struct in object.get_render_data(): obj_plots, obj_polygons = struct delta = len(plots) plots.extend(obj_plots) polygons.extend([ self.math.m_sum(i, (delta, delta, delta)) for i in obj_polygons ]) return plots, polygons def render(self, pre_render): pygame.draw.rect(self.screen, (0, 0, 0), (0, 0, self.size[0], self.size[1])) plots, polygons = pre_render rot_data = self.math.rotate_data(self.cam_angles) plots = self.math.rotate_plots(plots, rot_data, (0, 0, 0), self.cam_pos) ban = [i for i, j in enumerate(plots) if j[0] < 0] pixels = [(plot[1] / plot[0] * self.scale + self.delta_x, plot[2] / plot[0] * self.scale + self.delta_y) if (plot[0] > 0) else (0) for plot in plots] for poly in polygons: a, b, c = poly if a not in ban and b not in ban and c not in ban: pygame.draw.polygon(self.screen, (255, 255, 255), (pixels[a], pixels[b], pixels[c]), 1) def cam_place(self, coords): self.cam_pos = coords def cam_rotate(self, angles): self.local_cam_angles = self.math.m_sum(self.local_cam_angles, angles) a, b, c = self.local_cam_angles self.cam_angles = (self.math.cosd(c) * b, self.math.sind(c) * b, c)
def establish_session(self): generator, prime = Math.generate_generator_and_prime(256) self.__tcp_client.send(bytes(str(generator), 'utf8')) self.__tcp_client.send(bytes(str(prime), 'utf8')) diffie_hellman = DiffieHellman(generator, prime) result = diffie_hellman.get_result() self.__tcp_client.send(bytes(str(result), 'utf8')) result_from_server = int(self.__tcp_client.recv(1024)) self.__key = diffie_hellman.calculate_shared_secret(result_from_server) print("Session key: " + str(self.__key)) self.__key = self.__key.to_bytes(32, byteorder="big") self.__aes = pyaes.AESModeOfOperationCTR(self.__key)
def test_diffie_hellman_exchange(self): """ Simulates a diffie-hellman key exchange """ # Alice and Bob agree for a secure generator and prime base, modulus = Math.generate_generator_and_prime(128) alice = DiffieHellman(base, modulus) bob = DiffieHellman(base, modulus) # Alice calculates her exponentiation alice_result = alice.get_result() # Bob calculates his exponentiation bob_result = bob.get_result() # so, they exchange the result and calculates the shared secret secret_alice = alice.calculate_shared_secret(bob_result) secret_bob = bob.calculate_shared_secret(alice_result) # And if right, both secrets must be equal. self.assertEqual(secret_alice, secret_bob)
def old_collide(self, Spr): #print "map collide"#Spr.velocity.x ix = int(Math.floor((Spr.x - self.x) / self._tileSize)) iy = int(Math.floor((Spr.y - self.y) / self._tileSize)) for r in [iy - 1, iy, iy + 1]: if ((r < 0) or (r >= self.heightInTiles)): continue for c in [ix - 1, ix, ix + 1]: if ((c < 0) or (c >= self.widthInTiles)): continue at = (r) * self.widthInTiles + c #print "at=",at if (self._data[at] >= self._ci): self._block.x = self.x + c * self._tileSize self._block.y = self.y + r * self._tileSize self._block.collide(Spr) #print at,self._block.x,self._block.y return #old ix = int(Math.floor((Spr.x - self.x) / self._tileSize)) iy = int(Math.floor((Spr.y - self.y) / self._tileSize)) iw = int(Math.ceil(float(Spr.width) / self._tileSize) + 1) ih = int(Math.ceil(float(Spr.height) / self._tileSize) + 1) print "map collide", ih, iw print Spr.width, self._tileSize, Spr.x, Spr.y for r in range(ih): if ((r < 0) or (r >= self.heightInTiles)): continue for c in range(iw): if ((c < 0) or (c >= self.widthInTiles)): continue at = (iy + r) * self.widthInTiles + ix + c print "at=", at, if (at < len(self._data) and self._data[at] >= self._ci): self._block.x = self.x + (ix + c) * self._tileSize self._block.y = self.y + (iy + r) * self._tileSize self._block.collide(Spr)
def old_collide(self,Spr): #print "map collide"#Spr.velocity.x ix =int(Math.floor((Spr.x - self.x)/self._tileSize)) iy =int(Math.floor((Spr.y - self.y)/self._tileSize)) for r in [iy-1,iy,iy+1]: if((r < 0) or (r >= self.heightInTiles)): continue; for c in [ix-1,ix,ix+1]: if((c < 0) or (c >= self.widthInTiles)): continue; at=(r)*self.widthInTiles+c #print "at=",at if( self._data[at] >= self._ci): self._block.x = self.x+c*self._tileSize; self._block.y = self.y+r*self._tileSize; self._block.collide(Spr); #print at,self._block.x,self._block.y return #old ix =int(Math.floor((Spr.x - self.x)/self._tileSize)) iy =int(Math.floor((Spr.y - self.y)/self._tileSize)) iw =int( Math.ceil(float(Spr.width)/self._tileSize)+1) ih =int(Math.ceil(float(Spr.height)/self._tileSize)+1) print "map collide",ih,iw print Spr.width,self._tileSize,Spr.x,Spr.y for r in range( ih): if((r < 0) or (r >= self.heightInTiles)): continue; for c in range(iw): if((c < 0) or (c >= self.widthInTiles)): continue; at=(iy+r)*self.widthInTiles+ix+c print "at=",at, if(at<len(self._data) and self._data[at] >= self._ci): self._block.x = self.x+(ix+c)*self._tileSize; self._block.y = self.y+(iy+r)*self._tileSize; self._block.collide(Spr);
def randomFrame(self): self._pixels=self.pixels.get_region(Math.floor(Math.random()*(self.pixels.width/self._bw))*self._bw,0,self._bw,self._bh)
def getScreenXY(self,P): P.x = Math.floor(self.x-self.offset.x)+Math.floor(FlxG.scroll.x*self.scrollFactor.x); P.y = Math.floor(self.y-self.offset.y)+Math.floor(FlxG.scroll.y*self.scrollFactor.y);
def getScreenXY(self,p): #print "getscreenXY",self.x,FlxG.scroll.x,self.scrollFactor.x p.x = Math.floor(self.x)+Math.floor(FlxG.scroll.x*self.scrollFactor.x); p.y = Math.floor(self.y)+Math.floor(FlxG.scroll.y*self.scrollFactor.y);
def old_collide(self,Spr): #x y not chongdie #print "=",self.x,self.y,self.width,self.height,"collide",Spr.x,Spr.y,Spr.width,Spr.height if((Math.abs(Spr.x + (Spr.width>>1) - self.x - (self.width>>1)) > (self.width>>1) + (Spr.width>>1)) and (Math.abs(Spr.y + (Spr.height>>1) - self.y - (self.height>>1)) > (self.height>>1) + (Spr.height>>1))): return; #print FlxG.elapsed,":",self.x,self.y,self.width,self.height,"collide",Spr.x,Spr.y,Spr.width,Spr.height yFirst= true; if((Math.abs(Spr.velocity.x) > Math.abs(Spr.velocity.y))): yFirst = false; #print "yfirst=false",Spr.y else: #print "yfirst=true",Spr.y pass checkForMoreX = false; checkForMoreY = false; if(yFirst): if(Spr.velocity.y > 0): #print "velocity.y>0" if(self.overlapsPoint(Spr.x + (Spr.width>>1),Spr.y + Spr.height)): if(Spr.hitFloor()): Spr.y = self.y - Spr.height; else: checkForMoreY = true; elif(Spr.velocity.y < 0): #print "velocity.y<0" if(self.overlapsPoint(Spr.x + (Spr.width>>1),Spr.y)): if(Spr.hitCeiling()): Spr.y = self.y + self.height; else: checkForMoreY = true; if(Spr.velocity.x < 0): #print "velocity.x<0",self.x,self.y,self.width,self.height,Spr.x,Spr.y,Spr.width,Spr.height if(self.overlapsPoint(Spr.x,Spr.y + (Spr.height>>1))): if(Spr.hitWall()): #print "hitwall" Spr.x = self.x + self.width; else: #print "check more x" checkForMoreX = true; elif(Spr.velocity.x > 0): #print "velocity.x>0" if(self.overlapsPoint(Spr.x + Spr.width,Spr.y + (Spr.height>>1))): if(Spr.hitWall()): Spr.x = self.x - Spr.width; else: checkForMoreX = true; else: if(Spr.velocity.x < 0): if(self.overlapsPoint(Spr.x,Spr.y + (Spr.height>>1))): if(Spr.hitWall()): Spr.x = self.x + self.width; else: checkForMoreX = true; elif(Spr.velocity.x > 0): if(self.overlapsPoint(Spr.x + Spr.width,Spr.y + (Spr.height>>1))): if(Spr.hitWall()): Spr.x = self.x - Spr.width; else: checkForMoreX = true; if(Spr.velocity.y > 0): if(self.overlapsPoint(Spr.x + (Spr.width>>1),Spr.y + Spr.height)): if(Spr.hitFloor()): Spr.y = self.y - Spr.height; else: checkForMoreY = true; elif(Spr.velocity.y < 0): if(self.overlapsPoint(Spr.x + (Spr.width>>1),Spr.y)): if(Spr.hitCeiling()): Spr.y = self.y + self.height; else: checkForMoreY = true; if( not checkForMoreY and not checkForMoreX): return; bias = Spr.width>>3; if(bias < 1): bias = 1; if(checkForMoreY and checkForMoreX): #print "check more y and x",Spr.y if(yFirst): if(checkForMoreY): if((Spr.x + Spr.width - bias > self.x) and (Spr.x + bias < self.x + self.width)): if((Spr.velocity.y > 0) and (Spr.y + Spr.height > self.y) and (Spr.y + Spr.height < self.y + self.height) and Spr.hitFloor()): Spr.y = self.y - Spr.height; elif((Spr.velocity.y < 0) and (Spr.y > self.y) and (Spr.y < self.y + self.height) and Spr.hitCeiling()): Spr.y = self.y + self.height; if(checkForMoreX): if((Spr.y + Spr.height - bias > self.y) and (Spr.y + bias < self.y + self.height)): if((Spr.velocity.x > 0) and (Spr.x + Spr.width > self.x) and (Spr.x + Spr.width < self.x + self.width) and Spr.hitWall()): Spr.x = self.x - Spr.width; elif((Spr.velocity.x < 0) and (Spr.x > self.x) and (Spr.x < self.x + self.width) and Spr.hitWall()): Spr.x = self.x + self.width; else: if(checkForMoreX): if((Spr.y + Spr.height - bias > self.y) and (Spr.y + bias < self.y + self.height)): if((Spr.velocity.x > 0) and (Spr.x + Spr.width > self.x) and (Spr.x + Spr.width < self.x + self.width) and Spr.hitWall()): Spr.x = self.x - Spr.width; elif((Spr.velocity.x < 0) and (Spr.x > self.x) and (Spr.x < self.x + self.width) and Spr.hitWall()): Spr.x = self.x + self.width; if(checkForMoreY): if((Spr.x + Spr.width - bias > self.x) and (Spr.x + bias < self.x + self.width)): if((Spr.velocity.y > 0) and (Spr.y + Spr.height > self.y) and (Spr.y + Spr.height < self.y + self.height) and Spr.hitFloor()): Spr.y = self.y - Spr.height; elif((Spr.velocity.y < 0) and (Spr.y > self.y) and (Spr.y < self.y + self.height) and Spr.hitCeiling()): Spr.y = self.y + self.height; elif(checkForMoreY): #print "check more y",Spr.y if((Spr.x + Spr.width - bias > self.x) and (Spr.x + bias < self.x + self.width)): if((Spr.velocity.y > 0) and (Spr.y + Spr.height > self.y) and (Spr.y + Spr.height < self.y + self.height) and Spr.hitFloor()): Spr.y = self.y - Spr.height; elif((Spr.velocity.y < 0) and (Spr.y > self.y) and (Spr.y < self.y + self.height) and Spr.hitCeiling()): Spr.y = self.y + self.height; elif(checkForMoreX): if((Spr.y + Spr.height - bias > self.y) and (Spr.y + bias < self.y + self.height)): if((Spr.velocity.x > 0) and (Spr.x + Spr.width > self.x) and (Spr.x + Spr.width < self.x + self.width) and Spr.hitWall()): Spr.x = self.x - Spr.width; elif((Spr.velocity.x < 0) and (Spr.x > self.x) and (Spr.x < self.x + self.width) and Spr.hitWall()): Spr.x = self.x + self.width;
def test_sum(self): self.assertEqual(Math.sum(2,2),4)
def test_div(self): self.assertEqual(Math.div(4,2),2)
def test_divide(): assert Math.divide(100, 2) == 50
def test_subtract(): assert Math.subtract(10, 10) == 0
def overlapsWithCallback(self, Object, Callback=None, FlipCallbackParams=false, Position=None): results = false X = self.x Y = self.y if (Position != None): X = Position.x Y = Position.y #Figure out what tiles we need to check against selectionX = Math.floor((Object.x - X) / self._tileSize) selectionY = Math.floor((Object.y - Y) / self._tileSize) selectionWidth = selectionX + (Math.ceil( Object.width / self._tileSize)) + 1 selectionHeight = selectionY + Math.ceil( Object.height / self._tileSize) + 1 #Then bound these coordinates by the map edges if (selectionX < 0): selectionX = 0 if (selectionY < 0): selectionY = 0 if (selectionWidth > self.widthInTiles): selectionWidth = self.widthInTiles if (selectionHeight > self.heightInTiles): selectionHeight = self.heightInTiles #Then loop through this selection of tiles and call FlxObject.separate() accordingly rowStart = selectionY * self.widthInTiles row = selectionY overlapFound = false deltaX = X - self.last.x deltaY = Y - self.last.y while (row < selectionHeight): column = selectionX while (column < selectionWidth): #print "row,column",row,column overlapFound = false tile = self._tileObjects[self._data[int(rowStart + column)]] if (tile.allowCollisions): tile.x = X + column * self._tileSize tile.y = Y + row * self._tileSize tile.last.x = tile.x - deltaX tile.last.y = tile.y - deltaY if (Callback != None): if (FlipCallbackParams): overlapFound = Callback(Object, tile) else: overlapFound = Callback(tile, Object) else: overlapFound = (Object.x + Object.width > tile.x) and ( Object.x < tile.x + tile.width) and ( Object.y + Object.height > tile.y) and (Object.y < tile.y + tile.height) if (overlapFound): if ((tile.callback != None) and ((tile.filter == None) or (Object is tile.filter))): tile.mapIndex = rowStart + column tile.callback(tile, Object) results = true elif ((tile.callback != None) and ((tile.filter == None) or (Object is tile.filter))): tile.mapIndex = rowStart + column tile.callback(tile, Object) column += 1 rowStart += self.widthInTiles row += 1 return results
def calculator(): m = Math() res = m.input() print("The result: " + res)
def test_div_return_zero(self): x=Math() self.assertEqual(x.div(0,4),0)
def test_div_zero_zero(self): x=Math() self.assertEqual(x.div(0,0),Math.UNDEFINED)
def test_div_zero(self): x=Math() self.assertEqual(x.div(4,0),Math.INFINITE)
def test_add(): assert Math.add(10, 4) == 14
def __set_light(self): self._light = Math.vector_trans(self._s)+Math.dot(self._t, self._v)
def test_multiply(): assert Math.multiply(10, 3) == 30
def test_KSimplex(self): kSimplex1000 = Math.k_simplex(500) # assert that all distances between the points in the simplex are almost exactly 1 self.assertTrue(np.all(np.abs(pdist(kSimplex1000) - 1) < .000001))