I need to calculate the FOV [km].
the angular FOV is = 98 x 98° and
the distance of object is = 86km
FOV [km] = (tan(98/2) * 180 / pi) * 86
gives negative results. Where could be my error?
You have to put pi/180 multiplier inside parentheses, it should be applied to angle
FOV [km] = tan(98/2 * pi / 180) * 86 * 2 = 198 km
Note x2 for full range
Related
I have two points in 2D space as we can see in the figure to move from the blue point to the red points we can use the equation (1). Where b is a constant used to limit the shape of the logarithmic spiral, l is a random number in [−1,1], which is used to
control the indentation effect of the movement, D indicates the distance between blue points and the current point
I need another movement that can move from blue points to the red points like in the figure
You can use sinusoidal model.
For start point (X0, Y0) and end point (X1,Y1) we have vector end-start, determine it's length - distance between points L, and angle of vector Fi (using atan2).
Then generate sinusoidal curve for some standard situation - for example, along OX axis, with magnitude A, N periods for distance 2 * Pi * N:
Scaled sinusoid in intermediate point with parameter t, where t is in range 0..1 (t=0 corresponds to start point (X0,Y0))
X(t) = t * L
Y(t) = A * Sin(2 * N * Pi * t)
Then shift and rotate sinusoid using X and Y calculated above
X_result = X0 + X * Cos(Fi) - Y * Sin(Fi)
Y_result = Y0 + X * Sin(Fi) + Y * Cos(Fi)
Example Python code:
import math
x0 = 100
y0 = 100
x1 = 400
y1 = 200
nperiods = 4
amp = 120
nsteps = 20
leng = math.hypot(x1 - x0, y1 - y0)
an = math.atan2(y1 - y0, x1 - x0)
arg = 2 * nperiods* math.pi
points = []
for t in range(1, nsteps + 1):
r = t / nsteps
xx = r * leng
yy = amp * math.sin(arg * r)
rx = x0 + xx * math.cos(an) - yy * math.sin(an)
ry = y0 + xx * math.sin(an) + yy * math.cos(an)
points.append([rx, ry])
print(points)
Draw points:
I have a dataset from kaggle of 45,253 rows and a single column for temperature in Kelvin for the city of Detroit. It's mean = 282.97, std = 11, min = 243.48, max = 308.05.
This is the result when plotted as a histogram of 100 bins with density=True:
I am expected to write the following two functions and see whichever one approximates the closest to the histogram:
Like this one here using scipy.stats.norm.pdf:
I generated the above image using:
x = np.linspace(dataset.Detroit.min(), dataset.Detroit.max(), 1001)
P_norm = norm.pdf(x, dataset.Detroit.mean(), dataset.Detroit.std())
plot_pdf_single(x, P_norm)
However, whenever I try to implement any of the two approximation functions all of my values for P_norm result in 0s or infs.
This is what I tried:
P_norm = [(1.0/(np.sqrt(2.0*pi*(std*std))))*np.exp(((-x_i-mu)*(-x_i-mu))/(2.0*(std*std))) for x_i in x]
I also broke it down into parts for a single x_i:
part1 = ((-x[0] - mu)*(-x[0] - mu)) / (2.0*(std * std))
part2 = np.exp(part1)
part3 = 1.0 / (np.sqrt(2.0 * pi * (std*std)))
total = part3*part2
I got the following values:
1145.3913234604413
inf
0.036267480036493875
inf
Since both of the equations use the same formula:
def pdf_approximation(x_i, mu, std):
return (1.0 / (np.sqrt(2.0 * pi * (std*std)))) * np.exp((-(x_i-mu)*(x_i-mu)) / (2.0 * (std*std)))
The code for the first approximation is:
mu = 283
std = 11
P_norm = np.array([pdf_approximation(x_i, mu, std) for x_i in x])
plot_pdf_single(x, P_norm)
The code for the second approximation is:
mu1 = 276
std1 = 6
mu2 = 293
std2 = 6.5
P_norm = np.array([(pdf_approximation(x_i, mu1, std1) * 0.5) + (pdf_approximation(x_i, mu2, std2) * 0.5) for x_i in x])
plot_pdf_single(x, P_norm)
I want to put the red polygon in place of the empty one. But it goes above it first before returning again to it. Can someone help me with that?
Why's the red polygon goes out of plane, then returns to its specified place again?
def Rotating(Rotating_angle, polygon_points): # Drawing the rotated figure
my_points = (re.findall("\(\-?\d*\.?\d*\,\-?\d*\.?\d*\)", polygon_points))
sleep_time = .5
my_new_points = [] # Scale_points
for point in my_points:
new_point = str(point).replace(")", "").replace("(", "").split(",")
# creating a list with all coordinates components
all_coordinates_components = []
all_coordinates_components.append(abs(eval(new_point[0])))
all_coordinates_components.append(abs(eval(new_point[1])))
point = (scale * eval(new_point[0]), scale * eval(new_point[1]))
my_new_points.append(point)
rotated_points = []
for point in my_new_points:
new_point = str(point).replace(")", "").replace("(", "").split(",")
theta = Rotating_angle
X = (eval(new_point[0]) * cos(theta * pi / 180)) - (eval(new_point[1]) * sin(theta * pi / 180))
Y = (eval(new_point[0]) * sin(theta * pi / 180)) + (eval(new_point[1]) * cos(theta * pi / 180))
# length = sqrt((X) ** 2 + (Y) ** 2)
point = (X, Y)
rotated_points.append(point)
# draw steps
time.sleep(sleep_time)
draw_rotation_steps(my_new_points, theta) # draw steps ((((( 3 )))))
# drawing rotated polygon
draw_polygon(rotated_points) # draw rotated polygon ((((( 4 )))))
s = Shape('compound')
poly1 = (my_new_points)
s.addcomponent(poly1, fill="red")
register_shape('myshape', s)
shape('myshape')
polygon = Turtle(visible=False)
polygon.setheading(90)
polygon.speed('slowest')
polygon.penup()
polygon.shape('myshape')
polygon.st()
polygon.circle(0, theta)
pen_dot = Turtle(visible=False)
pen_dot.speed('fastest')
for point in rotated_points:
pen_dot.penup()
pen_dot.goto(point)
pen_dot.pendown()
pen_dot.dot(5, 'blue violet')
I can't reproduce the behaviour you describe. But your code is riddled with issues that should be addressed so perhaps fixing those might also fix the positioning issue:
These loops are nested, but they shouldn't be:
my_new_points = []
for point in my_points:
...
rotated_points = []
for point in my_new_points:
They both should be at the same level.
You shouldn't use eval(). In this situation, use float():
point = (scale * eval(new_point[0]), scale * eval(new_point[1]))
Here, you've already converted the points but you turn them back into strings and reconvert them:
new_point = str(point).replace(")", "").replace("(", "").split(",")
X = (eval(new_point[0]) * cos(theta * pi / 180)) - (eval(new_point[1]) * sin(theta * pi / 180))
when you can simply do:
X = point[0] * cos(theta * pi / 180) - point[1] * sin(theta * pi / 180)
You don't have to put the pen down for the .dot() method to work:
pen_dot.goto(point)
pen_dot.pendown()
pen_dot.dot(5, 'blue violet')
so you can move the penup() out of the loop.
Below is my rework of your example code. I've added just enough code to make it runnable and removed anything that had nothing to do with the problem. See if this gives you any ideas of how to simplify and fix your own code:
import re
from math import sin, cos, pi
from turtle import *
scale = 20
def draw_rotation_steps(points, theta):
''' Not supplied by OP '''
pass
def draw_polygon(rotated_points):
''' Simple replacement since not supplied by OP '''
hideturtle()
penup()
goto(rotated_points[-1])
pendown()
for point in rotated_points:
goto(point)
def Rotating(theta, polygon_points): # Drawing the rotated figure
my_points = re.findall(r"\(\-?\d*\.?\d*\,\-?\d*\.?\d*\)", polygon_points)
my_new_points = [] # Scaled points
for point in my_points:
X, Y = point.replace(")", "").replace("(", "").split(",")
point = (scale * float(X), scale * float(Y))
my_new_points.append(point)
rotated_points = []
for point in my_new_points:
X = point[0] * cos(theta * pi / 180) - point[1] * sin(theta * pi / 180)
Y = point[0] * sin(theta * pi / 180) + point[1] * cos(theta * pi / 180)
point = (X, Y)
rotated_points.append(point)
# draw steps
draw_rotation_steps(my_new_points, theta) # draw steps
# drawing rotated polygon
draw_polygon(rotated_points) # draw rotated polygon
s = Shape('compound')
s.addcomponent(my_new_points, fill="red")
register_shape('myshape', s)
polygon = Turtle('myshape', visible=False)
polygon.setheading(90)
polygon.showturtle()
polygon.circle(0, theta, steps=100) # added steps to visually slow it down
pen_dot = Turtle(visible=False)
pen_dot.penup()
for point in rotated_points:
pen_dot.goto(point)
pen_dot.dot(5, 'blue violet')
Rotating(180, "(-8,-6) (-6,-3) (-3,-4)")
mainloop()
I want to rotate an oval around it's center. Currently I can display the static object witohut rotation, and I can't seem to find what to do wit it.
EDIT: I made a rotation function based on complex number multiplication, which seemed to work on lower polygons but the ellipse kinda looks the same.
A potato-level noob C.S. student.
also, the code:
from tkinter import*
import cmath, math
import time
def poly_rot(tup, deg):
rad=deg*0.0174533
crot=cmath.exp(rad*1j)
rotpol=[]
i=0
while i < len(tup):
x=tup[i]
y=tup[i+1]
z=crot*complex(x,y)
rotpol.append(400+z.real)
rotpol.append(400+z.imag)
i=i+2
return rotpol
def poly_oval(x0,y0, x1,y1, steps=60, rotation=0):
"""return an oval as coordinates suitable for create_polygon"""
# x0,y0,x1,y1 are as create_oval
# rotation is in degrees anti-clockwise, convert to radians
rotation = rotation * math.pi / 180.0
# major and minor axes
a = (x1 - x0) / 2.0
b = (y1 - y0) / 2.0
# center
xc = x0 + a
yc = y0 + b
point_list = []
# create the oval as a list of points
for i in range(steps):
# Calculate the angle for this step
# 360 degrees == 2 pi radians
theta = (math.pi * 2) * (float(i) / steps)
x1 = a * math.cos(theta)
y1 = b * math.sin(theta)
# rotate x, y
x = (x1 * math.cos(rotation)) + (y1 * math.sin(rotation))
y = (y1 * math.cos(rotation)) - (x1 * math.sin(rotation))
point_list.append(round(x + xc))
point_list.append(round(y + yc))
return point_list
inp= input("We need an ellipse. One to fit in a 800*800 window. Give the size of it's axes separated by ','-s (ex: lx,ly)!\n")
inpu= inp.split(',')
lx=int(inpu[0])
ly=int(inpu[1])
x0=400-lx//2
x1=400+lx//2
y0=400-ly//2
y1=400+ly//2
deg= float(input("Let's rotate this ellipse! But how much, in degrees, should we?"))
pre=poly_oval(x0, y0, x1, y1)
post=poly_rot(poly_oval(x0, y0, x1, y1), deg)
root =Tk()
w = Canvas(root, width=801, height=801)
w.config(background="dark green")
w.pack()
w.create_polygon(tuple(post), fill="yellow", outline="yellow" )
root.mainloop()
coordinates of black points is known. width of rectangle is 10. how can i define all coordinates of rectangle?
http://i.stack.imgur.com/Sc2oz.jpg
Let's M0, M1 are black points.
//vector M0-M1
mx = M1.X - M0.X
my = M1.Y - M0.Y
//perpendicular vector
px = - my
py = mx
//it's length
lp =Sqrt(px*px + py*py)
//unit perp. vector
upx = px / lp
upy = py / lp
//vertices
V1.x = M0.X + 5 * upx
V1.y = M0.Y + 5 * upy
V2.x = M0.X - 5 * upx
V2.y = M0.Y - 5 * upy
//the same for M1 and V3, V4
By width = 10, I assume the shortest side has a width of 10. Half of its width is therefore 5.
Lets first find the vector going from L to R, then normalize it to have length 1 and stretch it to length 5. Lets call this vector A. A can be calculated as follows: A = 5*(R-L) / |R-L|.
Now, A can be rotated 90 degrees clockwise or counterclockwise and be applied to L, to obtain S or W, respectively.
In the same way, A can be rotated 90 degrees clockwise or counter clockwise and be applied to R, to botain E or N, respectively.
That is:
S = L + A * Rotate(-90)
W = L + A * Rotate(90)
E = R + A * Rotate(-90)
N = R + A * Rotate(90)
where Rotate(x) is the rotation matrix for rotating a vector x degrees counter clockwise, as defined in https://en.wikipedia.org/wiki/Rotation_matrix
The complete calculations:
Ax = 5 * (Rx-Lx) / sqrt((Rx-Lx)^2 + (Ry-Ly)^2)
Ay = 5 * (Ry-Ly) / sqrt((Rx-Lx)^2 + (Ry-Ly)^2)
S = (Lx + Ay, Ly - Ax)
W = (Lx - Ay, Ly + Ax)
E = (Rx + Ay, Ry - Ax)
N = (Rx - Ay, Ry + Ax)