OpenSCAD User Manual/Mathematical Functions
|
|
The text in its current form is incomplete. |
abs [edit]
Mathematical absolute value function. Returns the positive value of a signed decimal number.
Usage examples:
abs(-5.0); abs(0); abs(8.0);
Results:
5.0 0.0 8.0
acos [edit]
Mathematical arccosine, or inverse cosine, function.
asin [edit]
Mathematical arcsine, or inverse sine, function.
atan [edit]
Mathematical arctangent, or inverse tangent, function. Returns the principal value of the arc tangent of x, expressed in degrees. See: atan function
atan2 [edit]
Mathematical two-argument atan function. Returns the principal value of the arc tangent of y/x, expressed in degrees. See: atan2
ceil [edit]
Mathematical ceiling function. See: Ceil Function
cos [edit]
Mathematical cosine function.
| Usage Examples: | |
for(i=[0:36]) translate([i*10,0,0])cylinder(r=5,h=cos(i*10)*50+60); |
exp [edit]
Mathematical exp function. Returns the base-e exponential function of x, which is the number e raised to the power x. See: Exponent
floor [edit]
Mathematical floor function. See: Floor Function
ln [edit]
Mathematical natural logarithm. See: Natural logarithm
len [edit]
Mathematical length function. Returns the length of an array, a vector or a string parameter.
Usage examples:
str1="abcdef"; len_str1=len(str1); echo(str1,len_str1); a=6; len_a=len(a); echo(a,len_a); array1=[1,2,3,4,5,6,7,8]; len_array1=len(array1); echo(array1,len_array1); array2=[[0,0],[0,1],[1,0],[1,1]]; len_array2=len(array2); echo(array2,len_array2); len_array2_2=len(array2[2]); echo(array2[2],len_array2_2);
Results:
ECHO: "abcdef", 6 ECHO: 6, undef ECHO: [1, 2, 3, 4, 5, 6, 7, 8], 8 ECHO: [[0, 0], [0, 1], [1, 0], [1, 1]], 4 ECHO: [1, 0], 2
Note that the len() function is not defined when a simple variable is passed as the parameter.
This function allows (e.g.) the parsing of an array, a vector or a string.
Usage examples:
str2="4711";
for (i=[0:len(str2)-1])
echo(str("digit ",i+1," : ",str2[i]));
Results:
ECHO: "digit 1 : 4" ECHO: "digit 2 : 7" ECHO: "digit 3 : 1" ECHO: "digit 4 : 1"
log [edit]
Mathematical logarithm. See: Logarithm
lookup [edit]
Look up value in table, and linearly interpolate if there's no exact match. The first argument is the value to look up. The second is the lookup table -- a vector of key-value pairs.
Parameters
- key
- A lookup key
- <key,value array>
- keys and values
Notes
There is a bug where out-of-range keys will return the first value in the list. Newer versions of Openscad should use the top or bottom end of the table as appropriate instead.
Usage example:
|
|
function get_cylinder_h(p) = lookup(p, [ [ -200, 5 ], [ -50, 20 ], [ -20, 18 ], [ +80, 25 ], [ +150, 2 ] ]); for (i = [-100:5:+100]) { // echo(i, get_cylinder_h(i)); translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h = get_cylinder_h(i)*3); } |
max [edit]
Returns the maximum of the two parameters.
Parameters
- <a>
- Decimal.
- <b>
- Decimal.
Usage Example:
max(3.0,5.0); max(8.0,3.0);
Results:
5.0 8.0
min [edit]
Returns the minimum of the two parameters.
Parameters
- <a>
- Decimal.
- <b>
- Decimal.
Usage Example:
min(3.0,5.0); min(8.0,3.0);
Results:
3.0 3.0
norm [edit]
- !!Note this function is not in 2013.01 and is pending pull request https://github.com/openscad/openscad/pull/333.
Returns the euclidean norm of a vector. Note this returns is the actual numeric length while len returns the number of elements in the vector or array.
Usage examples:
a=[1,2,3,4]; b="abcd"; c=[]; d=""; e=[[1,2,3,4],[1,2,3],[1,2],[1]]; echo(norm(a)); //5.47723 echo(norm(b)); //undef echo(norm(c)); //0 echo(norm(d)); //undef echo(norm(e[0])); //5.47723 echo(norm(e[1])); //3.74166 echo(norm(e[2])); //2.23607 echo(norm(e[3])); //1
Results:
ECHO: 5.47723 ECHO: undef ECHO: 0 ECHO: undef ECHO: 5.47723 ECHO: 3.74166 ECHO: 2.23607 ECHO: 1
pow [edit]
Mathematical power function.
Parameters
- <base>
- Decimal. Base.
- <exponent>
- Decimal. Exponent.
Usage examples:
for (i = [0:5]) {
translate([i*25,0,0]) {
cylinder(h = pow(2,i)*5, r=10);
echo (i, pow(2,i));
}
}
rands [edit]
Random number generator. Generates a constant vector of pseudo random numbers, much like an array. When generating only one number, you still call it with variable[0]
Parameters
- min_value
- Minimum value of random number range
- max_value
- Maximum value of random number range
- value_count
- Number of random numbers to return as a vector
- seed_value (optional)
- Seed value for random number generator for repeatable results.
Usage Examples:
seed=42;
random_vect=rands(5,15,4,seed);
echo( "Random Vector: ",random_vect);
sphere(r=5);
for(i=[0:3]) {
rotate(360*i/4) {
translate([10+random_vect[i],0,0])
sphere(r=random_vect[i]/2);
}
}
round [edit]
The "round" operator returns the greatest or least integer part, respectively, if the numeric input is positive or negative.
Some examples:
round(x.5) = x+1.
round(x.49) = x.
round(-(x.5)) = -(x+1).
round(-(x.49)) = -x.
round(5.4); //-> 5
round(5.5); //-> 6
round(5.6); //-> 6
sign [edit]
Mathematical signum function. Returns a unit value that extracts the sign of a value see: Signum function
Parameters
- <x>
- Decimal. Value to find the sign of.
Usage examples:
sign(-5.0); sign(0); sign(8.0);
Results:
-1.0 0.0 1.0
sin [edit]
Mathematical sine function.
Parameters
- <degrees>
- Decimal. Angle in degrees.
Usage example 1
for (i = [0:5]) {
echo(360*i/6, sin(360*i/6)*80, cos(360*i/6)*80);
translate([sin(360*i/6)*80, cos(360*i/6)*80, 0 ])
cylinder(h = 200, r=10);
}
| Usage example 2: | |
for(i=[0:36]) translate([i*10,0,0])cylinder(r=5,h=sin(i*10)*50+60); |
sqrt [edit]
Mathematical square root function.
Usage Examples:
translate([sqrt(100),0,0])sphere(100);
tan [edit]
Mathematical tangent function. Parameters
- <degrees>
- Decimal. Angle in degrees.
Usage examples:
for (i = [0:5]) {
echo(360*i/6, tan(360*i/6)*80);
translate([tan(360*i/6)*80, 0, 0 ])
cylinder(h = 200, r=10);
}