CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Getting a vector length and angles

    Hi maybe this is not really belong in a c++ forum, but since im using C++ i might as well put it here.

    I am programming a Vector class, which has the vertex from A to B.
    I must be able to get the angles between A and B and the length of the vector.Meaning that if i follow these angles as a line from point A i must get to point B.

    i have tried google but i keep getting stuff like triangles. I just cant get the right information pages. And i never had this type of algebra/ math at school so i dont know where to start.

    can anyone point me in the right direction?

    here my code, its obviously far from complete.

    Code:
    	template<typename T>
    	struct CVectorT
    	{
    		typedef T VertexT;
    		
    		typedef typename VertexT::ValueT ValueT;
    
    		VertexT A;
    		VertexT B;
    
    		inline CVectorT(VertexT a = VertexT(), VertexT b = VertexT())
    			:A(a), B(b)
    		{
    		}
    
    		inline VertexT Angles() const
    		{
    			VertexT angles;
    			return angles;
    		}
    
    		inline ValueT Length() const
    		{
    		}
    	};

  2. #2
    Join Date
    Oct 2006
    Posts
    616

    Re: Getting a vector length and angles

    I'm not really sure which angles you want or even if you really want angles and not something else.

    I'm going to write what I *think* I understand from your post.
    1. You have a vector class V which is initialized by 2 vertices A and B.
    2. This vector, V, should get you from point A to point B, meaning that:
    Code:
    A + V = B
    To achieve 1 & 2, you must subtract A from B:
    Code:
    V = B - A = (B.x - A.x, B.y - A.y, B.z - A.z)
    3. To find the length of a vector V, you can use this formula(for 3D vector):
    Code:
    length(V) = sqrt( (V.x)^2 + (V.y)^2 + (V.z)^2 )
    sqrt = square root
    y^x = y raised by the power of x.

    If I misunderstood your question, please correct me.

    Regards,
    Zachm

  3. #3
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Getting a vector length and angles

    hi, im really confused.

    how can that be the length of the vector? maybe i try to explain otherwise. i have two 3d coordinate. i must get the distance between the two.. a 3d coordinate is called vertex right?

    I get really confused because i keep seeing all kind of different things for vertex and vector.. Im going insane...

    as for the angles... i think the right word is the rotational angles.. if i were to add 3d coordinate A and length and the angles it will give me point B. i might as well store Vector as 3d coordinate and length and angle but than i will end up having one more variable.

    thanks alot for your time

  4. #4
    Join Date
    Mar 2009
    Posts
    94

    Re: Getting a vector length and angles

    a vertex cannot go from point A to point B. A vector can.
    A vertex is a corner point http://en.wikipedia.org/wiki/Vertex_(geometry)
    But the design of your class looks like it is a vector going from one vertex to another. That would be fine.

    So you could say a vertex is more than just 3D coordinates because it is created by the intersection of two vectors. The angle would be between those vectors, right?

    So you want to know, given a vector between two vertices:
    - what is the angle between the last vector and this vector and
    - what is the angle between this vector and the next.

    correct?

    Zachm is right about the distance. Why should this not be the distance? It's just the Pythagorean theorem in 3D.

  5. #5
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Getting a vector length and angles

    A vector can result by subtracting two 3d points.

    x = x1 - x2;
    y = y1 - y2;
    z = z1 - z2;

    The result is a vector x,y,z.

    The length of that vector is as stated

    sqrt( x * x + y * y + z * z );

    (which should jump out at you as the distance between the two points used to make the vector)


    A vector can be added to a point, to get a destination point.

    vector1 = point1 - point2;

    point2 = point1 + vector1;


    I empathize with the confusion between vertex and vector. In some 3d study, you're encouraged to consider any 3d point AS a vector from the origin, which, essentially, is a valid point of view.
    Last edited by JVene; July 11th, 2009 at 05:03 PM.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  6. #6
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Getting a vector length and angles

    yes your right. i got confused with his formula cause he did not substract two vertices. before doing pythagoras.

    as for the angle between two points not two vectors.

    thanks alot for your reply.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Getting a vector length and angles

    The word "vertex" is usually used in relation to graphs.....for general coordinate systems, you should stick to the words "vector" and "point".

    So a collection of vectors and their endpoints could be represented by a graph of vertices and edges, but the two are different views of the situation---trying to mix up the terminology is just going to confuse you.

    As far as "angles" go, it doesn't make sense to talk about those unless you have more than one vector. If you do, the law of cosines (Google it) can be used to easily determine the angle between them.

  8. #8
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Getting a vector length and angles

    hi, i really do need to get the angle between two points, because of rotations...
    Last edited by wigga; July 11th, 2009 at 05:40 PM.

  9. #9
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Getting a vector length and angles

    All things are relative, and usually have a context.

    There are no angles between two points. That's a straight line.

    What you might be thinking is that there's an angle described by that line and it's relationship to the various coordinate axis.

    Is that what you're looking for?

    That could be 2 angles, found using trig, but they're not all that useful.

    What's the objective? Is this a "look at" feature?

    Are you familiar with linear algebra (the uses of 4x4 and 3x3 matrix operations)? That's quite likely where you're headed, but I'm not yet sure exactly what you're asking.

    Can you explain to me whats the diffrence between a vertex on graphs and a point?
    The convention of language. A vertex is a point, and the word vertex is commonly used to describe a point in the documentation for OpenGL, as an example.


    Wigga, I empathize. It may seem like we're toying around here, but we're not.

    This stuff requires careful thinking. There is a context we need to understand about what you're thinking.

    The question regarding the angle between two points is nonsensical to those of us who've studied this, which means there's something missing from your question, which you may well have in mind but haven't included.

    We can't guess at it, because there can be numerous concepts - we'd right a book guessing at it.

    For example, it could be that one point in your thinking represents an object.

    The other point might represent a target.

    Asking for the angle between two points still doesn't make sense, because that description implies at least two vectors - the vector between the source and target, and the vector describing the view - essentially the direction the nose of the observer points.

    That would be solvable.
    Last edited by JVene; July 11th, 2009 at 08:39 PM.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Getting a vector length and angles

    Maybe he's asking how to find the linear transformation that maps one point to another? That's the only interpretation of the question that I can come up with.

  11. #11
    Join Date
    Mar 2009
    Posts
    94

    Re: Getting a vector length and angles

    why dont you make a drawing of what you mean and upload it. Maybe it is easier to understand then.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Getting a vector length and angles

    Well, here's *one* thing we've been saying. I'm not sure if it's what you're confused about or not....
    http://mathforum.org/~klotz/Vectors/subtraction.html

  13. #13
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Getting a vector length and angles

    Quote Originally Posted by JVene View Post
    Is this a "look at" feature?
    it could be that, for now that is exactly the problem i am facing. But it could actually be used for other things in my program as well. I am trying to make a terrain editor actually, and being stuck at something like this, im beyond my capabilities in math. when i actually get this part down i will have to think about how i am going to store my points, keeping in mind that i have to be able to add points anywhere and move them around, and still be able to texture them, and figuring out where in 3d space i actually clicked... so yeah this is gonna take me alot of time and research on alot of things.

    The way i wanna do this now, is by getting the angles from one point to another, there is no other way i wanna do this, because i need this in the feature for other things than just a lookat. i might decide later to create a more efficient method with matrix multiplications. I need this formula for other things as well. So i geuss theres a diffrence between opengl rotation, and the actual angular diffrence between two points?

    Quote Originally Posted by JVene View Post
    Are you familiar with linear algebra (the uses of 4x4 and 3x3 matrix operations)? That's quite likely where you're headed, but I'm not yet sure exactly what you're asking.
    I know about these, but not in actual detail.


    I am sorry for all the confusion i might have created, but my brain works really weird most of the time

    Code:
    	template<typename T>
    	struct CVertexT
    	{
    		typedef T ValueT;
    
    		ValueT X;
    		ValueT Y;
    		ValueT Z;
    
    		inline CVertexT()
    			:X(0), Y(0), Z(0)
    		{
    		}
    
    		inline CVertexT(ValueT v[3])
    			:X(v[0]), Y(v[1]), Z(v[2])
    		{
    		}
    
    		inline CVertexT(CVertexT angles, ValueT distance)
    			//{0, 0, 0}/angles/distance to XYZ
    		{
    		}
    
    		inline CVertexT(ValueT x, ValueT y, ValueT z)
    			:X(x), Y(y), Z(z)
    		{
    		}
    
    		inline CVertexT operator ^(CVertexT v)
    		{
    			return CVertexT
    			(
    				Y * v.Z - Z * v.Y,
    				Z * v.X - X * v.Z,
    				X * v.Y - Y * v.X
    			);
    		}
    
    		inline CVertexT operator *(CVertexT other) const
    		{
    			return CVertexT(*this) *= other;
    		}
    		
    		inline CVertexT& operator *=(CVertexT other)
    		{
    			X *= other.X;
    			Y *= other.Y;
    			Z *= other.Z;
    			return *this;
    		}
    
    		inline CVertexT operator -(CVertexT other) const
    		{
    			return CVertexT(*this) -= other;
    		}
    		
    		inline CVertexT& operator -=(CVertexT other)
    		{
    			X -= other.X;
    			Y -= other.Y;
    			Z -= other.Z;
    			return *this;
    		}
    
    		inline CVertexT operator +(CVertexT other) const
    		{
    			return CVertexT(*this) += other;
    		}
    		
    		inline CVertexT& operator +=(CVertexT other)
    		{
    			X += other.X;
    			Y += other.Y;
    			Z += other.Z;
    			return *this;
    		}
    
    		inline CVertexT operator /(CVertexT other) const
    		{
    			return CVertexT(*this) /= other;
    		}
    		
    		inline CVertexT& operator /=(CVertexT other)
    		{
    			X /= other.X;
    			Y /= other.Y;
    			Z /= other.Z;
    			return *this;
    		}
    
    		inline CVertexT Angles() const
    		{
    			//{0, 0, 0}/XYZ to angles
    		}
    
    		inline ValueT Distance() const
    		{
    			return Math:Sqrt(X * X + Y * Y + Z * Z);
    		}
    
    		inline CVertexT& Normalize()
    		{
    			return *this /= Distance();
    		}
    	};
    
    	template<typename T>
    	struct CVectorT
    	{
    		typedef T VertexT;
    		
    		typedef typename VertexT::ValueT ValueT;
    
    		VertexT A;
    		VertexT B;
    
    		inline CVectorT()
    		{
    		}
    
    		inline CVectorT(VertexT a, VertexT b)
    			:A(a), B(b)
    		{
    		}
    
    		inline CVectorT(VertexT a, VertexT angles, ValueT distance)
    			:A(a), B(a + VertexT(angles, distance))
    		{
    		}
    
    		inline VertexT Angles() const
    		{
    			return (B - A).Angles();
    		}
    
    		inline ValueT Distance() const
    		{
    			return (B - A).Distance();
    		}
    	};
    
    	template<class T>
    	struct CCameraT
    	{
    		typedef T VertexT;
    
    		VertexT Pos;
    		VertexT Angle;
    
    		inline CCameraT()
    		{
    		}
    
    		inline CCameraT(VertexT pos, VertexT angle)
    			:Pos(pos), Angle(angle)
    		{
    		}
    
    		inline void LookAt(VertexT focus)
    		{
    			Angle = CVectorT<T>(Pos, focus).Angles();
    		}
    	};
    Last edited by wigga; July 12th, 2009 at 10:17 AM.

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Getting a vector length and angles

    So i geuss theres a diffrence between opengl rotation, and the actual angular diffrence between two points?
    The phrase "angular distance between points" is what we're trying to figure out. That phrase doesn't make any sense, so trying to divine your meaning is difficult.

    Both glRotate and glScale calls simply modify the modelview (well, the active, but typically the modelview) matrix by multiplying it by what's called a linear transformation.

    In the case of a "rigid" object, you can think of that transformation as simply moving the camera. When the modelview matrix is changed between drawing different parts of a model it gets a bit more confusing, however.

  15. #15
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: Getting a vector length and angles

    Quote Originally Posted by Lindley View Post
    Both glRotate and glScale calls simply modify the modelview (well, the active, but typically the modelview) matrix by multiplying it by what's called a linear transformation
    now tell me something i do not know. Like getting how much rotation is needed to "get from" point A to point B

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured