Vector: Difference between revisions

1,086 bytes added ,  1 month ago
m
Update Lang example: Use new struct definition syntax
(Added Oberon-2)
m (Update Lang example: Use new struct definition syntax)
 
(6 intermediate revisions by 3 users not shown)
Line 704:
(55,0 + i77,0)
(2,5 + i3,5)
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func[] vadd a[] b[] .
for i to len a[]
r[] &= a[i] + b[i]
.
return r[]
.
func[] vsub a[] b[] .
for i to len a[]
r[] &= a[i] - b[i]
.
return r[]
.
func[] vmul a[] b .
for i to len a[]
r[] &= a[i] * b
.
return r[]
.
func[] vdiv a[] b .
for i to len a[]
r[] &= a[i] / b
.
return r[]
.
print vadd [ 5 7 ] [ 2 3 ]
print vsub [ 5 7 ] [ 2 3 ]
print vmul [ 5 7 ] 11
print vdiv [ 5 7 ] 2
</syntaxhighlight>
 
{{out}}
<pre>
[ 7 10 ]
[ 3 4 ]
[ 55 77 ]
[ 2.50 3.50 ]
</pre>
 
Line 1,443 ⟶ 1,483:
=={{header|Lang}}==
<syntaxhighlight lang="lang">
struct &Vector = {
$x
$y
Line 1,449 ⟶ 1,489:
 
fp.initVector = ($x, $y) -> {
return fn.structOf(&Vector, (fn.double($x), fn.double($y))
}
 
fp.addVector = ($a, $b) -> {
return parser.op(fn.structOf(&Vector, ($a::$x + $b::$x, $a::$y + $b::$y))
}
 
fp.subVector = ($a, $b) -> {
return parser.op(fn.structOf(&Vector, ($a::$x - $b::$x, $a::$y - $b::$y))
}
 
fp.mulVector = ($vec, $scalar) -> {
return parser.op(fn.structOf(&Vector, ($vec::$x * $scalar, $vec::$y * $scalar))
}
 
fp.divVector = ($vec, $scalar) -> {
return parser.op(fn.structOf(&Vector, ($vec::$x / $scalar, $vec::$y / $scalar))
}
 
Line 3,413 ⟶ 3,453:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">class Vector2D {
construct new(x, y) {
_x = x
Line 3,459 ⟶ 3,499:
v1 / 2 = (2.5, 3.5)
</pre>
 
{{libheader|Wren-vector}}
Alternatively, using the above module and producing exactly the same output as before:
<syntaxhighlight lang="wren">import "./vector" for Vector2
 
var v1 = Vector2.new(5, 7)
var v2 = Vector2.new(2, 3)
var v3 = Vector2.fromPolar(2.sqrt, Num.pi / 4)
System.print("v1 = %(v1)")
System.print("v2 = %(v2)")
System.print("v3 = %(v3)")
System.print()
System.print("v1 + v2 = %(v1 + v2)")
System.print("v1 - v2 = %(v1 - v2)")
System.print("v1 * 11 = %(v1 * 11)")
System.print("11 * v2 = %(Vector2.scale(11, v2))")
System.print("v1 / 2 = %(v1 / 2)")</syntaxhighlight>
 
=={{header|XPL0}}==
168

edits