Code
var center , vertexes , perimeter , CoM ;
this.vertexesOfRegularPoly = function(_center , _diameter, _nVertexes,_increment){
var _vertexes = [] ,
_iVertex ,
_vertex={x:0,y:0} ,
_angle ;
for(_iVertex=0; _iVertex<_nVertexes*_increment ; _iVertex+=_increment){
_angle = 2*Math.PI * _iVertex / _nVertexes ;
_vertex.x = _center.x + _diameter * Math.cos(_angle) ;
_vertex.y = _center.y + _diameter * Math.sin(_angle) ;
_vertexes.push(_vertex);
}
return _vertexes ;
};
this.computePerimeter = function(_vertexes,_gridSpacing){
var _iVertex = 0, _nVertexes = _vertexes.length ;
var _vertex ={x:0,y:0}, nextVertex={x:0,y:0} ;
var _dX = 0 , _dY = 0 ;
var _side =[0,0] , _norm = 0, _iStep , _nSteps = 0 ,_adjustedGridSpacing = 0;
var _perimeter = [] ;
for(_iVertex=0 ; _iVertex<_nVertexes ; _iVertex++){
_vertex = _vertexes[_iVertex] ;
_nextVertex = _vertexes[(_iVertex+1)%_nVertexes] ;
_side= {x:_nextVertex.x - _vertex.x ,y:_nextVertex.y - _vertex.y };
_norm=Math.sqrt(_side.x*_side.x + _side.y*_side.y) ;
_nSteps = Math.floor( _norm / _gridSpacing ) ;
for(_iStep = 0 ; _iStep < _nSteps ;_iStep++){
// this.say(_vertex.x + (_iStep/_nSteps)*_side.x);
_perimeter.push({x:_vertex.x + (_iStep/_nSteps)*_side.x ,
y:_vertex.y + (_iStep/_nSteps)*_side.y});
}
}
return _perimeter ;
};
this.fillPolygon = function(_perimeter ,_center, _booChangeColor){
var _nPeri = _perimeter.length ,
_iPeri = 0 ,
_colors = ["red","pink","yellow","blue","purple","white"],
_nColors = _colors.length ;
this.toggleFlowers(false);
this.moveXY(_center.x, _center.y);
this.toggleFlowers(true);
for(_iPeri = 0 ; _iPeri < _nPeri ; _iPeri ++){
if(_iPeri % 2 ===0){
if(_booChangeColor){
this.setFlowerColor(_colors[(_iPeri/2) % _nColors]) ;
}
this.moveXY(_center.x, _center.y);
this.moveXY(_perimeter[_iPeri].x, _perimeter[_iPeri].y);
}else{
this.moveXY(_perimeter[_iPeri].x, _perimeter[_iPeri].y);
// this.toggleFlowers(true);
this.moveXY(_center.x, _center.y);
}
}
};
this.centerOfMass = function(_vertexes){
var _nVertexes = _vertexes.length ,
_iVertex = 0 ,
_CoM = {x:0 , y:0};
for(_iVertex= 0 ; _iVertex< _nVertexes ; _iVertex++){
_vertex = _vertexes[_iVertex] ;
_CoM.x += _vertex.x ;
_CoM.y += _vertex.y ;
}
_CoM.x /= _nVertexes ;
_CoM.y /= _nVertexes ;
return _CoM ;
};
// Blue triangle
center = {x:38,y:75} ;
vertexes = this.vertexesOfRegularPoly(center , 10 , 3 , 1);//Triangle size 10
perimeter = this.computePerimeter(vertexes , 2);
CoM = this.centerOfMass(vertexes) ;
this.setFlowerColor("blue");
this.fillPolygon(perimeter,CoM, false) ;
// Red Square
center = {x:60,y:75} ;
vertexes = this.vertexesOfRegularPoly(center , 10 , 4 , 1); // Square, size 10
perimeter = this.computePerimeter(vertexes , 2);
CoM = this.centerOfMass(vertexes) ;
this.setFlowerColor("red");
this.fillPolygon(perimeter,CoM, false) ;
// White Pentagon
center = {x:82,y:75} ;
vertexes = this.vertexesOfRegularPoly(center , 10 , 5 , 1); // Pentagon, size 10
perimeter = this.computePerimeter(vertexes , 2);
CoM = this.centerOfMass(vertexes) ;
this.setFlowerColor("white");
this.fillPolygon(perimeter,CoM, false) ;
// Purple Pentagram
center = {x:38,y:50} ;
vertexes = this.vertexesOfRegularPoly(center , 10 , 5 , 2); // Pentagram, size 10
perimeter = this.computePerimeter(vertexes , 2);
CoM = this.centerOfMass(vertexes) ;
this.setFlowerColor("purple");
this.fillPolygon(perimeter,CoM, false) ;
// Yellow SpearHead
center = {x:60,y:50} ;
vertexes = this.vertexesOfRegularPoly(center , 10 , 3 , 1);//Triangle size 10
vertexes.push(center); // Adding a 4th vertex
perimeter = this.computePerimeter(vertexes , 2);
var focalPoint={x:(vertexes[1].x+center.x)/2 , y:(vertexes[1].y+center.y)/2};
this.setFlowerColor("yellow");
this.fillPolygon(perimeter,focalPoint, false) ;
// Pink hexagon
center = {x:82,y:50} ;
vertexes = this.vertexesOfRegularPoly(center , 10 , 6 , 1); // hexagon, size 10
perimeter = this.computePerimeter(vertexes , 1);
CoM = this.centerOfMass(vertexes) ;
this.setFlowerColor("pink");
this.fillPolygon(perimeter,CoM, false) ;
this.toggleFlowers(false);
this.moveXY(60, 63);