Home c++ How do I rotate a 2D vector by 45 degrees?

# How do I rotate a 2D vector by 45 degrees?

Author

Date

Category

How to implement rotation of a two-dimensional vector by 45 degrees, found solutions for 90, 180, 270. Such rotation can be realized only through the rotation matrix?

Another question regarding the further use of vector elements

``````int E;
cout & lt; & lt; "Input E";
cin & gt; & gt; E;
for (int y = 0; y & lt; col; ++ y)
{
if ((array [y] [y] == (min - E)) || (array [y] [y] == (min + E)))
{
cout & lt; & lt; "Element #" & lt; & lt; y + 1 & lt; & lt; ": match";
}
else {
cout & lt; & lt; "Element #" & lt; & lt; y + 1 & lt; & lt; ": not match";
}
}
``````

In this case, the values ​​are simply checked for matches with the condition (which is logical), but how to implement that the values ​​that are suitable were used for further calculations?

To rotate the 2D vector `(x, y) `by the angle `a `, you need to do the following calculations (note that `a `here is measured in radians, not degrees):

``````cs = cos (a);
sn = sin (a);
rx = x * cs - y * sn;
ry = x * sn + y * cs;
``````

Accordingly, in the case of a 45 degree rotation, the sine and cosine are `sqrt (2) / 2 `, so the resulting vector `(rx, ry) `can be found like this:

``````cs = sn = sqrt (2) / 2;
rx = x * cs - y * sn;
ry = x * sn + y * cs;
``````

It is extremely difficult to answer your second question about the use in further calculations, because it is not clear what it consists of. Perhaps you want to add values ​​that match the conditions to a list so that you can process them later. Explain what exactly is needed here.

There are 2 ways to filter out unnecessary elements:

1. Filtering an existing dataset. To do this, the standard library has a function remove_if
2. Create a new set based on this one. To do this, use copy_if

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.