-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
054e4e4
commit 8592588
Showing
5 changed files
with
200 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {Vector2} from "./Vector2.js" | ||
|
||
export class Face { | ||
constructor(from, to) { | ||
this.from = from.clone() | ||
this.to = to.clone() | ||
|
||
let delta = to.clone().subtract(from).normal() | ||
this.normal = new Vector2(-delta.y, delta.x) | ||
} | ||
|
||
center() { | ||
let delta = this.to.clone().subtract(this.from).normal() | ||
return this.from.clone().add(delta.scale(delta.magnitude() / 2)) | ||
} | ||
} | ||
|
||
export class Polygon { | ||
constructor(points) { | ||
console.log(points) | ||
this.points = points; | ||
this.sides = []; | ||
for (let i = 0; i < points.length - 1; i++) { | ||
this.sides.push(new Face(points[i], points[i + 1])) | ||
console.log(this.sides[i].center()) | ||
console.log(this.sides[i].from) | ||
} | ||
this.sides.push(new Face(points[points.length-1], points[0])) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export class Vector2 { | ||
constructor(x, y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
normal() { | ||
let mag = this.magnitude() | ||
return new Vector2(this.x / mag, this.y / mag); | ||
} | ||
|
||
magnitude() { | ||
return Math.sqrt(this.x*this.x + this.y*this.y); | ||
} | ||
|
||
add(vector) { | ||
this.x += vector.x; | ||
this.y += vector.y; | ||
return this; | ||
} | ||
|
||
subtract(vector) { | ||
this.x -= vector.x; | ||
this.y -= vector.y; | ||
return this; | ||
} | ||
|
||
multiply(vector) { | ||
this.x *= vector.x; | ||
this.y *= vector.y; | ||
return this; | ||
|
||
} | ||
|
||
divide(vector) { | ||
this.x /= vector.x; | ||
this.y /= vector.y; | ||
return this; | ||
|
||
} | ||
|
||
scale(scalar) { | ||
this.x *= scalar; | ||
this.y *= scalar; | ||
return this; | ||
} | ||
|
||
clone() { | ||
return new Vector2(this.x, this.y) | ||
} | ||
|
||
dot(vector) { | ||
return this.x*vector.x + this.y*vector.y | ||
} | ||
|
||
cross(vector) { | ||
return this.x * vector.y - this.y * vector.x | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | ||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | ||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | ||
<!--[if gt IE 8]> <html class="no-js"> <!--<![endif]--> | ||
<html> | ||
<head> | ||
<base href="."> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title></title> | ||
<meta name="description" content=""> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" href=""> | ||
</head> | ||
<body> | ||
<script type="module" src="./main.js" async defer></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Vector2 } from "./Vector2.js"; | ||
import * as polyModule from "./Polygon.js" | ||
|
||
let newPoly = new polyModule.Polygon([ | ||
new Vector2(-1, 1), | ||
new Vector2(1, 1), | ||
new Vector2(1, -1), | ||
new Vector2(-1, -1) | ||
]) | ||
|
||
console.log(newPoly) | ||
|
||
let vector = new Vector2(1, 1); | ||
let vector2 = new Vector2(5, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters