forked from ccccourse/wp110b
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw6.js
70 lines (62 loc) · 1.18 KB
/
hw6.js
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
67
68
69
70
class vector
{
constructor(array)
{
this.x = array
}
length()
{
let y = 0;
for(let i = 0 ; i < this.x.length ; i++)
{
y += this.x[i]*this.x[i]
}
return Math.sqrt(y)
}
neg()
{
for(let i = 0 ; i < this.x.length ; i++)
{
this.x[i] = -this.x[i]
}
return new vector(this.x)
}
add(p2)
{
let y = [];
for(let i = 0 ; i < this.x.length ; i++)
{
y[i] = this.x[i] + p2.x[i]
}
return new vector(y)
}
sub(p2)
{
return this.add(p2.neg())
}
distance(p2)
{
return this.sub(p2).length()
}
dot(p2)
{
let y = 0;
for(let i = 0 ; i < this.x.length ; i++)
{
y += this.x[i]*p2.x[i]
}
return y
}
toString()
{
return this.x.toString()
}
}
let p = new vector([2,3])
let p2 = new vector([1,2])
console.log('p.length()=', p.toString())
console.log('p.length()=', p.length())
console.log('p.add()=',p.add(p2))
console.log('p.sub()=',p.sub(p2))
console.log('p.distance()=',p.distance(p2))
console.log('p.dot()=',p.dot(p2))