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)
python类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 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)
Advait balloon duel.py 文件源码
项目:Graphical-Programs-Python
作者: Advait-M
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
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 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 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 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 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 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 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)
extract_features.py 文件源码
项目:inception-face-shape-classifier
作者: adonistio
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
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 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 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 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 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 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 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 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)