How to resolve the algorithm Vector step by step in the Processing programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Vector step by step in the Processing programming language
Table of Contents
Problem Statement
Implement a Vector class (or a set of functions) that models a Physical Vector. The four basic operations and a pretty print function should be implemented.
The Vector may be initialized in any reasonable way.
The four operations to be implemented are:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Vector step by step in the Processing programming language
Source code in the processing programming language
PVector v1 = new PVector(5, 7);
PVector v2 = new PVector(2, 3);
println(v1.x, v1.y, v1.mag(), v1.heading(),'\n');
// static methods
println(PVector.add(v1, v2));
println(PVector.sub(v1, v2));
println(PVector.mult(v1, 11));
println(PVector.div(v1, 2), '\n');
// object methods
println(v1.sub(v1));
println(v1.add(v2));
println(v1.mult(10));
println(v1.div(10));
v1 = PVector(5, 7)
v2 = PVector(2, 3)
println('{} {} {} {}\n'.format( v1.x, v1.y, v1.mag(), v1.heading()))
# math overloaded operators (static methods in the comments)
println(v1 + v2) # PVector.add(v1, v2)
println(v1 - v2) # PVector.sub(v1, v2)
println(v1 * 11) # PVector.mult(v1, 11)
println(v1 / 2) # PVector.div(v1, 2)
println('')
# object methods (related augmented assigment in the comments)
println(v1.sub(v1)) # v1 -= v1; println(v1)
println(v1.add(v2)) # v1 += v2; println(v2)
println(v1.mult(10)) # v1 *= 10; println(v1)
println(v1.div(10)) # v1 /= 10; println(v1)
You may also check:How to resolve the algorithm Sorting algorithms/Strand sort step by step in the Go programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Slate programming language
You may also check:How to resolve the algorithm Introspection step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Visualize a tree step by step in the Python programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Go programming language