Create your own version of a Vector class that can be used as a replacement for the built-in p5.Vector class in three sketches.
Duplicate this Sketch and implement: • The constructor o Parameters: x (int or float), y (int or float) o Assigns parameters to member variables. • .add(other) o Parameters: other (Vector) o Adds the other vector to itself. Mutates member variables. • .equals(other) o Parameters: other (Vector) o Compares its members variables with other vector’s members. o Return: False if other is not an instance of a vector. Then true if vectors are identical, else false.
Duplicate this Sketch and implement: • Easy mode methods plus: • .sub(other) o Parameters: other (Vector) o Subtracts the other vector from itself. Mutates member variables. • .mag() o Parameters: none o Returns: Magnitude of vector as int or float.
Duplicate this Sketch and implement: • Medium mode methods plus: • .mult(factor) o Parameters: factor (int or float) o Multiples both of its vector component by the factor. Mutates members variables. • .div(denominator) o Parameters: denominator (int or float) o Divides both of its vector components by the denominator. Mutates members variables. • .limit(maximumMagnitude) o Parameters: maximumMagnitude (int or float) o Does nothing if maximumMagnitude is greater than actual magnitude. o Otherwise, limits magnitude to maximumMagnitude.
Duplicate your completed hard mode sketch and implement: • .setMag(magnitude) – Research the actual .setMag() to see what needs to be implemented. • .dist(other) – Research the actual .dist() to see what needs to be implemented. • Add tests for these methods to simple_test.js. • Add functionality to the Mover class that makes use of your new .setMag and .dist methods
Easy mode is pretty easy to implement as it simply is just to create a vector without having to use the createVector function that is built in the p5.js. I finished it pretty quickly. Medium mode is decent as you would need to add more functions in to the vector and the subtract function is very similar to the addition function. In this part of the challenge, a magnitude value is required. This means that I would have to get the length of the vector. I had to google the formula for it because I forgot what it was. The formula is sqrt of a squared plus b squared which is the formula for getting the Pythagorean theorem. Next, the hard mode was easy at first because multiply and divide is just the same as the addition and subtraction. The hard part was to set a limit to the vector and this took me hours to solve. Turns out, you just have to check if the value of the magnitude is greater than the maximum magnitude and if it is, just set the magnitude as the maximum magnitude. Lastly, the extra hard mode which includes the .setMag function and the .dist function. The .setMag function's process is to have to get the normalized value of magnitude first and then multiply it to the value of the maximum magnitude that you are setting. On the other hand, the .dist function is about getting the distance of two vectors. In order to get the distance, you will have to get the square root of the 2nd power of the two x-axis values and the 2nd power of the two y-axis values. Once I was able to get the code working, I needed to implement them into the mover sketch which was the hardest part. What I have done, is I just checked the distance of the movers of point x and point y. If it reaches a certain distance, it would show the mover in a different color. Same goes with the .setMag as once the magnitude of the vector is set to a certain value, the color of the mover changes as well.
This is one rough challenge and I was able to solve it with the help of fellow programmers as well as the Nature of Code book.
Since I have briefly discussed how I implemented the functions required, I would like to share my thoughts on the challenge.
This challenge is a good exercise for people who are starting with Javascript for graphics. I did enjoy some parts of it but it was really hard to implement if you have not been engaged with mathematics. I do suggest to read the Nature of Code book as it does not only discuss about programming but also discusses math concepts and physics as well.