omdl  v0.6.1
OpenSCAD Mechanical Design Library
math_oshapes.scad
Go to the documentation of this file.
1 //! Other shapes mathematical functions.
2 /***************************************************************************//**
3  \file math_oshapes.scad
4  \author Roy Allen Sutton
5  \date 2015-2017
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \ingroup math math_oshapes
31 *******************************************************************************/
32 
33 include <../datatypes/datatypes-base.scad>;
34 
35 //----------------------------------------------------------------------------//
36 /***************************************************************************//**
37  \addtogroup math
38  @{
39 
40  \defgroup math_oshapes Other Shapes
41  \brief Mathematical functions for other shapes.
42  @{
43 *******************************************************************************/
44 //----------------------------------------------------------------------------//
45 
46 //! Compute the coordinates for an n-sided regular polygon.
47 /***************************************************************************//**
48  \param n <integer> The number of sides.
49  \param r <decimal> The vertex circumradius of the circumcircle.
50  \param a <decimal> The inradius of the incircle.
51  \param vr <decimal> The vertex rounding radius.
52  \param cw <boolean> Use clockwise point ordering.
53 
54  \returns <coords-2d> A list of coordinates points [[x, y], ...].
55 
56  \details
57 
58  The radius can be specified by either the circumradius \p r or the
59  inradius \p a. If both are specified, \p r is used.
60 
61  \b Example
62  \code{.C}
63  vr=5;
64 
65  hull()
66  {
67  for ( p = rpolygon_lp( r=20, n=5, vr=vr ) )
68  translate( p )
69  circle( r=vr );
70  }
71  \endcode
72 *******************************************************************************/
73 function rpolygon_lp
74 (
75  n,
76  r,
77  a,
78  vr,
79  cw = true
80 ) =
81 [
82  let
83  (
84  s = is_defined(r) ? r
85  : is_defined(a) ? a / cos(180/n)
86  : 0,
87 
88  b = (cw == true) ? [360:-(360/n):1] : [0:(360/n):359]
89  )
90  for ( a = b )
91  let( v = [s*cos(a), s*sin(a)] )
92  not_defined(vr) ? v : v - vr/cos(180/n) * unit_l(v)
93 ];
94 
95 //! Compute the area of an n-sided regular polygon.
96 /***************************************************************************//**
97  \param n <integer> The number of sides.
98  \param r <decimal> The vertex circumradius of the circumcircle.
99  \param a <decimal> The inradius of the incircle.
100 
101  \returns <decimal> Area of the n-sided regular polygon.
102 
103  \details
104 
105  The radius can be specified by either the circumradius \p r or the
106  inradius \p a. If both are specified, \p r is used.
107 *******************************************************************************/
108 function rpolygon_area
109 (
110  n,
111  r,
112  a
113 ) = is_defined(r) ? pow(r, 2) * n * sin(360/n) / 2
114  : is_defined(a) ? pow(a, 2) * n * tan(180/n)
115  : 0;
116 
117 //! Compute the perimeter of an n-sided regular polygon.
118 /***************************************************************************//**
119  \param n <integer> The number of sides.
120  \param r <decimal> The vertex circumradius of the circumcircle.
121  \param a <decimal> The inradius of the incircle.
122 
123  \returns <decimal> Perimeter length of the n-sided regular polygon.
124 
125  \details
126 
127  The radius can be specified by either the circumradius \p r or the
128  inradius \p a. If both are specified, \p r is used.
129 *******************************************************************************/
130 function rpolygon_perimeter
131 (
132  n,
133  r,
134  a
135 ) = is_defined(r) ? 2 * n * r * sin(180/n)
136  : is_defined(a) ? 2 * n * a * tan(180/n)
137  : 0;
138 
139 //! @}
140 //! @}
141 
142 //----------------------------------------------------------------------------//
143 // end of file
144 //----------------------------------------------------------------------------//
function rpolygon_lp(n, r, a, vr, cw=true)
Compute the coordinates for an n-sided regular polygon.
function not_defined(v)
Test if a value is not defined.
function rpolygon_perimeter(n, r, a)
Compute the perimeter of an n-sided regular polygon.
function unit_l(l)
Compute the normalized unit vector of a Euclidean line (or vector).
function is_defined(v)
Test if a value is defined.
function rpolygon_area(n, r, a)
Compute the area of an n-sided regular polygon.