Files
modeling-app/rust/kcl-lib/std/math.kcl
Nick Cameron 997f539a8c More numeric type propagations (#6221)
Last few numeric type propagations

Signed-off-by: Nick Cameron <nrc@ncameron.org>
2025-04-09 11:46:54 +12:00

120 lines
2.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@no_std
@settings(defaultLengthUnit = mm)
/// The value of `pi`, Archimedes constant (π).
///
/// ```
/// circumference = 70
///
/// exampleSketch = startSketchOn(XZ)
/// |> circle(center = [0, 0], radius = circumference / (2 * PI))
///
/// example = extrude(exampleSketch, length = 5)
/// ```
export PI = 3.14159265358979323846264338327950288_
/// The value of Eulers number `e`.
///
/// ```
/// exampleSketch = startSketchOn(XZ)
/// |> startProfileAt([0, 0], %)
/// |> angledLine({
/// angle = 30,
/// length = 2 * E ^ 2,
/// }, %)
/// |> yLine(endAbsolute = 0)
/// |> close()
///
/// example = extrude(exampleSketch, length = 10)
/// ```
export E = 2.71828182845904523536028747135266250_
/// The value of `tau`, the full circle constant (τ). Equal to 2π.
///
/// ```
/// exampleSketch = startSketchOn(XZ)
/// |> startProfileAt([0, 0], %)
/// |> angledLine({
/// angle = 50,
/// length = 10 * TAU,
/// }, %)
/// |> yLine(endAbsolute = 0)
/// |> close()
///
/// example = extrude(exampleSketch, length = 5)
/// ```
export TAU = 6.28318530717958647692528676655900577_
/// Compute the cosine of a number (in radians).
///
/// ```
/// exampleSketch = startSketchOn(XZ)
/// |> startProfileAt([0, 0], %)
/// |> angledLine({
/// angle = 30,
/// length = 3 / cos(toRadians(30)),
/// }, %)
/// |> yLine(endAbsolute = 0)
/// |> close()
///
/// example = extrude(exampleSketch, length = 5)
/// ```
@(impl = std_rust)
export fn cos(@num: number(rad)): number(_) {}
/// Compute the sine of a number (in radians).
///
/// ```
/// exampleSketch = startSketchOn(XZ)
/// |> startProfileAt([0, 0], %)
/// |> angledLine({
/// angle = 50,
/// length = 15 / sin(toRadians(135)),
/// }, %)
/// |> yLine(endAbsolute = 0)
/// |> close()
///
/// example = extrude(exampleSketch, length = 5)
/// ```
@(impl = std_rust)
export fn sin(@num: number(rad)): number(_) {}
/// Compute the tangent of a number (in radians).
///
/// ```
/// exampleSketch = startSketchOn(XZ)
/// |> startProfileAt([0, 0], %)
/// |> angledLine({
/// angle = 50,
/// length = 50 * tan(1/2),
/// }, %)
/// |> yLine(endAbsolute = 0)
/// |> close()
///
/// example = extrude(exampleSketch, length = 5)
/// ```
@(impl = std_rust)
export fn tan(@num: number(rad)): number(_) {}
/// Convert polar/sphere (azimuth, elevation, distance) coordinates to
/// cartesian (x/y/z grid) coordinates.
///
/// ```
/// exampleSketch = startSketchOn(XZ)
/// |> startProfileAt([0, 0], %)
/// |> line(end = polar(angle = 30, length = 5), tag = $thing)
/// |> line(end = [0, 5])
/// |> line(end = [segEndX(thing), 0])
/// |> line(end = [-20, 10])
/// |> close()
///
/// example = extrude(exampleSketch, length = 5)
/// ```
export fn polar(angle: number(deg), length: number(mm)): [number(mm); 2] {
// TODO could be done by implicit conversion when UoM coercions are activated.
rads = toRadians(angle)
x = length * cos(rads)
y = length * sin(rads)
return [x, y]
}