createFigure
by evgkch
JavaScript
class Dihedron {
static createVertex(radius, phi){
return {
x: radius * Math.cos(phi),
y: radius * Math.sin(phi),
};
}
static createVerticies(radius, N){
const verticies = [];
for (let i = 0; i < N; i++)
{
const vertex = Dihedron.createVertex(
radius,
2 * Math.PI * i / N
);
verticies.push(vertex);
}
return verticies;
}
static create(radius, N){
const verticies = Dihedron.createVerticies(radius, N);
return new Dihedron(verticies);
}
constructor(verticies){
this.verticies = verticies;
}
rotate(phi){
const cosPhi = Math.cos(phi);
const sinPhi = Math.sin(phi);
this.verticies.forEach(vertex =>{
const { x, y } = vertex;
vertex.x = x * cosPhi - y * sinPhi;
vertex.y = x * sinPhi + y * cosPhi;
});
return this;
}
move(x, y){
this.verticies.forEach(vertex =>{
vertex.x += x;
vertex.y += y;
});
return this;
}
clone(){
return new Dihedron(this.);
}
}
const createDihedronVertices = (r, n)=>{
const buffer = [];
for (let i = 0; i < n; i++)
buffer.push(
createPoint(r, 2 * Math.PI * i / n)
);
return buffer;
}
const createTriangle = r=>createFigure(r, 3);
const createSquare = r=>createFigure(r, 4);
console.log(createSquare(1))