forked from ccccourse/wp110b
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework6
66 lines (66 loc) · 1.3 KB
/
homework6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class vector{
constructor(array)
{
this.a=array;
}
length()
{
let i,sum=0;
for(i=0;i<this.a.length;i++)
{
sum=sum+Math.pow(this.a[i],2)
}
return Math.sqrt(sum);
}
neg()
{
let a=[];
for(i=0;i<this.a.length;i++)
{
a[i]=this.a[i]*-1;
}
return new vector(a);
}
add(p2)
{
let i=0,a=[];
for(i=0;i<this.a.length;i++)
{
a[i]=this.a[i]+p2.a[i];
}
return new vector(a);
}
sub(p2)
{
let i=0,a=[];
for(i=0;i<this.a.length;i++)
{
a[i]=this.a[i]-p2.a[i];
}
return new vector(a);
}
distance(p2)
{
return this.sub(p2).length();
}
dot(p2)
{
let i=0,a=[];
for(i=0;i<this.a.length;i++)
{
a[i]=this.a[i]*p2.a[i];
}
return new vector(a);
}
toString()
{
this.a.toString();
}
}
let p = new vector([3,4,5]), p2 = new vector([1,2,3]);
console.log('p.length()=', p.length())
console.log('p2.length()=', p2.length())
console.log('p.add(p2)=', p.add(p2))
console.log('p.sub(p2)=', p.sub(p2))
console.log('p.dot(p2)=', p.dot(p2))
console.log('p.distance(p2)=', p.distance(p2))