Use class to represent shapes

This commit is contained in:
keanutaufan 2024-09-18 21:43:16 +07:00
parent e1b0deb855
commit da90785ff3
No known key found for this signature in database
GPG Key ID: 0E2EF5BE1DA75288
5 changed files with 33 additions and 9 deletions

8
src/geometry/Shape2D.ts Normal file
View File

@ -0,0 +1,8 @@
import Vector2D from "./Vector2D";
export default class Shape2D {
constructor(
public position: Vector2D,
public vertices: Float32Array,
) {}
}

10
src/geometry/Triangle.ts Normal file
View File

@ -0,0 +1,10 @@
import Shape2D from "./Shape2D";
import Vector2D from "./Vector2D";
export default class Triangle extends Shape2D {
constructor(
public position: Vector2D,
) {
super(position, new Float32Array([0.0, -0.5, 0.3, 0.5, -0.3, 0.5]));
}
}

11
src/geometry/Vector2D.ts Normal file
View File

@ -0,0 +1,11 @@
export default class Vector2D {
constructor(
public x: number,
public y: number,
) {}
add(other: Vector2D) {
this.x += other.x;
this.y += other.y;
}
}

View File

@ -5,7 +5,10 @@ import vsSource from "./shaders/basic.vert?raw";
import fsSource from "./shaders/basic.frag?raw";
import { drawVertex, loadBuffer, loadProgram, loadShader } from "./shaderUtils";
import triangle from "./shapes/triangle.json";
import Triangle from "./geometry/Triangle";
import Vector2D from "./geometry/Vector2D";
const triangle = new Triangle(new Vector2D(0, 0));
function render() {
const canvas = document.querySelector<HTMLCanvasElement>("#canvas");

View File

@ -1,8 +0,0 @@
{
"name": "triangle",
"vertices": [
0.0, -0.5,
0.3, 0.5,
-0.3, 0.5
]
}