Vector or screen displacement

Pages: 12
Hi Guys,

I'm reading Stephen Prata's C++ primer plus, I'm 535 pages deep into the book, so far It's a great book clear and precise explanations with excellent examples but I've finally run into the first chapter that makes me wonder if this notion is correct,

so a vector (not to be confused with std::vector, also a 2d vector) has both a length( size ) and direction, we can represent a screen displacement using a vector, Stephen mentions a vector has both an x co ordinate and y co ordinate, this doesn't seem right to me,

a vector is basically two points , but both points have an x value and also a y value, but in Stephen's code he represents a vector with just an x value and y value.

1
2
3
4
5
6
7
8
9

  class Vector{
    
     double x;
     double y;

     // rest of code
  }


is this correct or am I correct thinking this? I mean to represent a vector on a screen wouldn't you need both the x,y coordinates of both points?

Thanks
Last edited on
An x,y pair is a valid way of representing a vector. It's implied that it's with respect to the origin of your coordinate system. So the length in your case would be sqrt(x*x + y*y), and the angle is atan2(y, x).

The displacement between two points (ax, ay), (bx, by) can be represented by a vector (B - A),
i.e. < bx - ax, by - ay>
Last edited on
so the first point would be x = 0 and y = 0 and the second point would be x = ( user input ) y = ( user input ) ?
Can you clarify, the first point of what? I wouldn't overcomplicate it too much: A vector is just a way to represent a magnitude and a direction. This can be done with an <x, y> representation or with an <magnitude, angle> representation. Have you had physics/math classes that have vectors in them?

If you're talking about displacement, there is no longer a concept of points.
- If you travel from New York City to London, your displacement is ~3,000 miles.
- If you travel from (100 miles north of New York City) to (100 miles north of London), your displacement is still ~3,000 miles.
Last edited on
By definition, a vector is an element of a vector space. A vector space is a cartesian product of a scalar set by itself some number of times (usually 2 or 3), with two operations: addition of vectors and multiplication of a vector by a scalar.
So, a simple vector space would be (R^2, +, *), where each vector is made up of two real numbers, and two vectors can be added, and any vector can be multiplied by a real.

So, no. A two-dimensional vector does not consist of two points, it consists of two numbers and it is itself used to represent a point. So, for example, (0, 0) would be a vector and it could represent the top-left corner of the screen, while (1920, 1080) could represent just outside the bottom-right corner of the screen.

PS: A set is called "scalar" when it is used as the basic set to construct a vector space on top of it. The set of all rational polynomials could be scalar, for example, because a vector space can be constructed on top of it. This is wrong. Polynomials form a ring, not a field.
Last edited on
another way to say this is that a vector could be represented like this: it starts at 0,0 and is 3 units long at 33.33 degrees, you can draw that. Does he have more info in the class about its direction and length? And 0,0 is not critical; it could start at 11, -23 and you can still draw that.

You also have to beware of jargon. Not everything referred to as a vector has a 3-d space direction. A column vector of a matrix, for example, is not what a physics guy would call a vector. It may be safer, when looking at code to solve a problem, to ask 'does this thing contain the information needed to solve this problem'. I know tons of times in mathy code I have left out 'known' or unimportant information and just had what I needed for that particular code.
Last edited on
And 0,0 is not critical; it could start at 11, -23 and you can still draw that.
True, however the information of 11,-23 is external to the vector itself. (The reason I brought up the origin was that adam seemed to think a vector requires two points; it doesn't, as helios already mentioned.)

And a column vector of a matrix is essentially the same as how you would use it in physics, it's just you need to be consistent with what each row represents.
Last edited on
sure, but it may not have a true 'direction' that you can feed to the cos() function when talking about a matrix. you can probably give it one with math, but it may not 'mean' anything in real 3-d space.

yes, the starting point is not important either. Comes back to my main point: does the c++ class have what it needs to correctly solve some problem is the key. If it does, the class is fine, even if it turns out to be a little fast and loose on the math side from a formal perspective.
That's fine, but you should name your classes thoughtfully to avoid confusion. A Vector class containing an origin Point and an offset or final Point would just be weird, even if it does solve your problem. Call it what it is: a segment.
oh ok, starting to make sense, ... I think

so with Vectors, the tip of the vector always starts at the origin?? in a computers case or screen displacement generally this would be the top left corner of the screen? at least with SDL anyway.

can a vector start at a point that is not the origin? and if it does wouldn't you also need to know the x and y co ordinates of where the tip is and where the end of the vector is?

thanks
adam2016 wrote:
so with Vectors, the tip of the vector always starts at the origin??

No, a vector does not necessarily start at the origin.

(I always thought the tail of a vector would be at the origin, rather than the tip/head?)

A vector used to describe a position -- on the screen or wherever -- is called (appropriately enough) a position vector. And it will be defined with respect to some or other origin.

A displacement vector, on the other hand, represents a change of position and starts at the position it is applied. If you add a displacement vector to a position vector you end up with a new position vector.

<position vector> + <displacement vector> = <new position vector>

(I am restating a bit here!?)

See here from some illustrations: Vectors
https://www.mathsisfun.com/algebra/vectors.html

Amongst assorted other type of vectors, there's the velocity vector:

<velocity vector> x <time duration> = <displacement vector>

Andy
Last edited on
I get the basic idea of vectors, how to add them and multiply them ( scale them )

but shouldn't the end of the vector ( not the tip my bad ) always start at the origin? from what I have read it seems like it does except for when you add two vectors A and B, when you want to add A and B you move the end of B to the tip ( or arrow head ) of A this means you add A.x + B.x and B.y + B.x right?

but both ends of the vectors start at the origin? I mean if that didn't wouldn't you need two sets of co ordinates ?? one for the end of the vector and one for the tip ( or arrow head ) of the vector?? ( or is this just with displacement vectors? and if so we are mainly concerned with displacement vectors in things like screen movement correct? )

thanks
Last edited on
A vector is just N ordered values, where N is the dimension if the vector. You can *visualize* a vector by placing its tail at the origin, but that isn't part of the definition itself. Hope that clears up my first post.

If you put the vectors "tip to tail", that's a good way of visualizing vector addition.

So if you have A and B, and attach B onto A, the resultant vector A + B will have the same tail as A, but the tip will be the sums of each dimension of A and B.

https://www.khanacademy.org/science/ap-physics-1/ap-two-dimensional-motion/analyzing-vectors-using-trigonometry-ap/v/visualizing-vectors-in-2-dimensions
Last edited on
but with screen displacement vectors since we only have an x and y co ordinate the tail of the vector always starts conceptually at the origin right?

but I do get the concept of vectors, please correct me if I'm wrong, here's my understanding,

lets say we have an object A moving straight vertically on the screen and another object B lets say moving slower (magnitude) hits into our object A, this will knock A of it's course a little but where does it knock it of course???? well I'm guessing we would add the x and y components of object B to the x and y components of object A and this will give object A it's new trajectory right?

but going back to screen displacement again we will normally start at the origin of the screen or x = y = 0, so that is why the author just represents the vector with just one set of x and y co ordinates??

Thanks Ganado :)
Last edited on
but with screen displacement vectors since we only have an x and y co ordinate the tail of the vector always starts conceptually at the origin right?
You're thinking of the classical way to depict a vector graphically, which is an arrow with a beginning and an end. A 2D vector is simply two numbers. That's it. That's all it is. It doesn't start somewhere or end somewhere.
For example, where does the number 9 start? The question is meaningless, right? A real number is just an infinitesimally tiny element of the real set. It doesn't have extension. Likewise, the vector (9, 5) doesn't start anywhere. It's just an element of a set, with no size.

Now, a vector can be used to represent different things (just like regular numbers). It may represent a displacement, a position, a direction, etc. If we're talking about displacement then it's incorrect to say that it's relative to the origin. Displacement is shifting all points of a plane by the same amount. Therefore displacement is relative to the current positions of every point in the plane.

lets say we have an object A moving straight vertically on the screen and another object B lets say moving slower (magnitude) hits into our object A, this will knock A of it's course a little but where does it knock it of course???? well I'm guessing we would add the x and y components of object B to the x and y components of object A and this will give object A it's new trajectory right?
Err... Sure, you can model such an interaction like that. You're not ignoring several factors such as the shape, masses, rotation of the objects and the angle of incidence of the collision (a head-on collision affects the trajectories differently than a grazing collision) so the model will be quite unrealistic, but yes, you can model the situation like that.

but going back to screen displacement again we will normally start at the origin of the screen or x = y = 0, so that is why the author just represents the vector with just one set of x and y co ordinates?
No, they represent vectors as a single pair of coordinates because that's what vectors are.
You're thinking of the classical way to depict a vector graphically, which is an arrow with a beginning and an end. A 2D vector is simply two numbers. That's it. That's all it is. It doesn't start somewhere or end somewhere.
For example, where does the number 9 start? The question is meaningless, right? A real number is just an infinitesimally tiny element of the real set. It doesn't have extension. Likewise, the vector (9, 5) doesn't start anywhere. It's just an element of a set, with no size.


yeah I think my concept of a vector is wrong, so a vector is not used to represent objects?

a 2d vector contains 2 numbers and x value and y value, this seems just like a point(x,y) to me?

why are vectors typically depicted graphically with a tail and an arrow head( head ) pointing in the direction it is going?
so a vector is not used to represent objects?
I'm not sure what your question is here. If "the velocity of a car", or "the force on a spring", or "the direction of the electric field" are objects, then yes, a vector can be used to represent objects.

It's an abstract mathematical concept; you give it meaning by applying it to something.

why are vectors typically depicted graphically with a tail and an arrow head( head ) pointing in the direction it is going?
One reason is that mathematicians and engineers love drawing lines. That's why in discrete signal processing, you'll see lines drawn from the time/sample axis to the y coordinate. It doesn't mean anything, it just stands out more than a dot.

But other times, it in fact does help to convey meaning. Let's say you have a vector field that shows how fast water is moving at a particular location. Each location has a vector associated with it, where the vector is the direction/speed that the water is moving at that location. If you only showed the vector as a dot, you wouldn't be able to convey that the water is moving faster at one location as opposed to another location, or what direction the water is flowing at a particular point. Drawing the tails/arrows helps show this information.
https://en.wikipedia.org/wiki/Vector_field

Perhaps a simpler example: If the vector represents velocity of an object, then the direction the vector points clearly shows which direction the object is moving, and the length of the vector represents the magnitude (speed) of the movement.

(x, y) could mean "the x component of the velocity, the y component of the velocity",
or it could mean "amount of apples, amount of bananas", or it could mean "the man's latitude, longitude location".

It's just information; you give it meaning by applying it to something.
Last edited on
a 2d vector contains 2 numbers and x value and y value, this seems just like a point(x,y) to me?
They're similar. A point is a geometric shape. A vector is similar to a point in that it has no extension (no size). A vector is in a way more abstract than a point, because it doesn't necessarily represent anything geometrical. A vector can represent the location of a point, but it could represent, say, a color.
But yes, if we're talking exclusively about 2D and 3D geometry "vector" and "point" can be used more or less synonymously, as long as you keep clear in your mind that you're talking about a position.

why are vectors typically depicted graphically with a tail and an arrow head( head ) pointing in the direction it is going?
Because probably their most common application is to represent the velocity of an object. The velocity is also known as the motion vector of the object. In fact in aerospace jargon they just call it "vector". Since the vector is representing motion, it's useful to depict that as an arrow, like saying "this is going to move that way". The arrow could be placed anywhere in the diagram and it would still be correct, since like I said, a vector doesn't have a beginning. However, since the point of those diagrams is to visualize information clearly and intuitively, it's more useful to place the arrows originating from the object in question.

An unfortunate side-effect is that, since classical mechanics is a subject taught earlier in school than linear algebra, most people learn about vectors first from these diagrams and then think that vectors and arrows are the same thing.
The simple explanation of all this is the x and y referred to in the Vector class are the x and y components of the vector. The (x,y) pair in the case of the Vector is not a point and nothing like it - a point has neither magnitude nor direction.

In the case where 0,0 is (implicitly) used to describe the vector associated with a position x, y on a screen, the Vector(x,y) is called the Position Vector of that point on the screen.

A Displacement Vector describes the components of movement from some previously defined starting point.

For example if you are at Point (7,9) on the screen and you apply a Displacement (3,19), then your new position will be (10,28).

Or, (Position) Vector(7,9) + (Displacement) Vector(3,19) gives a new (Position) Vector(10,28)

Vectors can describe positions, displacements, velocities, accelerations, turbulence, forces of all sorts, and anything else having magnitude and direction, not just, or even mainly, velocity.
Another way of doing the same depending what the starting position is:
Find vector AB, given:
A start(x,y) coordinates (10,10) - this is a point, or the Position Vector of the start
B end(x,y) coordinates (17,19) - this is a point, or the Position Vector of the end

Thus, vector AB(7,9) as before. (just subtract the coordinates)

Vector Displacement D(3,19) as given

CD = AB(7,9) + D(3,19)

So, CD(10,28) as before

Pages: 12