RosettaCodeData/Task/Vector/Arturo/vector.arturo

35 lines
672 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
define :vector [
2026-02-01 16:33:20 -08:00
init: method [x,y][
this\x: x
this\y: y
]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
add: method [that :vector][
to :vector @[this\x + that\x, this\y + that\y]
]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
sub: method [that :vector][
to :vector @[this\x - that\x, this\y - that\y]
]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
mul: method [n :integer][
to :vector @[this\x * n, this\y * n]
]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
div: method [n :integer][
to :vector @[this\x // n, this\y // n]
]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
string: method [][
render "(|this\x|, |this\y|)"
]
2023-07-01 11:58:00 -04:00
]
; test our vector object
a: to :vector [5 7]
b: to :vector [2 3]
2026-02-01 16:33:20 -08:00
print [a '+ b '= a + b]
print [a '- b '= a - b]
print [a '* 11 '= a * 11]
print [a '/ 11 '= a / 2]