def draw_triangle(self, screen, position, size, angle=0, color=(255, 255, 255), chromatic_aberration=False): a = size * rotate(np.array([0, 0.5]), angle) b = size * rotate(np.array([0, -0.5]), angle) c = size * rotate(np.array([np.sqrt(3) / 2, 0]), angle) if chromatic_aberration: offset = 0.05 * size * basis(0) points = [ self.world_to_screen(-self.zoom / 100 * p + position - offset) for p in [a, b, c] ] pygame.draw.polygon(screen, (255, 0, 0), points) points = [ self.world_to_screen(-self.zoom / 100 * p + position + offset) for p in [a, b, c] ] pygame.draw.polygon(screen, (0, 255, 255), points) points = [ self.world_to_screen(-self.zoom / 100 * p + position) for p in [a, b, c] ] pygame.draw.polygon(screen, color, points)
def shore_tile(island, tileset, row, col): island_width = len(island[0])-1 island_height = len(island)-1 if row is 0 or row is not 0 and island[row-1][col] is 0: if col is 0 or col is not 0 and island[row][col-1] is 0: return get_tile(tileset['corner']) elif col is island_width or col is not island_width and island[row][col+1] is 0: return rotate(get_tile(tileset['corner']),90) else: return get_tile(tileset['side']) if row is island_height or row is not island_height and island[row+1][col] is 0: if col is 0 or col is not 0 and island[row][col-1] is 0: return rotate(get_tile(tileset['corner']),270) elif col is island_width or col is not island_width and island[row][col+1] is 0: return rotate(get_tile(tileset['corner']),180) else: return rotate(get_tile(tileset['side']),180) if col is 0 or col is not 0 and island[row][col-1] is 0: return rotate(get_tile(tileset['side']), 270) if col is island_width or col is not island_width and island[row][col+1] is 0: return rotate(get_tile(tileset['side']),90) else: return rotate(get_tile(tileset['inland']), rand(4)*90)
def upload(thing_id): print "thing id:" + thing_id # Get the name of the uploaded file file = request.files['file'] print("upload request ....") print file print file.filename # Check if the file is one of the allowed types/extensions if file and allowed_file(file.filename): filename = thing_id + '.jpg' file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) img = cv2.imread( os.path.join(app.config['UPLOAD_FOLDER']) + '/' + filename) img = helpers.rotate(img, 270) _, _, response = helpers.detectTriangles(img) global live_image live_image = img mqtt.publish(thing_id + '/sensors/camera', payload=json.dumps(response)) return jsonify(response) return "bad file"
def draw(self, screen, camera, image_handler): self.image_path = f'body_{self.parent.body_type}' super().draw(screen, camera, image_handler) image = image_handler.images['blood'] for b in self.blood: camera.draw_image(screen, image, self.position + rotate(b[0], self.angle), 1, self.direction, self.angle + b[1])
def rotate(self, angle): for i in range(len(self.xs)): v = np.array([self.xs[i], self.ys[i]]) v = rotate(v, angle) self.xs[i] = v[0] self.ys[i] = v[1] self.angles[i] += angle self.angle += angle
def draw(self, screen, camera, image_handler): if not self.image_path: self.debug_draw(screen, camera, image_handler) return image = image_handler.images[self.image_path] pos = self.position + rotate(self.image_position, self.angle) camera.draw_image(screen, image, pos, self.size, self.direction, self.angle)
def gen(show, size, dir, axis, basis): for point in basis.sites: np.multiply(point, size, out=point) original = np.array(basis.sites) cube = [] lattice = [size, size, size] im = np.array(axis) ix = np.array(dir) R = helpers.rotate(ix, im) file = open('basis.input', 'w') file.write( str(lattice[0]) + ' ' + str(lattice[1]) + ' ' + str(lattice[2]) + '\n') file.write(str(dir[0]) + ' ' + str(dir[1]) + ' ' + str(dir[2]) + '\n') file.write(str(axis[0]) + ' ' + str(axis[1]) + ' ' + str(axis[2]) + '\n') for i in range(len(original)): v = original[i] p = np.asarray(np.asarray((np.matmul(R, v))[0])[0]) cube.append(p) if show: fig = plt.figure() if show: ax = fig.add_subplot(111, projection='3d') for p in original: if show: ax.scatter(p[0], p[1], p[2], c='g') n = 0 for p in cube: if show: ax.scatter(p[0], p[1], p[2], c='r') file.write( str(p[0]) + '\t' + str(p[1]) + '\t' + str(p[2]) + '\t' + str(basis.atoms[n]) + '\n') n = n + 1 file.close() if show: ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') ax.set_xlim(-size, size) ax.set_ylim(-size, size) ax.set_zlim(-size, size) plt.show()
def getMutations(n: int, placed: Tuple[int, ...]) -> List[Tuple[int, ...]]: """ Simple approach to identify all mutations of a combination :return: all mutations of the current placement by rotation and mirroring. """ ret: Set[Tuple[int, ...]] = set() # make sure we take every element only once. ret.add(placed) ret.add(mirror(n, placed)) # rotate 3 times and mirror always last = placed for i in range(3): last = rotate(n, last) ret.add(last) ret.add(mirror(n, last)) return list(ret)
def animate(self, time_step): anim = self.animations[self.animation] self.image_path = anim.image_path pos, angle = anim.update(self.animation_direction * time_step) pos[0] *= self.direction pos = rotate(pos, self.animation_angle) self.relative_position = pos self.set_position(self.position + pos) self.angle = self.direction * angle + self.animation_angle if not self.loop and anim.time >= anim.times[-1]: anim.time = anim.times[-1]
def addRef(self, direction): #Normalize the direction. ss = math.sqrt(direction[0]**2 + direction[1]**2 + direction[2]**2) direction[0] /= ss direction[1] /= ss direction[2] /= ss print(direction) im = np.array(self.axis) #im = np.array(direction) ix = np.array(self.dir) #ix = np.array(direction) R = helpers.rotate(ix, im) #R_inv = R#np.linalg.inv(R) R_inv = np.linalg.inv(R) ex = np.asarray(np.matmul(R_inv, np.array([1, 0, 0])))[0] ey = np.asarray(np.matmul(R_inv, np.array([0, 1, 0])))[0] ez = np.asarray(np.matmul(R_inv, np.array([0, 0, 1])))[0] #direction = self.axis #Convert the direction to the local coordinate ststem tmp = [0, 0, 0] tmp[0] = ex[0] * direction[0] + ex[1] * direction[1] + ex[ 2] * direction[2] tmp[1] = ey[0] * direction[0] + ey[1] * direction[1] + ey[ 2] * direction[2] tmp[2] = ez[0] * direction[0] + ez[1] * direction[1] + ez[ 2] * direction[2] for i in range(10): p = [0, 0, 0] p[0] = p[0] + tmp[0] * i / 1.0 p[1] = p[1] + tmp[1] * i / 1.0 p[2] = p[2] + tmp[2] * i / 1.0 self.points.addPoint(p)
def draw_shadow(self, screen, camera, image_handler, light): r = self.position - light pos = self.position + 0.5 * r / norm(r) + rotate(self.image_position, self.angle) image = image_handler.images[f'shadow_{self.image_path}'] camera.draw_image(screen, image, pos, self.size, self.direction, self.angle)
def rotate(self, delta_angle): super().rotate(delta_angle) if self.collider: self.collider.rotate(delta_angle) r = self.collider.position - self.position self.collider.position = self.position + rotate(r, delta_angle)
def gen(_size, _dir, _axis, _basis, _n, zTop, zBottom): basisgen.gen(False, _size, _dir, _axis, _basis) file = open('basis.input', 'r') onlyTop = False if onlyTop: zBottom = -2.5*_size n = 0; # Repeat distance for crystal lattice = [0,0,0] dir = [0,0,1] axis = [0,0,1] # List of basis coordinates basis = [] # Lists of crystal coordinates to export crystal = [] for line in file: arr = line.split() if n==0 : lattice[0] = float(arr[0].replace('D0','').replace('d0','')) lattice[1] = float(arr[1].replace('D0','').replace('d0','')) lattice[2] = float(arr[2].replace('D0','').replace('d0','')) n = n + 1 continue if n==1 : dir[0] = int(arr[0].replace('D0','').replace('d0','')) dir[1] = int(arr[1].replace('D0','').replace('d0','')) dir[2] = int(arr[2].replace('D0','').replace('d0','')) n = n + 1 continue if n==2 : axis[0] = int(arr[0].replace('D0','').replace('d0','')) axis[1] = int(arr[1].replace('D0','').replace('d0','')) axis[2] = int(arr[2].replace('D0','').replace('d0','')) n = n + 1 continue basis.append([float(arr[0]),float(arr[1]),float(arr[2])]) n = n + 1 file.close() im = np.array(axis) ix = np.array(dir) R = helpers.rotate(ix, im) R_inv = np.linalg.inv(R) ex = np.asarray(np.matmul(R_inv, np.array([1,0,0])))[0]*lattice[0] ey = np.asarray(np.matmul(R_inv, np.array([0,1,0])))[0]*lattice[1] ez = np.asarray(np.matmul(R_inv, np.array([0,0,1])))[0]*lattice[2] maxZ = -1e20 maxZI = 0 minZ = 1e20 minZI = 0 for i in range(len(basis)): if basis[i][2]>maxZ: maxZI = i maxZ = basis[i][2] if basis[i][2]<minZ: minZI = i minZ = basis[i][2] n = 0 m = 0 diff = [0,0,0] ns = -_n ne = _n for x in range(ns, ne): for y in range(ns, ne): for z in range(ns, ne): diff[0] = (ex[0] * x + ex[1] * y + ex[2] * z) diff[1] = (ey[0] * x + ey[1] * y + ey[2] * z) diff[2] = (ez[0] * x + ez[1] * y + ez[2] * z) # Check if entire cell fits if (diff[2] + basis[maxZI][2]) <= zTop: for i in range(len(basis)): if onlyTop and i!=maxZI: continue px = diff[0] + basis[i][0] py = diff[1] + basis[i][1] pz = diff[2] + basis[i][2] if pz < zBottom: continue site = [px,py,pz,_basis.atoms[i]] if not site in crystal: crystal.append(site) print('Generated Points, Now Clearing Duplicates') n = len(crystal) print(str(n)+ ' Points to check') clearDuplicates(crystal) print('Duplicates Cleared') output = open('crystal.input', 'w') for site in crystal: output.write(str(site[0])+'\t'+str(site[1])+ '\t'+ str(site[2]) \ +'\t'+str(site[3])+'\n') output.close() return crystal
def tiles(island, tileset): for row in range(len(island)): for col in range(len(island[0])): if island[row][col] == 1: island[row][col] = rotate(get_tile(tileset['mainland']), rand(4)*90) elif island[row][col] == 2: island[row][col] = shore_tile(island, tileset, row, col)