So say I have two lines going from the radius to the edge, one of these is my start point and the other is the end point. I need to find out which direction I should rotate them in to get to the end point fastest. (this will be done through a while loop adding and subtracting 1). For example say we have StartRotation = 45 and EndRotation = 270, I need the math algorithm to find out whether to add our subtract (in the examples case we will subtract because going counter clockwise around a circle from 45 degrees to 270 degrees is faster than going clock wise).
(A Side note: setting the rotation to -90, 630, etc... is the same as setting it to 270)
Possible logic:
Normalize angles
if Start < End
Direction = 1
else
Direction = - 1
if Abs(Start - End) > 180
Direction = - Direction
The two angles are |S-E| and 360°-|S-E|. Keep the smallest.
D= S - E
if D > 0:
if D > 180:
D-= 360
else:
if D < -180:
D+= 360
D gives the signed amplitude.
Related
Given a grid inclined at an angle θ (theta) with equal sized square-shaped cells with indices 00, 01, 02, ..., 55 (where first digit being x index and second being y index, for instance 00 means a cell at the intersection of row 0 and column 0), and a point p(x,y), how can we know that in which grid cell does the point lie without checking all the cells?
For instance, in the image below point p lies in the cell with index 23.
I found one answer at Checking if a point is inside a rotated rectangle that explains how to determine if a point lies inside a rotated rectangle, but with this approach I need to check all the grid cells.
Perhaps the simplest way - rotate point by the same angle (with negative sign) and check new coordinates in axis-aligned grid
nx = x * cos(theta) - y * sin(theta)
ny = x * sin(theta) + y * cos(theta)
row = (int) (ny / cellsize)
col = (int) (nx / cellsize)
If having a centre(xc,yc) and radius(r), how can i calculate total number of coordinates(x,y) in the circle?
I suspect that you are talking about integer points in the circle. Otherwise question is senseless.
In this case you can apply Gauss formula
N = 1 + 4 * r + 4 * sum[i=1..r]{floor(sqrt(r^2-i^2))}
Note that center has integer coordinates too.
In a circle there is 360 degrees. if the vessel is facing east, the computer shows that it is facing 90 degrees. the anemometer tells the office that the wind is coming form 90 degrees also. so the reading shown is 90 degrees. But its intact 180 degrees because it is coming from the south not east. I have this part completed but if the vessel was facing 300 degrees, and the wind is coming 90 degrees to the vessel, the reading comes out as 90 from the anemometer, but it should infect be 210. I've created a program that takes in the boat direction and wind direction and if both inputs are from 0 to 180 then I can add them both together to receive the true wind direction. but if the inputs are from 270 to 360 then the total will go past 360 degrees and come out as lets say 540.
How could I make my program restart counting the degrees when it gets to 360 degrees?
The conventional way to restrict a calculation between 0 and some number N is to get the modulo of a number. We might also call this the "remainder" of a number when divided by something else. The result of the modulo will always be between 0 (inclusive) and the number you're dividing by (exclusive).
In python, the modulo operator is the percent character - %.
So 1 % 10 is the remainder of 1 divided by 10, or 1. 12 % 10 = 2. 0 % 10 = 0;
Applying this to your case, if you want to limit the values of your calculations to between 0 and 360, you can do all your calculations, and then get the modulo against 360.
An example might look like
deg = (vessel + anemometer) % 360
This should work. As was mentioned previously, you need to use modulus 360 to keep your angles within 0-360. You should also use modulus 360 on the result in case your ship angle is small enough to result in a negative value after subtracting the wind angle relative to the vessel.
def calculate_wind_angle(vessel_degrees, wind_degrees_from_vessel):
return ((vessel_degrees % 360) - (wind_degrees_from_vessel % 360)) % 360
vessel_degrees = 300
wind_degrees_from_vessel = 90
wind_degrees = calculate_wind_angle(vessel_degrees, wind_degrees_from_vessel)
print(wind_degrees)
I've got a big number of nodes (lon,lat) in WGS-84 and I need to draw them on a Pixmap, so I have read a lot of Q&A here, but haven't found the algorithm how to convert lon/lat from WGS-84 to a x/y coordinates. By the way, I need to draw a simple scheme of map. Any ideas?
To go from WGS-84 latitude and longitude to an two-dimensional map, you first need to consider what kind of projection you have. This is because one minute of arc, for example, could mean different distance changes over a particular projection. You're mapping an ellipsoid to a plane, so you're going to get some distortion somewhere.
But for a simple case, let's say that your area is small enough, and close enough to the equator, that the change in angle (latitude or longitude) corresponds to a constant change in distance on the map (Y or X).
So, if you have a 600*600 image of a particular area, and it corresponds to a 10-minute by 10-minute area of the earth that has an upper-left corner at 30 degrees north, 40 degrees west.
To locate the pixel where 29 degrees, 55 minutes north, 39 degrees, 57 minutes west, we use a proportion for both latitude and longitude:
5' / 10' = Y / 600 pixels ---> Y = 300 (from the top edge)
3' / 10' = X / 600 pixels ---> X = 180 (from the left edge)
Hope that helps.
Suppose 5 samples of hue are taken using a simple HSV model for color, having values 355, 5, 5, 5, 5, all a hue of red and "next" to each other as far as perception is concerned. But the simple average is 75 which is far away from 0 or 360, close to a yellow-green.
What is a better way to calculate this mean and associated std?
The simple solution is to convert those angles to a set of vectors, from polar coordinates into cartesian coordinates.
Since you are working with colors, think of this as a conversion into the (a*,b*) plane. Then take the mean of those coordinates, and then revert back into polar form again. Done in matlab,
theta = [355,5,5,5,5];
x = cosd(theta); % cosine in terms of degrees
y = sind(theta); % sine with a degree argument
Now, take the mean of x and y, compute the angle, then
convert back from radians to degrees.
meanangle = atan2(mean(y),mean(x))*180/pi
meanangle =
3.0049
Of course, this solution is valid only for the mean angle. As you can see, it yields a consistent result with the mean of the angles directly, where I recognize that 355 degrees really wraps to -5 degrees.
mean([-5 5 5 5 5])
ans =
3
To compute the standard deviation, it is simplest to do it as
std([-5 5 5 5 5])
ans =
4.4721
Yes, that requires me to do the wrap explicitly.
I think the method proposed by user85109 is a good way to compute the mean, but not the standard deviation:
imagine to have three angles: 180, 180, 181
the mean would be correctly computed, as a number aproximately equal to 180
but from [180,180,-179] you would compute a high variance when in fact it is near zero
At first glance, I would compute separately the means and variances for the half positive angles , [0 to 180] and fot the negative ones [0,-180] and later I would compute the combined variance
https://www.emathzone.com/tutorials/basic-statistics/combined-variance.html
taking into account that the global mean and the difference between it and the local means has to be computed in both directions: clockwise and counterclockwise, and the the correct one has to be chosen.