USE OF CLASS "PVector" IN EXAMPLES WITH PROCESSING AND JAVA
The "PVector" class is used to represent vector quantities in 2D plane and 3D space. A vector object is created as follows:
PVector M = new PVector(300,-80,200);
|
(see figure 1)
|
or for a vector in the plane:
PVector M = new PVector(300,-80);
To print those coordinates we use the command System.out.println():
println("Point M("+M.x+","+"M.y+","+M.z+")");
Or in the plane, if Z=0
To print data about this vector you can using println() method:
println("Point M("+M.x+","+"M.y")");
Therefore, the position of point M is determined by the vector that connects the coordinate origin with point M. In the above-mentioned way, a new vector object is created, i.e. new memory is taken for it, but if we want to download only a copy then we would do it by calling the copy() method(or get() method which is depracated) like e.g.
PVector M = new PVector(100,200);
PVector MCopy= M.copy();
PVector MCopy= M.copy();
The "PVector" class is used to represent vector quantities in a 2D plane or 3D space. A vector object is created as follows:
PVector M = new PVector(300, -80, 200); // For a vector in 3D space
PVector N = new PVector(300, -80); // For a vector in the 2D plane
Addition of vectors using PVector in processing
Objects of class PVector can represent any vector quantity in plane or space. The rules that apply to vectors can also be applied here. E.g. to add two vectors, we can do it by adding one to the other and then the resulting vector is a vector that starts at the beginning of the first and the end coincides with the end of the second, which can be seen in Figure 3.
The addition of two vectors is shown in the following figure:
The addition of two vectors is shown in the following figure:
Here are two ways to add the vectors a and b and get the resultant c. Both methods are used to keep the initial vector a unchanged. If you would just write:
PVector c;
a.add(b);
a.add(b);
We would get vector a as the sum of vector b and itself, so that vector a would be changed, ie we would not keep the original vector.
On the other hand, if we call the add method via the PVector class, i.e. statically, adding a vector will not affect the vector a(initial vector). So if we write:
On the other hand, if we call the add method via the PVector class, i.e. statically, adding a vector will not affect the vector a(initial vector). So if we write:
PVector c = PVector.add(a,b);
Both vector a and vector b will remain unchanged, while vector c will be the sum of these two vectors.
Subtraction of two vectors in processing:
The subtraction of two vectors is explained in the following figure:
Let's look at two vectors a and b defined as in Figure 4. First, a copy of the vector a is created using the get() method. Then the vector b is subtracted from the copy c using the sub() method, to which the mentioned vector b is passed as a parameter. Creating a copy avoids changing the original vector a. In the picture you can see that the resultant vector c (colored red) is actually a vector obtained by adding vector a and vector (-b) which is connected to a. The minus sign in front of b means that it is a vector equal to the magnitude(intensity) of vector b, but have the opposite direction.
Another way to avoid changing the original vector a is to use the PVector class, which can also be seen in the figure.
Another way to avoid changing the original vector a is to use the PVector class, which can also be seen in the figure.
Multiplying a vector by a scalar in processing (mult method)
This is explained in the following image:
If the vector a was defined and we wanted to increase its intensity, say 2 times, then we could write:
PVector a= new PVector(20,-10);
a.mult(2);
a.mult(2);
This would increase the intensity vector a twice, but would not retain the original vector. To do this, we first need to make a copy of the vector, as shown in Figure 5.
Dividing a vector by a scalar in processing (div method)
If we want to divide the intensity value by some number, then we could do it as shown in the following image:
Here, too, the situation is similar to multiplying a vector by a scalar, only instead of the mult method, the div method is used. Also, if it is necessary to keep the original vector, in this case marked b, unchanged, it can be done in two ways as explained in Figure 6.
Reducing the vector to a unit vector in processing (normalize). Determination of the intensity of the given vector (mag). Vector rotation (rotate).
These three methods are explained in the following image:
If we want to determine a vector that has the same direction as another, known vector, we can reduce that known vector to a unit one that has the same direction, but the intensity is 1 (normalize method), and then, let's say, multiply it by the intensity, which is known and so on determine the mentioned vector. This will be covered in more detail in the example of friction (see the next lessons).
For example, the direction and direction of the friction force can always be determined if the velocity vector is known, as mentioned above.
The mag() method determines the intensity of a known vector, while the rotate() method rotates the known vector by an angle around an axis that passes through the current coordinate origin in P2D or P3D koo. system.
For example, the direction and direction of the friction force can always be determined if the velocity vector is known, as mentioned above.
The mag() method determines the intensity of a known vector, while the rotate() method rotates the known vector by an angle around an axis that passes through the current coordinate origin in P2D or P3D koo. system.
Determination of the angle between two vectors in processing
If two vectors are given in processing, the angle between them in radians can be determined using the angleBetween() method of the PVector class object, as shown in the following image:
Next
Operations with vectors >| |