00001 /* 00002 * prism.cpp 00003 * 00004 * Copyright (C) 2010 Thomas A. Vaughan 00005 * All rights reserved. 00006 * 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the <organization> nor the 00016 * names of its contributors may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY 00020 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY 00023 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 * 00031 * Geometry methods specific to 3D prisms (see prism.h) 00032 */ 00033 00034 // includes -------------------------------------------------------------------- 00035 #include "prism.h" // always include our own header first 00036 00037 00038 00039 00040 //////////////////////////////////////////////////////////////////////////////// 00041 // 00042 // static helper methods 00043 // 00044 //////////////////////////////////////////////////////////////////////////////// 00045 00046 00047 //////////////////////////////////////////////////////////////////////////////// 00048 // 00049 // public API 00050 // 00051 //////////////////////////////////////////////////////////////////////////////// 00052 00053 void 00054 createRegularPrism 00055 ( 00056 IN int nSides, 00057 IN float radius, 00058 IN float height, 00059 IO point3d_t * points 00060 ) 00061 { 00062 ASSERT(nSides > 2, "Polygon must have at least 3 sides: %d", nSides); 00063 ASSERT(radius > 0, "prism base must have radius > 0: %f", radius); 00064 ASSERT(height > 0, "prism must have positive height: %f", height); 00065 ASSERT(points, "null"); 00066 00067 // loop through and construct n-sided regular polygon 00068 float dPhi = 2 * M_PI / nSides; 00069 float phi = 0; 00070 point3d_t * p = points; 00071 for (int i = 0; i < nSides; ++i, phi += dPhi, p += 2) { 00072 point3d_t& base = *p; 00073 point3d_t& top = *(p + 1); 00074 00075 base.x = radius * sin(phi); 00076 base.z = radius * cos(phi); 00077 base.y = 0.0; 00078 00079 top.x = base.x; 00080 top.z = base.z; 00081 top.y = height; 00082 } 00083 } 00084