OpenSCAD User Manual/Mathematical Functions
The text in its current form is incomplete.
[edit] abs
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
[edit] acos
Mathematical arccosine, or inverse cosine, function.
[edit] asin
Mathematical arcsine, or inverse sine, function.
[edit] atan
Mathematical arctangent, or inverse tangent, function. Returns the principal value of the arc tangent of x, expressed in degrees. See: atan function
[edit] atan2
Mathematical two-argument atan function. Returns the principal value of the arc tangent of y/x, expressed in degrees. See: atan2
[edit] ceil
Mathematical ceiling function. See: Ceil Function
[edit] cos
Mathematical cosine function.
| Usage Examples: | |
for(i=[0:36]) translate([i*10,0,0])cylinder(r=5,h=cos(i*10)*50+60); |
[edit] exp
Mathematical exp function. Returns the base-e exponential function of x, which is the number e raised to the power x. See: Exponent
[edit] floor
Mathematical floor function. See: Floor Function
[edit] ln
Mathematical natural logarithm. See: Natural logarithm
[edit] log
Mathematical logarithm. See: Logarithm
[edit] lookup
(Inserted by a beginner, may need fixing!)
From a given key-value array, it will interpolate a value for any key using linear interpolation.
Parameters
- key
- A lookup key
- <key,value array>
- keys and values
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); } |
[edit] max
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
[edit] min
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
[edit] pow
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));
}
}
[edit] rands
Random number generator.
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
- 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);
}
}
[edit] round
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.
[edit] sign
Mathmatical 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
[edit] sin
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); |
[edit] sqrt
Mathematical square root function.
Usage Examples:
translate([sqrt(100),0,0])sphere(100);
[edit] tan
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);
}