Skip to content

Commit f19e5ec

Browse files
committed
Added new geometry, a cuboid.
1 parent f246f83 commit f19e5ec

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

csg.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,43 @@ CSG.cube = function(options) {
189189
}));
190190
};
191191

192+
// Construct an axis-aligned solid cuboid. Optional parameters are `center` and
193+
// `radius`, which default to `[0, 0, 0]` and `[1, 1, 1]`.
194+
//
195+
// Example code:
196+
//
197+
// var cube = CSG.cube({
198+
// center: [0, 0, 0],
199+
// radius: 1
200+
// });
201+
CSG.cuboid = function(options) {
202+
options = options || {};
203+
var c = new CSG.Vector(options.center || [0, 0, 0]);
204+
var r = ( !options.radius ) ?
205+
( [ 1, 1, 1 ] ) :
206+
( options.radius.length ?
207+
options.radius :
208+
[ options.radius, options.radius, options.radius ]
209+
);
210+
return CSG.fromPolygons([
211+
[[0, 4, 6, 2], [-1, 0, 0]],
212+
[[1, 3, 7, 5], [+1, 0, 0]],
213+
[[0, 1, 5, 4], [0, -1, 0]],
214+
[[2, 6, 7, 3], [0, +1, 0]],
215+
[[0, 2, 3, 1], [0, 0, -1]],
216+
[[4, 5, 7, 6], [0, 0, +1]]
217+
].map(function(info) {
218+
return new CSG.Polygon(info[0].map(function(i) {
219+
var pos = new CSG.Vector(
220+
c.x + r[0] * (2 * !!(i & 1) - 1),
221+
c.y + r[1] * (2 * !!(i & 2) - 1),
222+
c.z + r[2] * (2 * !!(i & 4) - 1)
223+
);
224+
return new CSG.Vertex(pos, new CSG.Vector(info[1]));
225+
}));
226+
}));
227+
};
228+
192229
// Construct a solid sphere. Optional parameters are `center`, `radius`,
193230
// `slices`, and `stacks`, which default to `[0, 0, 0]`, `1`, `16`, and `8`.
194231
// The `slices` and `stacks` parameters control the tessellation along the

0 commit comments

Comments
 (0)