Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a map where they find several points (lat/long) and want to know the distance that exists between them.
So, given a set of lat/long coordinates, how can I compute the distance between them in python?
I once wrote a python version of this answer. It details the use of the Haversine formula to calculate the distance in kilometers.
import math
def get_distance(lat_1, lng_1, lat_2, lng_2):
d_lat = lat_2 - lat_1
d_lng = lng_2 - lng_1
temp = (
math.sin(d_lat / 2) ** 2
+ math.cos(lat_1)
* math.cos(lat_2)
* math.sin(d_lng / 2) ** 2
)
return 6373.0 * (2 * math.atan2(math.sqrt(temp), math.sqrt(1 - temp)))
Ensure the coordinates being passed to the function are in radians. If they're in degrees, you can convert them first:
lng_1, lat_1, lng_2, lat_2 = map(math.radians, [lng_1, lat_1, lng_2, lat_2])
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 13 hours ago.
Improve this question
When a variable is normalized, its value will change and its sum will be 1. However, the value obtained when the variable is later substituted does not change. Can anyone help to solve this problem? Let's put the specific example below. Normalize fy, y does not change, no matter yF will change, but dy has not changed. I hope you can solve this problem.
fy_ = 0
for i in range(len(fy)):
fy_ += fy[i]
fy = fy / fy_
print(sum(fy))
yF = 0
for i in range(len(y) - 1):
yF += (y[i + 1] -y[i]) * (y[i] * fy[i])
print(yF)
dy = y * fy / yF
print(dy)
I have used different y, fy has tried many times, and every time dy basically does not change, which makes me wonder why this phenomenon occurs
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I must've skipped school that day because I cannot remember how to calculate the middle of a square.
There are a few different ways to calculate the middle of a square using Python. One way is to find the average of the x and y coordinates of the square's four corners. Another way is to find the point that is equidistant from all four corners of the square.
# method-1
def square_middle(square):
x1, y1, x2, y2 = square
return ((x1 + x2) / 2, (y1 + y2) / 2)
# method-2
def square_middle(square):
x1, y1, x2, y2 = square
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
return (cx, cy)
frameworks usually report the size of a screen with 2 values or a touple of 2. For example, this is how pyautogui reports it:
print(pyautogui.size())
Size(width=1728, height=1117)
And this is how that framework gets the center for every coordinates tuple:
https://github.com/asweigart/pyscreeze/blob/master/pyscreeze/__init__.py#L579
So you can safely use the example above to get the center of any object on screen, even the screen itself.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm trying to create a program that calculates the adjacent and opposite of a triangle using the hypotenuse and the angle between the hypotenuse and the adjacent with python
Just remember SohCahToa. We have the hypotenuse (h) so for the opposite, we set up the equation sin(angle) = opposite/hypotenuse. To find the opposite we multiply both sides by the hypotenuse to get hypotenuse * sin(angle) = opposite. The do a similar thing with cos(angle) = adjacent / hypotenuse. Then you have your opposite and adjacent sides.
In terms of code it would look something like this:
import math # we need this specifically to use trig functions
angle = (some angle here)
hypotenuse = (some number here)
opposite = hypotenuse * math.sin(math.radians(angle))
# we have to multiply the angle by PI/180 because the python sin function uses radians and we do this by using math.radians() is how to convert from degrees to radians.
adjacent = hypotenuse * math.cos(angle * (math.pi/180))
the best way I found was to do:
import math
hype = 10
angle = 30
adj = 0
opp = 0
adj = hype*math.cos(math.radians(angle))
opp = hype*math.sin(math.radians(angle))
opp
print(str(opp) + ' is the opposite side value')
print(str(adj) + ' is the adjacent side value')
print(str(hype) + ' is the hypotenuse')
print(str(angle) + ' is the angle')
the math package in python allows you to work with sine and cosine, which you will need when doing trig like this.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have following for loop to extract bounding boxes in an image. Assuming the p is the center point of each box.
The distance formula is: √[(x₂ - x₁)² + (y₂ - y₁)²]. This works for any two points in 2D space with coordinates (x₁, y₁) for the first point and (x₂, y₂) for the second point. You can notice that it is Pythagoras theorem and the distance is the hypotenuse, and the lengths of the sides are the difference between the x and y components of the points.
So once you have p (center point of bounding box) for two boxes you need to just apply the formula. You can also create a loop and do that for all the p variables that you calculate depending on how many boxes you have.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Need help with this of python
Write a program with 3 functions to find out the
(function 1) dot product, (function 2) angle, and
(function 3) cross product of two vectors.
Program should ask a user to input three
points in 3D space such as (x1, y1, z1), (x2,
y2, z2) (x3, y3, z3).
Find two vectors
Find dot product,
Find the angle between two vectors
Find the cross product of two vectors
You will need to import library visual to use the following functions.
Given vector v1 and v2:
To find the angle:
diff_angle(v1,v2)
or
v1.diff_angle(v2)
This gives the angle in radians.
To get the dot product:
dot(v1,v2)
can also be written as:
mag(v1)*mag(v2)*cos(diff_angle(v1,v2))
or
v1.dot(v2))
To find cross product:
cross(v1,v2)
or:
mag(v1)*mag(v2)*sin(diff_angle(v1,v2))
or:
v1.cross(v2)
Given two point p1 and p2, to find vector fromp1top2`:
vector(p2)-vector(p1)