新增shape形状

This commit is contained in:
yhh
2020-06-11 20:36:36 +08:00
parent 53ded30e0b
commit ad68f0e1a0
14 changed files with 235 additions and 67 deletions

View File

@@ -0,0 +1,40 @@
///<reference path="./Shape.ts" />
class Polygon extends Shape {
public points: Vector2[];
constructor(vertCount: number, radius: number){
super();
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
}
public setPoints(points: Vector2[]){
this.points = points;
this.recalculateCenterAndEdgeNormals();
}
public recalculateCenterAndEdgeNormals(){
}
public static findPolygonCenter(points: Vector2[]){
let x = 0, y = 0;
for (let i = 0; i < points.length; i++){
x += points[i].x;
y += points[i].y;
}
return new Vector2(x / points.length, y / points.length);
}
public static buildSymmertricalPolygon(vertCount: number, radius: number){
let verts = new Vector2[vertCount];
for (let i = 0; i < vertCount; i ++){
let a = 2 * Math.PI * (i / vertCount);
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius);
}
return verts;
}
}