I need some help with programming the calculation of displacement. Given the distance the object has moved in the XY plane, and the yaw (heading) of the object, I'd like to calculate the displacement of the object. For example, the object moved 5m North and 2m East.
The data I have is the distance it travels in the XY plane (X distance and Y distance) and the heading of the device which is determined by the direction of X. I know how to calculate it on paper but I am having trouble to program it.
I've attached two examples that shows how I obtained the calculations. The black dot is the displacement of the object. In the 1st example, the X-distance travelled is 3 meters while the Y-distance is 4 meters and the heading of the X-axis is 70°. After calculating, I managed to get that it has travelled 2.733m South and 4.187m East. In example 2, it travel, the X-distance is -5m and the Y-distance is -12m, the heading is 160°. I calculated that the object travels 8.80m North and 9.566m East.
Example 1
Example 2
Assume you have your XY pane rotated by some angle Zp and have a 2D movement vector within that pane, let's say its direction rotated by Zv – what is total rotation of the vector, relative to the global orientation? Zp + Zv...
So all you need to do is applying the rotation Zp to the movement vector within XY-pane, i. e. apply a rotation matrix to the vector. This matrix for a rotation by z looks like:
cos(z) -sin(z)
sin(z) cos(z)
With our data above:
north = cos(Zp) * x - sin(Zp) * y
east = sin(Zp) * x + cos(Zp) * y
which gives, with your first sample data applied:
north = cos(70°) * 3 - sin(70°) * 4 ~ -2.7327
east = sin(70°) * 3 + cos(70°) * 4 ~ 4.1872
north = cos(160°) * -5 - sin(160°) * -12 ~ 8.8027
east = sin(160°) * -5 + cos(160°) * -12 ~ 9.5662
Corresponding pretty much to the values you calculated (note: negative movement to north is positive movement towards south...).
What you are doing is a conversion from polar to Cartesian coordinates. https://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates
If your Cartesian coordinates are not aligned to the polar axis, just compensate by adding the rotation angle to the polar argument.
Related
I know that a Hadamard gate is implemented by rotating around the axis (x + z )/ sqrt(2), but how can I compute the matrix obtained by rotation around this axis by π radians, and compare to a Hadamard gate matrix.
Thanks for your help.
Using H = (x + z)/ sqrt(2)
Find the rotation along x-axis (rx), and rotation along z-axis (rz), then multiply.
Here in this code, 50 random coordinates are generated(population size 50).
I have an array,
B = [150 90; -100 -120; -80 130; 140 -70; 60 120; -90 -130].
I want to calculate the distance of each 50 coordinates from each of the coordinates of B array. After calculating the distance, I have to preserve all of the distance values separately in an array(or matrix) to retrieve them afterwards.
Please help me to calculate the distance.
clear all
clc
%Common Parameter Setting
N=2; % Number of variables
M=50; % Populations size 50
F=0.5; % Mutation factor
C=0.9; % Crossover rate
I_max=20; % Max iteration time
Run=1; % The number of test time
X_max=[100,100];
X_min=[-100,-100];
%Func=#Rastrigin;
% 2.The whole test loop
for r=1:Run
iter=0;
% 1.Generate MxN matrix
for m=1:M
for n=1:N
X(m,n)=X_min(n)+rand()*(X_max(n)-X_min(n));
end
fprintf('value of X:');
disp(X);
end
end
I did not completely understand the last part. But if you have 2 lists of coordinates, say
x = randn(10,2); %10 points in 2D
y = randn(3,2); % 3 others points in 2D
and you want the pairwise distance between all points in x and all points in y, you can use pdist2
D = pdist2(x,y);
Now D(1,2) will be the Euclidean distance from x(1) to y(2) and so on.
For a 2D matrix A, we want to find the sum of the submatrix from (x1,y1) to (x2, y2) coordinates.
I came across this explanation of finding the submatrix sums, and I can follow the logic of the solution until the last part where they perform the following computation:
sum[x2][y2] + sum[x1][y1] - sum[x1][y2] - sum[x2][y1]
The idea is that they compute a matrix where each point represent the sum of the matrix, with all of which with reference to the original x1,y1 being (0,0). Then using some geometry method, they get the sums for a certain sub array. What I don't get is the geometry part. How does it come into play? I will draw the array for the sake of completeness.
Assume we have A as such:
1 2 3
4 5 6
7 8 9
For example, assume after finding the matrix sums we have the following:
1 3 6
5 12 21
12 27 45
Assume that I want to find the sum of the sub array starting from (1,1) to (2,2) where (0,0) is the origin at the sum 10. Then according to the formula, we have the sum
A[2][2] + A[1][1] - A[1][2] - A[2][1]
which gives 12 + 45 - 27 - 21 = 9.
Which isn't really the correct answer, 28?
Is there something wrong with this answer?
After calculating the sums you mentioned, sum[x][y] will represent the sum of the rectangle from (0,0) to (x,y).
Now we want to calculate the sum of the subarray from (x1,y1) to (x2,y2). We start with sum[x2][y2]. we need to subtract sum[x1-1][y2] and sum[x2][y1-1] since they don't belong to the required rectangle. However, notice that the red rectangle was subtracted twice, so we add sum[x1-1][y1-1]
I'm taking a sprite sheet and splitting it up into an array. I then take that array and rotate the image by a certain granularity and push that onto the end of the array too to pre bake the rotations for faster drawing (no hardware acceleration). The final result looks something like this:
array[0] = frame1 of the animation
array[1] = frame2 of the animation
array[2] = frame3 of the animation
array[3] = frame1 of the animation rotated at 45 degrees
array[4] = frame2 of the animation rotated at 45 degrees
array[5] = frame3 of the animation rotated at 45 degrees
array[6] = frame1 of the animation rotated at 90 degrees
... etc
So now I'm trying to come up with a function that'll return the right element for any angle at the right granularity. For example, say my in-game object is currently rotated 30 degrees and is on frame1 of the animation, I would want array[3]. If I was on frame2 of the animation, I would want array[4]. If the object was rotated 80 degrees and was on frame1, I would want array[6], and so on.
These are the constants I have:
The # of animation frames (pre-rotation)
The # of animation frames (post-rotation)
The Granularity of the Rotations (from 1 to 360, ie granularity of 45 would be the above example).
This is the signature of the function to calculate the frame:
function calculateImageIndex(animationFrame : Number, currentAngle : Number) : Number
Any ideas on how to calculate this? I'm stumped.
nf: total number of frames
gr: granularity of rotation
r: actual angle of rotation
f: actual frame number
i = round(r / gr) * nf + f - 1
n = Number of frames (Pre-rotation)
g = granularity of rotation
r = actual angle of rotation
f = actual frame
I think these are the constants you need and are given.
The first thing we will want to do it find the number of rotations it would take to get to the angle desired. In the above example getting to an angle of 45 would be 1 rotation. 90 degrees = 2 rotations and so on. Let R = number of rotations.
R = r / g This should always be an integer as you should never need an angle that doesn't fit the gratuity you used.
Next we will calculate the starting index of that rotation "group". In your example above the first group with a rotation 0 would start at index 0. 45 degress or 1 rotation would start at index 3. And so on. To do this we need to multiply the number of rotations (R) by the number of frames pre-rotation (n). Let j = the starting index of that rotation group.
j = R * n
The last step would be to figure out how much you must add to the starting index (j) to reach the frame you want. I will assume the first frame will be numbered 1 as in your example above, but if the first frame is numbered 0 then remove the -1 in the algorithm. Let i = the final index.
i = j + (f - 1)
I will be the index you are looking for. To put this together in one algorithm it would look like this.
i = ((r / g) * n ) + (f - 1)
Hope this helps! Let me know if you need my to clarify anything.
So i m trying to make hexagonal grid in C for a game. I am really dumb founded on where to start on it. ANyone have any ideas.
EDIT: I need about 15-20 hexagons in a grip shape all joined,something like a game board. for a game i m working on. Sorry for not being clear
Absolutely. Despite their odd shape, hexagons can still be contained in your usual multidimensional array, for future use (I assume you'll want to put things in your hexagons). As for drawing them, it's simple. Sum of angles = (6 - 2) * 180 = 4 * 180 = 720. One angle is 720 / 6 = 120 degrees. Calculate first the leftmost angle's Y position, which is equal to √(hexagonSide - hexagonWidth * hexagonWidth). I'm sure you can figure out hexagonWidth, right? Okay, now the X position relative to the last one will be 0. You'll need to offset the Y position by half the height of the hexagon before it, up or down depending on whether row * col is even or odd. Since you know the hexagon's width you have the coordinates of the opposite angle. Rotate by 120° and repeat.
Before I continue, is this supposed to be in the console? Or is it real graphics?
So let's get this straight, the game will be played on the console? Right, well now you will need to set up your data structures, the most obvious is with nodes.
The nodes
Each hexagon is a node with six edges.
typedef struct Node {
void *object; /* Pointer to object */
Node *node_array; /* Pointer to node_array with 'node_count' nodes */
int node_count; /* size of node_array */
} Node;
How to initialize and connect the node structure
Imagine the following hexagon:
/\
| |
\/
It has the following edges, NORTHEAST, EAST, SOUTHEAST, SOUTHWEST, WEST and NORTHWEST. Next observe how they will be arranged (10, 11 and 12 were represented in Hex so that they can fit in one space):
// 0 1 2 3
// 4 5 6 7 8
// 9 A B C
So 0 will link to 5 through it's SOUTHEAST link, and 4 through it's SOUTHWEST link. Also notice how the rows alternate between odd and even numbers of elements. Let's call {0, 1, 2, 3} row[0], and {4, 5, 6, 7, 8} row[1]. And let's call this a 5x3 hexmap. The easiest way to create this array is with malloc(sizeof(Node) * width * height).
Connecting the nodes
First of all let me point out that every even row (0, 2, 4, ...) will have width-1 elements. But there's more, each element (x, y) on this row will link to the following element in your array:
(x+1, y-1) - NORTHEAST
(x+1, y) - EAST
(x+1, y+1) - SOUTHEAST
(x, y+1) - SOUTHWEST
(x-1, y) - WEST
(x, y-1) - NORTHWEST
Elements on the other rows, such as {4, 5, 6, 7, 8} will have width elements, where element (x, y) links to the following:
(x, y-1) - NORTHEAST
(x+1, y) - EAST
(x, y+1) - SOUTHEAST
(x-1, y+1) - SOUTHWEST
(x-1, y) - WEST
(x-1, y-1) - NORTHWEST
When trying to link (x1,y1) with (x2, y2), ensure that 0 <= x < width and 0 <= y < height.
Remember ...
Your array contains one unused element at the end of every two rows (row[0], row[2], etc.). Also you might want to provide them all with some sort of label or index so that you can refer the player to them. You could label them as (x,y) pairs, or numerically by their index, it's all up to you. The (x, y) pair is very easy for you since that will map directly to the array they are stored in.