vector problem

1
2
3
double length_of_vector(double *a){
    return(sqrt(sqrt((a[0]*a[0]+a[1]*a[1])*sqrt(a[0]*a[0]+a[1]*a[1]))+a[2]*a[2]));
}


This function computes the length between 0 and point a (x, y, z).


vector <double> b[3];

Can I use the function to compute the length between 0 and point b?

If so how?
Your getting vectors mixed up a bit here.

The STL vector is a container, not to be confused with the mathematical vector; an entity which has magnitude and direction.

If you had a vector named 'b', presumably you could just pass it into that function.

Have to say, I think there are better ways than this. A 3DVector class with x, y and z elements would be a lot nicer.
Last edited on
b is an array of 3 vector<double>. Is vector here std::vector? std::vector is not really optimal as a geometric vector.
This function computes the length between 0 and point a (x, y, z).

It does? Looks like there are a few sqrts too many...

Can I use the function to compute the length between 0 and point b?

Depends on what point b is.

Also, you should create a class for vectors/points.
should i declear a vector like this

1
2
3
class vector{
     double x, y, z;
}
I would name it something else.

And add some functions. CalculateMagnitude, Normalize. Stuff like that.
I think the function works though
I wouldn't be so sure. Like Athar says, looks like too many sqrt functions.

Whenever I work out the magnitude of a vector, it's the following formula:

|v| = sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z))
Topic archived. No new replies allowed.