Skip to content

Commit b61f191

Browse files
committed
Merge pull request #4 from KevinVDVelden/master
New geometry type.
2 parents f76b8ef + f19e5ec commit b61f191

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
@@ -192,6 +192,43 @@ CSG.cube = function(options) {
192192
}));
193193
};
194194

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

0 commit comments

Comments
 (0)