Skip to content

Commit

Permalink
added scale method to points, paths, and shapes; fixed some defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
jdan committed Apr 7, 2014
1 parent 92241cb commit 30e10eb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 16 deletions.
16 changes: 16 additions & 0 deletions path.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
}));
};


/**
* Returns a new path rotated along the Z axis by a given origin
*
Expand All @@ -58,6 +59,21 @@
}));
};


/**
* Scales a path about a given origin
*
* Simply a forward to Point#scale
*/
Path.prototype.scale = function () {
var args = arguments;

return new Path(this.points.map(function (point) {
return point.scale.apply(point, args);
}));
};


/* Expose the Path constructor */
exports.Path = Path;

Expand Down
38 changes: 33 additions & 5 deletions point.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
(function (exports) {

function Point(x, y, z) {
this.x = x;
this.y = y;
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}


Point.ORIGIN = new Point(0, 0, 0);


/**
* Translate a point from a given dx, dy, and dz
*/
Point.prototype.translate = function (dx, dy, dz) {
return new Point(
this.x + dx,
this.y + dy,
this.z + dz);
};


/**
* Rotate about origin on the Z axis
* Scale a point about a given origin
*/
Point.prototype.rotateZ = function (angle, origin) {
origin = origin || new Point(0, 0, 0);
Point.prototype.scale = function (origin, dx, dy, dz) {
var p = this.translate(-origin.x, -origin.y, -origin.z);

if (dy === undefined && dz === undefined) {
/* If both dy and dz are left out, scale all coordinates equally */
dy = dz = dx;
/* If just dz is missing, set it equal to 1 */
} else {
dz = (dz || 1);
}

p.x *= dx;
p.y *= dy;
p.z *= dz;

return p.translate(origin.x, origin.y, origin.z);
};


/**
* Rotate about origin on the Z axis
*/
Point.prototype.rotateZ = function (origin, angle) {
var p = this.translate(-origin.x, -origin.y, -origin.z);

var x = p.x * Math.cos(angle) - p.y * Math.sin(angle);
Expand Down
40 changes: 29 additions & 11 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,36 @@ function Stairs(origin) {
return stairs;
}

iso.add(Shape.Prism(new Point(1, 0, 0), 4, 4, 2));
iso.add(Shape.Prism(new Point(0, 0, 0), 1, 4, 1));
iso.add(Shape.Prism(new Point(-1, 1, 0), 1, 3, 1));

iso.add(Stairs(new Point(-1, 0, 0)));
iso.add(Stairs(new Point(0, 3, 1)).rotateZ(-Math.PI / 2, new Point(0.5, 3.5, 1)));
function drawStructure() {
iso.add(Shape.Prism(new Point(1, 0, 0), 4, 4, 2));
iso.add(Shape.Prism(new Point(0, 0, 0), 1, 4, 1));
iso.add(Shape.Prism(new Point(-1, 1, 0), 1, 3, 1));

iso.add(Shape.Prism(new Point(3, 0, 2), 2, 4, 1));
iso.add(Shape.Prism(new Point(2, 1, 2), 1, 3, 1));
iso.add(Stairs(new Point(-1, 0, 0)));
iso.add(Stairs(new Point(0, 3, 1)).rotateZ(new Point(0.5, 3.5, 1), -Math.PI / 2));

iso.add(Stairs(new Point(2, 0, 2)).rotateZ(-Math.PI / 2, new Point(2.5, 0.5, 0)));
iso.add(Shape.Prism(new Point(3, 0, 2), 2, 4, 1));
iso.add(Shape.Prism(new Point(2, 1, 2), 1, 3, 1));

iso.add(Shape.Pyramid(new Point(2, 3, 3), 1, 1, 1), new Color(180, 180, 0));
iso.add(Shape.Pyramid(new Point(4, 3, 3), 1, 1, 1), new Color(180, 0, 180));
iso.add(Shape.Pyramid(new Point(4, 0, 3), 1, 1, 1), new Color(0, 180, 180));;
iso.add(Stairs(new Point(2, 0, 2)).rotateZ(new Point(2.5, 0.5, 0), -Math.PI / 2));

iso.add(Shape.Pyramid(new Point(2, 3, 3), 1, 1, 1), new Color(180, 180, 0));
iso.add(Shape.Pyramid(new Point(4, 3, 3), 1, 1, 1), new Color(180, 0, 180));
iso.add(Shape.Pyramid(new Point(4, 0, 3), 1, 1, 1), new Color(0, 180, 180));;
}

function testScales() {
var cube = Shape.Prism(new Point(5, 5), 1, 1, 1);

for (var i = 0; i < 20; i++) {
iso.add(cube
.scale(new Point(5.5, 5.5), 10 - i/2, 10 - i/2, 1/3)
.translate(0, 0, i/5)
.rotateZ(new Point(5.5, 5.5), -Math.PI/20 * i),
new Color(parseInt(Math.random() * 256), parseInt(Math.random() * 256), parseInt(Math.random() * 256)));
}
}

//testScales();
drawStructure()
18 changes: 18 additions & 0 deletions shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
}
}


/**
* Pushes a path onto the end of the Shape
*/
Shape.prototype.push = function (path) {
this.paths.push(path);
};


/**
* Translates a given shape
*
Expand All @@ -33,6 +35,7 @@
}));
};


/**
* Rotates a given shape along the Z axis around a given origin
*
Expand All @@ -46,6 +49,21 @@
}));
};


/**
* Scales a path about a given origin
*
* Simply a forward to Point#scale
*/
Shape.prototype.scale = function () {
var args = arguments;

return new Shape(this.paths.map(function (path) {
return path.scale.apply(path, args);
}));
};


/**
* Some shapes to play with
*/
Expand Down

0 comments on commit 30e10eb

Please sign in to comment.