Dihedron

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;
  }
  constructor(radius, N){
  	this.radius = radius;
    this.N = N;
  	this.verticies = [];
    for (let i = 0; i < N; i++)
    {
    	const vertex = Dihedron.createVertex(
        radius,
        2 * Math.PI  * i / N
      );
      this.verticies.push(vertex);
    }
  }
  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 Obeject.assign(
    	Object.create(getPrototypeOf(this)),
      this
    );
  }
}

const triangle = new Dihedron(1, 3);
console.log(triangle)