PlaceOnRectangle now uses MarchingAnts and has a new shift argument.

This commit is contained in:
Richard Davey 2017-03-29 20:27:36 +01:00
parent 081b2816ce
commit dccda7a5b3
3 changed files with 27 additions and 10 deletions

View file

@ -1,15 +1,32 @@
var PerimeterPoint = require('../geom/rectangle/PerimeterPoint');
var MarchingAnts = require('../geom/rectangle/MarchingAnts');
var RotateLeft = require('../utils/array/RotateLeft');
var RotateRight = require('../utils/array/RotateRight');
var PlaceOnRectangle = function (items, rect)
// Place the items in the array around the perimeter of the given rectangle.
// Placement starts from the top-left of the rectangle, and proceeds in a
// clockwise direction. If the shift parameter is given you can offset where
// placement begins.
var PlaceOnRectangle = function (items, rect, shift)
{
var angle = 0;
var step = 360 / items.length;
if (shift === undefined) { shift = 0; }
var points = MarchingAnts(rect, false, items.length);
if (shift > 0)
{
RotateLeft(points, shift);
}
else if (shift < 0)
{
RotateRight(points, Math.abs(shift));
}
for (var i = 0; i < items.length; i++)
{
PerimeterPoint(rect, angle, items[i]);
angle += step;
items[i].x = points[i].x;
items[i].y = points[i].y;
}
return items;

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: '2bdd4310-14b0-11e7-829e-c915fb3f8068'
build: '237de9e0-14b5-11e7-9970-cb460f4880e6'
};
module.exports = CHECKSUM;

View file

@ -231,9 +231,9 @@ var Layer = new Class({
return this;
},
placeOnRectangle: function (rect)
placeOnRectangle: function (rect, shift)
{
Actions.PlaceOnRectangle(this.children.entries, rect);
Actions.PlaceOnRectangle(this.children.entries, rect, shift);
return this;
},