Vector: Difference between revisions

702 bytes added ,  4 years ago
→‎{{header|Processing}}: adding Processing Python mode
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(→‎{{header|Processing}}: adding Processing Python mode)
Line 1,379:
A vector class, PVector, is a Processing built-in. It expresses an x,y or x,y,z vector from the origin. A vector may return its components, magnitude, and heading, and also includes .add(), .sub(), .mult(), and .div() -- among other methods. Methods each have both a static form which returns a new PVector and an object method form which alters the original.
 
<lang Processingjava>PVector v1 = new PVector(5, 7);
PVector v2 = new PVector(2, 3);
 
Line 1,409:
[ 2.0, 3.0, 0.0 ]
</pre>
 
==={{header|Processing Python mode}}===
 
Python mode adds math operator overloading for static methods.
 
<lang python>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)</lang>
 
=={{header|Python}}==
Anonymous user