0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-03-14 00:38:41 -04:00

Code tidy-up (reduce unnecessary if statements in for loops)

This commit is contained in:
Marc Brevoort 2015-05-01 21:54:55 +01:00
parent f4f3bf7c48
commit f6261664db

View file

@ -34,22 +34,22 @@ var Composites = {};
for (var column = 0; column < columns; column++) { for (var column = 0; column < columns; column++) {
var body = callback(x, y, column, row, lastBody, i); var body = callback(x, y, column, row, lastBody, i);
if (body) { if (!body) continue;
var bodyHeight = body.bounds.max.y - body.bounds.min.y,
bodyWidth = body.bounds.max.x - body.bounds.min.x;
if (bodyHeight > maxHeight) var bodyHeight = body.bounds.max.y - body.bounds.min.y,
maxHeight = bodyHeight; bodyWidth = body.bounds.max.x - body.bounds.min.x;
Body.translate(body, { x: bodyWidth * 0.5, y: bodyHeight * 0.5 });
x = body.bounds.max.x + columnGap; if (bodyHeight > maxHeight)
maxHeight = bodyHeight;
Body.translate(body, { x: bodyWidth * 0.5, y: bodyHeight * 0.5 });
Composite.addBody(stack, body); x = body.bounds.max.x + columnGap;
lastBody = body; Composite.addBody(stack, body);
i += 1;
} lastBody = body;
i ++;
} }
y += maxHeight + rowGap; y += maxHeight + rowGap;
@ -115,29 +115,48 @@ var Composites = {};
bodyA, bodyA,
bodyB, bodyB,
bodyC; bodyC;
for (row = 0; row < rows; row++) { row=0;
for (col = 0; col < columns; col++) { for (col = 1; col < columns; col++) {
if (col > 0) { bodyA = bodies[(col - 1)];
bodyA = bodies[(col - 1) + (row * columns)]; bodyB = bodies[col];
bodyB = bodies[col + (row * columns)]; Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyA, bodyB: bodyB }, options)));
Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyA, bodyB: bodyB }, options))); }
}
for (row = 1; row < rows; row++) {
var prevoffset = (row - 1)*columns;
var offset = row*columns;
for (col = 1; col < columns; col++) {
bodyA = bodies[(col - 1) + offset];
bodyB = bodies[col + offset];
Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyA, bodyB: bodyB }, options)));
} }
for (col = 0; col < columns; col++) { if (!crossBrace)
if (row > 0) { {
bodyA = bodies[col + ((row - 1) * columns)]; for (col = 0; col < columns; col++) {
bodyB = bodies[col + (row * columns)]; bodyA = bodies[col + prevoffset];
bodyB = bodies[col + offset];
Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyA, bodyB: bodyB }, options))); Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyA, bodyB: bodyB }, options)));
}
if (crossBrace && col > 0) { } else {
bodyC = bodies[(col - 1) + ((row - 1) * columns)]; /* If the order of the constraints does not matter,
we can optimize further by always performing the above loop
and then conditionally executing the second half of the below loop.
*/
for (col = 0; col < columns; col++) {
bodyA = bodies[col + prevoffset];
bodyB = bodies[col + offset];
Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyA, bodyB: bodyB }, options)));
// second half starts here
if (col > 0) {
bodyC = bodies[(col - 1) + prevoffset];
Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyC, bodyB: bodyB }, options))); Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyC, bodyB: bodyB }, options)));
} }
if (crossBrace && col < columns - 1) { if (col < columns - 1) {
bodyC = bodies[(col + 1) + ((row - 1) * columns)]; bodyC = bodies[(col + 1) + prevoffset];
Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyC, bodyB: bodyB }, options))); Composite.addConstraint(composite, Constraint.create(Common.extend({ bodyA: bodyC, bodyB: bodyB }, options)));
} }
} }