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 7 years ago.
Improve this question
I am using SimpleGui and pygame modules to develop a game on python. I have a line and its coordinates as shown below:
I am interested in moving the line diagonally in the direction of an arrow for which I will require to update the coordinates before drawing it on canvas. I am confused as how to update the x and y coordinates to move the line in desired direction.
You have to choose N to get step size
dx = (x1-x2)/N
dy = (y1-y2)/N
and then you can move
x1 += dx
y1 += dy
x2 += dx
y2 += dy
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 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 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 2 years ago.
Improve this question
I need to understand how numpy.interp function works. This function has a couple of parameters which are vague to me, like: xp, fp, left and right.
I saw an example of this function as below:
X is a numpy ndarray and y is one-dimensional array:
X = np.interp(X, (X.min(), X.max()), (0, 10))
y = np.interp(y, (y.min(), y.max()), (30000, 100000))
Thanks for helping me out!
x are the points which are not in xp but you want their y values i.e. points where you want to perform interpolation
xp and yp are the main inputs based on which 1D interpolation works (these are the discrete data points through which you want to interpolate)
left and right handle the edge case scenarios when x is out of the closed interval range of [xp_minimum, xp_maximum] i.e. the output values to generate incase of extrapolation
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 4 years ago.
Improve this question
I have an numpy array image which contain circles. I extracted the whole x,y centroids (in pixels) of these circles (a numpy array as well). Now, I want to crop a square around each x,y centroid.
Can someone instruct me how to solve it?
Note that I didn't find any question in Stack that deals with crop around a specific coordinate.
Thank you!
If your centroid has indices i,j and you want to crop a square of size 2*w+1 around it on a numpy array a, you can do
a[i-w:i+w+1,j-w:j+w+1]
This is provided your indices are always more than w from the boundary.
If they're not, you can do
imin = max(0,i-w)
imax = min(a.shape[0],i+w+1)
jmin = max(0,j-w)
jmax = min(a.shape[1],j+w+1)
a[imin:imax,jmin:jmax]