我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用math.atan()。
def pixel_to_lonlat(self, px, zoom): "Converts a pixel to a longitude, latitude pair at the given zoom level." if len(px) != 2: raise TypeError('Pixel should be a sequence of two elements.') # Getting the number of pixels for the given zoom level. npix = self._npix[zoom] # Calculating the longitude value, using the degrees per pixel. lon = (px[0] - npix) / self._degpp[zoom] # Calculating the latitude value. lat = RTOD * (2 * atan(exp((px[1] - npix) / (-1.0 * self._radpp[zoom]))) - 0.5 * pi) # Returning the longitude, latitude coordinate pair. return (lon, lat)
def evaluate_constraints(self, solution: FloatSolution) -> None: constraints : [float] = [0.0 for x in range(self.number_of_constraints)] x1 = solution.variables[0] x2 = solution.variables[1] constraints[0] = (x1 * x1 + x2 * x2 - 1.0 - 0.1 * cos(16.0 * atan(x1 / x2))) constraints[1] = -2.0 * ((x1 - 0.5) * (x1 - 0.5) + (x2 - 0.5) * (x2 - 0.5) - 0.5) overall_constraint_violation = 0.0 number_of_violated_constraints = 0.0 for constrain in constraints: if constrain < 0.0: overall_constraint_violation += constrain number_of_violated_constraints += 1 solution.attributes["overall_constraint_violation"] = overall_constraint_violation solution.attributes["number_of_violated_constraints"] = number_of_violated_constraints
def xyz_to_spr(x, y, z): """Convierte las coordenadas cartesianas (x,y,z) a las coordenadas esfericas (r,phi,theta) con angulos en grados""" # Calculo el radio r = math.sqrt(x ** 2 + y ** 2 + z ** 2) # Calculo el angulo theta if z > 0: theta = math.atan(math.sqrt(x ** 2 + y ** 2) / z) elif z == 0: theta = math.pi / 2 else: theta = math.pi + math.atan(math.sqrt(x ** 2 + y ** 2) / z) # Calculo el angulo phi if x > 0: if y > 0: phi = math.atan(y / x) else: phi = 2 * math.pi + math.atan(y / x) elif x == 0: phi = sgn(y) * math.pi / 2 else: phi = math.pi + math.atan(y / x) theta = math.degrees(theta) phi = math.degrees(phi) % 360 theta = min(max(theta, 0.000001), 180) return r, phi, theta
def get_angle(line1, line2): """ Calculates the angle between two lines (in Degrees) using their parsed tuples each containing slope & y-intercept """ (m1, c1) = line1 (m2, c2) = line2 denominator = 1.0 + m1 * m2 # If this part of the expression results to zero # then it implies the angle between the lines is 90 degrees. if denominator == 0: return 90.0 angle_radian = math.atan((m1 - m2) / denominator) return angle_radian * (180 / math.pi)
def angle(m1, m2): if isPerpendicular(m1, m2) == True: return 90 elif m1 == "undefined" or m2 == "undefined": if m1 == "undefined" and m2 == "undefined": return 0 elif m1 == "undefined": tanAngle = abs(m2) angleRadians = math.atan(tanAngle) angleDegrees = angleRadians * (180/pi) return angleDegrees else: tanAngle = abs(m1) angleRadians = math.atan(tanAngle) angleDegrees = angleRadians * (180/pi) return angleDegrees else: tanAngle = abs((m1 - m2) / (1 + m1 * m2)) angleRadians = math.atan(tanAngle) angleDegrees = angleRadians * (180/pi) return angleDegrees #Helper function that returns whether two lines are perpendicular or not (True or False)
def usphericalise(self, x, y, z): if y == 0.0: if x > 0: theta = 0.0 else: theta = self.a180 elif x == 0.0: if y > 0: theta = self.a90 else: theta = self.a270 else: theta = atan(y / x) if x < 0.0 and y < 0.0: theta = theta + self.a180 elif x < 0.0 and y > 0.0: theta = theta + self.a180 u = theta return u
def ellipsecomp(self, efactor, theta): if theta == self.a90: result = self.a90 elif theta == self.a270: result = self.a270 else: result = atan(tan(theta) / efactor**0.5) if result >= 0.0: x = result y = self.a180 + result if fabs(x - theta) <= fabs(y - theta): result = x else: result = y else: x = self.a180 + result y = result if fabs(x - theta) <= fabs(y - theta): result = x else: result = y return result
def get_sph_cor(x,y,z): #using radian lon=0 if(x==0 and y>0): lon=PI/2 elif(x==0 and y<0): lon=3*PI/2 elif(x==0 and y==0): print ("error") return elif(x>0 and y==0): lon=0 elif(x>0 and y>0): lon=math.atan(float(y)/float(x)) elif(x>0 and y<0): lon=2*PI+math.atan(float(y)/float(x)) elif(x<0 and y==0): lon=PI elif(x<0 and y>0): lon=PI+math.atan(float(y)/float(x)) elif(x<0 and y<0): lon=PI+math.atan(float(y)/float(x)) lat=PI/2-math.acos(z/1.0) return lon,lat
def test_atan(): mp.dps = 15 assert atan(-2.3).ae(math.atan(-2.3)) assert atan(1e-50) == 1e-50 assert atan(1e50).ae(pi/2) assert atan(-1e-50) == -1e-50 assert atan(-1e50).ae(-pi/2) assert atan(10**1000).ae(pi/2) for dps in [25, 70, 100, 300, 1000]: mp.dps = dps assert (4*atan(1)).ae(pi) mp.dps = 15 pi2 = pi/2 assert atan(mpc(inf,-1)).ae(pi2) assert atan(mpc(inf,0)).ae(pi2) assert atan(mpc(inf,1)).ae(pi2) assert atan(mpc(1,inf)).ae(pi2) assert atan(mpc(0,inf)).ae(pi2) assert atan(mpc(-1,inf)).ae(-pi2) assert atan(mpc(-inf,1)).ae(-pi2) assert atan(mpc(-inf,0)).ae(-pi2) assert atan(mpc(-inf,-1)).ae(-pi2) assert atan(mpc(-1,-inf)).ae(-pi2) assert atan(mpc(0,-inf)).ae(-pi2) assert atan(mpc(1,-inf)).ae(pi2)
def atan_newton(x, prec): if prec >= 100: r = math.atan(int((x>>(prec-53)))/2.0**53) else: r = math.atan(int(x)/2.0**prec) prevp = 50 r = MPZ(int(r * 2.0**53) >> (53-prevp)) extra_p = 50 for wp in giant_steps(prevp, prec): wp += extra_p r = r << (wp-prevp) cos, sin = cos_sin_fixed(r, wp) tan = (sin << wp) // cos a = ((tan-rshift(x, prec-wp)) << wp) // ((MPZ_ONE<<wp) + ((tan**2)>>wp)) r = r - a prevp = wp return rshift(r, prevp-prec)
def shiftPointOnLine(self, x1, y1, x2, y2, distance): if x2 - x1 == 0: # vertical line x_T1 = x1 y_T1 = y1 - distance else: a = (y2 - y1) / (x2 - x1) if a == 0: # horizontal line x_T1 = x1 - distance y_T1 = y1 else: alfa = atan(a) #alfa = tan(a) x_T1 = x1 - distance * cos(alfa) y_T1 = y1 - distance * sin(alfa) return [x_T1, y_T1]
def angle(center, point): """Return the angle (radian) of point from center of the radian circle. ------p | / | / c|a/ """ dx = point.x - center.x dy = point.y - center.y if dx == 0: if dy < 0: return pi * 3 / 2 return pi / 2 if dy == 0: if dx < 0: return pi return 0 if dx < 0: return pi + atan(dy / dx) if dy < 0: return 2 * pi + atan(dy / dx) return atan(dy / dx)
def q(landmarks,index1,index2): #get angle between a i1 and i2 x1 = landmarks[int(index1)][0] y1 = landmarks[int(index1)][1] x2 = landmarks[int(index2)][0] y2 = landmarks[int(index2)][1] x_diff = float(x1 - x2) if (y1 == y2): y_diff = 0.1 if (y1 < y2): y_diff = float(np.absolute(y1 - y2)) if (y1 > y2): y_diff = 0.1 print("Error: Facial feature located below chin.") return np.absolute(math.atan(x_diff/y_diff)) #image_dir should contain sub-folders containing the images where features need to be extracted #only one face should be present in each image #if multiple faces are detected by OpenCV, image must be manually edited; the parameters of the face-detection routine can also be changed
def conferenceWakeOverlap_bk(X, Y, R): from math import atan, tan, cos n = np.size(X) # theta = np.zeros((n, n), dtype=np.float) # angle of wake from fulcrum f_theta = np.zeros((n, n), dtype=np.float) # smoothing values for smoothing for i in range(0, n): for j in range(0, n): if X[i] < X[j]: z = R/tan(0.34906585) theta = atan(Y[j] - Y[i]) / (X[j] - X[i] + z) if -0.34906585 < theta < 0.34906585: f_theta[i][j] = (1 + cos(9*theta))/2 # print f_theta return f_theta
def quaterion2Euler(q): #Compute estimated rotation matrix elements R11 = 2.*q[0]**2-1 + 2.*q[1]**2 R21 = 2.*(q[1]*q[2] - q[0]*q[3]) R31 = 2.*(q[1]*q[3] + q[0]*q[2]) R32 = 2.*(q[2]*q[3] - q[0]*q[1]) R33 = 2.*q[0]**2 - 1 + 2*q[3]**2 phi = math.atan2(R32, R33 )*180/math.pi # Roll theta = -math.atan(R31 / math.sqrt(1-R31**2) )*180/math.pi # Pitch psi = math.atan2(R21, R11 )*180/math.pi # Yaw return [phi,theta,psi] #Define body frame later
def update_cue(self, game_state, initial_mouse_dist, events): # updates cue position current_mouse_pos = events["mouse_pos"] displacement_from_ball_to_mouse = self.target_ball.ball.pos - current_mouse_pos self.update_cue_displacement(current_mouse_pos, initial_mouse_dist) prev_angle = self.angle # hack to avoid div by zero if not displacement_from_ball_to_mouse[0] == 0: self.angle = 0.5 * math.pi - math.atan( displacement_from_ball_to_mouse[1] / displacement_from_ball_to_mouse[0]) if displacement_from_ball_to_mouse[0] > 0: self.angle -= math.pi game_state.redraw_all(update=False) self.draw_lines(game_state, self.target_ball, prev_angle + math.pi, config.table_color) self.draw_lines(game_state, self.target_ball, self.angle + math.pi, (255, 255, 255)) pygame.display.flip()
def angle(vector1, vector2): """Return the angle between vector1 and vector2 in radians [0, pi].""" u1 = vector1.normalize() u2 = vector2.normalize() cosine = u1 * u2 if cosine == 1.0: return 0 elif cosine == -1.0: return math.pi elif cosine == 0.0: return math.pi/2 # Unit vectors: sine == sqrt(1.0 ** 2 - cosine ** 2) if angle within [0, pi] tangent = math.sqrt(1.0 - cosine * cosine) / cosine return math.atan(tangent)
def turn_xyz_into_llh(x,y,z,system): """Convert 3D Cartesian x,y,z into Lat, Long and Height See http://www.ordnancesurvey.co.uk/gps/docs/convertingcoordinates3D.pdf""" a = abe_values[system][0] b = abe_values[system][1] e2 = abe_values[system][2] p = math.sqrt(x*x + y*y) long = math.atan(y/x) lat_init = math.atan( z / (p * (1.0 - e2)) ) v = a / math.sqrt( 1.0 - e2 * (math.sin(lat_init) * math.sin(lat_init)) ) lat = math.atan( (z + e2*v*math.sin(lat_init)) / p ) height = (p / math.cos(lat)) - v # Ignore if a bit out # Turn from radians back into degrees long = long / 2 / math.pi * 360 lat = lat / 2 / math.pi * 360 return [lat,long,height]
def _step(self, action): assert self.action_space.contains(action), "%r (%s) invalid"%(action, type(action)) # Action Step self.A.speed = (action[0]+action[1])/2.0 * 5.0 self.A.dir_Angle += math.atan((action[0] - action[1]) * self.A.speed / 2.0 / 5.0) self.A.dir_Angle = ( self.A.dir_Angle + np.pi) % (2 * np.pi ) - np.pi done = self.A.Move([self.BBox]) self.A.Sens(self.Course) self.state = (1,) if self.A.EYE.obj == 1 else (0,) if not done: reward = 1.0 elif self.steps_beyond_done is None: # Robot just went out over the boundary self.steps_beyond_done = 0 reward = 1.0 else: if self.steps_beyond_done == 0: logger.warn("You are calling 'step()' even though this environment has already returned done = True. You should always call 'reset()' once you receive 'done = True' -- any further steps are undefined behavior.") self.steps_beyond_done += 1 reward = 0.0 return np.array(self.state), reward, done, {'AgentPos':(self.A.pos_x,self.A.pos_y),'AegntDir':self.A.dir_Angle}
def calcRoll(self, accel): x = accel[0] y = accel[1] z = accel[2] try: phi = math.atan(y / z) except: if y > 0: phi = 1.0 else: phi = -1.0 return phi
def calcPitch(self, accel): x = accel[0] y = accel[1] z = accel[2] try: theta = math.atan(-x / math.sqrt(y * y + z * z)) except: if x > 0: theta = -1.0 else: theta = 1.0 return theta
def calcYaw(self, magnet, roll, pitch): magX = magnet[0] magY = magnet[1] magZ = magnet[2] rowX = magX rowY = -magY rowZ = -magZ row = np.matrix([[rowX], [rowY], [rowZ]]) A = np.matrix([ \ [math.cos(roll), math.sin(roll) * math.sin(pitch), math.cos(pitch) * math.sin(roll)] \ , [0, math.cos(pitch), -math.sin(pitch)] \ , [-math.sin(roll), math.sin(pitch) * math.cos(roll), math.cos(roll) * math.cos(pitch)] \ ]) calib = A * row calibX = row[0] calibY = row[1] calibZ = row[2] try: yaw = math.atan(calibY / calibX) except: if calibY > 0: yaw = 1.0 else: yaw = -1.0 return yaw
def uv_to_st(cls, u): if cls.PROJECTION == cls.LINEAR_PROJECTION: return 0.5 * (u + 1) elif cls.PROJECTION == cls.TAN_PROJECTION: return (2 * (1.0 / math.pi)) * (math.atan(u) * math.pi / 4.0) elif cls.PROJECTION == cls.QUADRATIC_PROJECTION: if u >= 0: return 0.5 * math.sqrt(1 + 3 * u) else: return 1 - 0.5 * math.sqrt(1 - 3 * u) else: raise ValueError('unknown projection type')
def area(a, b, c): """Area of the triangle (a, b, c). see :cpp:func:`S2::Area` """ assert is_unit_length(a) assert is_unit_length(b) assert is_unit_length(c) sa = b.angle(c) sb = c.angle(a) sc = a.angle(b) s = 0.5 * (sa + sb + sc) if s >= 3e-4: s2 = s * s dmin = s - max(sa, max(sb, sc)) if dmin < 1e-2 * s * s2 * s2: area = girard_area(a, b, c) if dmin < 2 * (0.1 * area): return area return 4 * math.atan(math.sqrt( max(0.0, math.tan(0.5 * s) * math.tan(0.5 * (s - sa)) * math.tan(0.5 * (s - sb)) * math.tan(0.5 * (s - sc))) ))
def __init__(self, root, tip, span, sweep=None, sweepangle=45.0): self.root = root #: Root Chord of fin self.tip = tip #: Tip Chord of fin self.span = span #: Span ("height") of fin self._length = root if sweep is not None: self.sweep = sweep #: Sweep length of the fin self.sweepangle = atan(self.sweep / self.span) """Angle of sweep of the fin [radians]""" else: self.sweep = span * tan(radians(sweepangle)) self.sweepangle = radians(sweepangle)
def get_xy(alpha, beta, structure_settings): r = structure_settings.r # short arm length (attached to the rotative axis) a = structure_settings.a # long arm length s = structure_settings.s # pen distance xa = structure_settings.xa #left short arm x xb = structure_settings.xb #right short arm x # d is the first short arm extremity xd = xa - r * math.sin(alpha) yd = r * math.cos(alpha) # e is the first short arm extremity xe = xb - r * math.sin(beta) ye = r * math.cos(beta) de = compute_distance(xd, yd, xe, ye) #theta is the angle formed by de and the left long arm cos_theta = de/float(2 * a) cos_theta = min(cos_theta, 1.0) cos_theta = max(cos_theta, -1.0) theta = math.acos(cos_theta) #gamma is the angle formed by an horizontal axis and de tan_gamma = (ye-yd)/float(xe-xd) gamma = math.atan(tan_gamma) #lambda is the angle formed by an horizontal axis and the left long arm lam = theta + gamma xt = xd + a * math.cos(lam) - s * math.sin(lam) yt = yd + a * math.sin(lam) + s * math.cos(lam) return xt, yt
def atand(self, x): """Returns the arc tan in degrees""" return math.atan(x) * self.RADEG
def calc_user_look_at(pos_sat, lat, lon, alt, t): pos = calc_pos(lat, lon, alt, t) rx = pos_sat.x - pos.x ry = pos_sat.y - pos.y rz = pos_sat.z - pos.z jd = julian_date(t) theta = (theta_g_JD(jd) + lon) % TWO_PI top_s = sin(lat) * cos(theta) * rx + \ sin(lat) * sin(theta) * ry - \ cos(lat) * rz top_e = -sin(theta) * rx + \ cos(theta) * ry top_z = cos(lat) * cos(theta) * rx + \ cos(lat) * sin(theta) * ry + \ sin(lat) * rz az = atan(-top_e/top_s) if top_s > 0: az += pi if az < 0: az += TWO_PI rg = (rx*rx + ry*ry + rz*rz)**0.5 el = asin(top_z/rg) return (az, el, rg)
def unproject(x, y): """ Converts a x/y point to a lat/lng coordinate """ d = 180.0 / math.pi return ( (2.0 * math.atan(math.exp(y / SphericalMercator.R)) - (math.pi / 2.0)) * d, x * d / SphericalMercator.R )
def fromGeographic(self, lat, lon): lat = math.radians(lat) lon = math.radians(lon-self.lon) B = math.sin(lon) * math.cos(lat) x = 0.5 * self.k * self.radius * math.log((1.+B)/(1.-B)) y = self.k * self.radius * ( math.atan(math.tan(lat)/math.cos(lon)) - self.latInRadians ) return (x,y)
def toGeographic(self, x, y): x = x/(self.k * self.radius) y = y/(self.k * self.radius) D = y + self.latInRadians lon = math.atan(math.sinh(x)/math.cos(D)) lat = math.asin(math.sin(D)/math.cosh(x)) lon = self.lon + math.degrees(lon) lat = math.degrees(lat) return (lat, lon)
def tLowDotOperator(stack, z, mode): if mode == 1: # num stack.append(utilities.formatNum(math.atan(z))) elif mode == 2 or mode == 3: # str or list stack.append(1 if z[::-1]==z else 0) else: monadNotImplemented(mode, '') # ?
def __init__(self,g_pool , focal_length ): super().__init__(g_pool , "Debug Visualizer", False) self.focal_length = focal_length self.image_width = 640 # right values are assigned in update self.image_height = 480 camera_fov = math.degrees(2.0 * math.atan( self.image_height / (2.0 * self.focal_length))) self.trackball = Trackball(camera_fov) ############## MATRIX FUNCTIONS ##############################
def __init__(self, g_pool, world_camera_intrinsics , cal_ref_points_3d, cal_observed_points_3d, eye_camera_to_world_matrix0 , cal_gaze_points0_3d, eye_camera_to_world_matrix1 = np.eye(4) , cal_gaze_points1_3d = [], run_independently = False , name = "Calibration Visualizer" ): super().__init__( g_pool,name, run_independently) self.image_width = 640 # right values are assigned in update self.focal_length = 620 self.image_height = 480 self.eye_camera_to_world_matrix0 = eye_camera_to_world_matrix0 self.eye_camera_to_world_matrix1 = eye_camera_to_world_matrix1 self.cal_ref_points_3d = cal_ref_points_3d self.cal_observed_points_3d = cal_observed_points_3d self.cal_gaze_points0_3d = cal_gaze_points0_3d self.cal_gaze_points1_3d = cal_gaze_points1_3d if world_camera_intrinsics: self.world_camera_width = world_camera_intrinsics['resolution'][0] self.world_camera_height = world_camera_intrinsics['resolution'][1] self.world_camera_focal = (world_camera_intrinsics['camera_matrix'][0][0] + world_camera_intrinsics['camera_matrix'][1][1] ) / 2.0 else: self.world_camera_width = 0 self.world_camera_height = 0 self.world_camera_focal = 0 camera_fov = math.degrees(2.0 * math.atan( self.window_size[0] / (2.0 * self.focal_length))) self.trackball = Trackball(camera_fov) self.trackball.distance = [0,0,-80.] self.trackball.pitch = 210 self.trackball.roll = 0 ########### Open, update, close #####################