简体   繁体   中英

c++ normalize a vector to a double?

I am trying to follow an algebraic equation, and convert it to c++.

I am stuck on:

Calculate the radius as r = ||dp||

where dp is a vector, and:

 dp = (dx,dy)

According to my google searching, the vertical bars in r = ||dp|| mean I need to normalize the vector.

I have:

std::vector<double> dpVector;
dpVector.push_back(dx);
dpVector.push_back(dy);

How should I be normalizing this so that it returns a double as 'r'?

||dp|| is the euclidean norm of the vector dp. Take a look at this link for a more complete explanation:

https://en.wikipedia.org/wiki/Euclidean_distance

The euclidean norm is computed as follow: ||dp|| = sqrt(dp.dp) ||dp|| = sqrt(dp.dp) , where . represents the dot product.

In C++, this would equate to ||dp|| = std::sqrt(dx*dx + dy*dy) ||dp|| = std::sqrt(dx*dx + dy*dy) . If dp had more dimensions, you would be better off using a linear algebra library for the dot product.

A normalized vector is one that has a length of 1, that is not what you want if you are looking for a length. Calculating the length is the first step for normalizing a vector, but I don't think you need the final step!

To calculate the length you need Pythagoras's Theorem. I'm not going to go into a full description but basically you take the square root of the square of both sides.

In other words multiply dx and dy by themselves, add them together, then square root the result.

r = std::sqrt(dx*dx + dy*dy);

If you really did want to normalize the vector then all you do as the final step is to divide dx and dy both by r. This gives a resulting unit vector of length 1.

You're probably looking for the euclidean norm which is the geometric length of the vector and a scalar value.

double r = std::sqrt(dx*dx + dy*dy);

In contrast to that, normalization of a vector represents the same direction with it length (its euclidean norm ;)) being set to 1. This is again a vector.

Fixed-dimensional vector objects (especially with low dimensionality) lend themselves to be represented as a class type.

A simple example:

namespace wse
{
    struct v2d { double x, y; };
    inline double dot(v2d const &a, v2d const &b) 
    { 
        return a.x*b.x + a.y*b.y; 
    }
    inline double len(v2d const &v) { return std::sqrt(dot(v,v)); }
}

// ...

wse::v2d dp{2.4, 3.4};
// ... Euclidean norm:
auto r = len(dp);
// Normalized vector
wse::v2d normalized_dp{dp.x/r, dp.y/r};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM