RosettaCodeData/Task/Vector/M2000-Interpreter/vector.m2000
2026-04-30 12:34:36 -04:00

67 lines
1.3 KiB
Text

class vector {
private:
double x, y
public:
operator "+" (b as vector){
.x+=b.x
.y+=b.y
}
operator "-" (b as vector){
.x-=b.x
.y-=b.y
}
function mul_literal(b as double){
ret=this
' x in ret is private but is accessible...
' ...from same type's member of other object.
ret.x=.x*b
ret.y=.y*b
=ret
}
function div_literal(b as double){
ret=this
ret.x=.x/b
ret.y=.y/b
=ret
}
property printVector {
value {
link parent x, y to x, y
value=format$(.fm$, str$(round(x,.r), .Lcid),if$(y>=0->"+", "-"),str$(abs(round(y,.r)),.lcid))
}
}="" // make type string
// added members to printVector (is a group type)
group printVector {
integer Lcid=1033
fm$="{0} î {1}{2} û"
r=6
}
class:
module vector(r as double, theta as double, Lcid=1033) {
def deg(rad)=rad*180@/pi
.printVector.Lcid<=Lcid
.x<=r*cos(deg(theta))
.y<=r*sin(deg(theta))
}
}
document s$
a=vector(3,pi/6)
s$="Vector a : "+a.printVector+{
}
b=vector(5,2*pi/3)
s$="Vector b : "+b.printVector+{
}
sum_a_b=a+b
s$="Sum of vectors a and b : "+sum_a_b.printVector+{
}
diff_a_b=a-b
s$="Difference of vectors a and b : "+diff_a_b.printVector+{
}
mul_a_3=a.mul_literal(3)
s$="Multiplying vector a by 3 : "+mul_a_3.printVector+{
}
div_b_2.5=b.div_literal(2.5)
s$="Dividing vector b by 2.5 : "+div_b_2.5.printVector+{
}
report s$
clipboard s$