Vector: Difference between revisions

1,661 bytes added ,  1 year ago
no edit summary
No edit summary
Line 1,434:
(55, 77)
(2.5, 3.5)</pre>
 
=={{header|M2000 Interpreter}}==
Adapted from C
 
<syntaxhighlight lang="m2000 interpreter">
class vector {
private:
double x, y
public:
class literal {
double v
class:
module Literal(.v) {
}
}
operator "+" (b as vector){
.x+=b.x
.y+=b.y
}
operator "-" (b as vector){
.x-=b.x
.y-=b.y
}
operator "*" (b as literal){
.x*=b.v
.y*=b.v
}
operator "/" (b as literal){
.x/=b.v
.y/=b.v
}
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*a.literal(3)
s$="Multiplying vector a by 3 : "+mul_a_3.printVector+{
}
div_b_2.5=b/b.literal(2.5)
s$="Dividing vector b by 2.5 : "+div_b_2.5.printVector+{
}
report s$
clipboard s$
</syntaxhighlight>
{{out}}
<pre>
Vector a : 2.598076 î +1.5 û
Vector b : -2.5 î +4.330127 û
Sum of vectors a and b : 0.098076 î +5.830127 û
Difference of vectors a and b : 5.098076 î -2.830127 û
Multiplying vector a by 3 : 7.794229 î +4.5 û
Dividing vector b by 2.5 : -1 î +1.732051 û
</pre>
 
 
=={{header|Maple}}==
404

edits