MCPcopy Create free account
hub / github.com/marijnh/Eloquent-JavaScript / Vec

Class Vec

code/draw_layout.js:3–19  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1// The familiar Vec type.
2
3class Vec {
4 constructor(x, y) {
5 this.x = x; this.y = y;
6 }
7 plus(other) {
8 return new Vec(this.x + other.x, this.y + other.y);
9 }
10 minus(other) {
11 return new Vec(this.x - other.x, this.y - other.y);
12 }
13 times(factor) {
14 return new Vec(this.x * factor, this.y * factor);
15 }
16 get length() {
17 return Math.sqrt(this.x * this.x + this.y * this.y);
18 }
19}
20
21// Since we will want to inspect the layouts our code produces, let's
22// first write code to draw a graph onto a canvas. Since we don't know

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected