Diese Seite auf Deutsch

Angles and ramps ​

A ramp up to your castle, a slide down a mountain, a roof: you know how long it should be and how steep it should look, but how many blocks high does it go? And the other way round: how steep is the hill behind your house? Two functions answer it, TAN and its opposite ATN.

Functions: TAN and ATN, with ROUND and the constant PI

What you need

A trigger block with a button and a display block. For height, the variable items length of type INTEGER and degrees of type FLOAT (optional: the defaults are 10 blocks and 30 degrees) and two uninitialized items; for slope, the variable items run and rise of type INTEGER and one uninitialized item. For the first exercise: a variable item height of type INTEGER and one uninitialized item.

The idea ​

The steepness of a ramp is its slope: how many blocks it rises per block forward. Stairs rise 1 per 1, a ramp of slabs 1 per 2. The tangent of the angle is exactly that slope:

  • TAN turns an angle into a slope: rise = length × tan(angle).
  • ATN, the arc tangent, turns a slope back into an angle: angle = atn(rise / run).

The functions of c0de/math work in radians, where a full turn is 2 × PI. People think in degrees, where it is 360. So the program converts: degrees × PI / 180 gives radians, radians × 180 / PI gives degrees.

The program ​

c0de
PROGRAM ramps
NOOP
INCLUDE c0de/math
NOOP
FUNCTION height
    PARAMETER length INTEGER 10
    PARAMETER degrees FLOAT 30.0
    RESULT rise FLOAT
    RESULT blocks INTEGER
BEGIN
    VARIABLE tangent FLOAT
    CALL c0de/math/TAN
        PARAMETER value degrees * c0de/math/PI / 180 ;
        RESULT result tangent
    ASSIGN rise length * tangent ;
    CALL c0de/math/ROUND
        PARAMETER value rise ;
        RESULT result blocks
    CALL PRINT
        PARAMETER value length ⊕ " long at " ⊕ degrees ⊕ "°: " ⊕ blocks ⊕ " blocks high" ;
END FUNCTION
NOOP
FUNCTION slope
    PARAMETER run INTEGER
    PARAMETER rise INTEGER
    RESULT degrees INTEGER
BEGIN
    VARIABLE angle FLOAT
    IF run = 0 ;
    BEGIN
        ASSIGN degrees 90 ;
        RETURN
    END IF
    CALL c0de/math/ATN
        PARAMETER value rise / run ;
        RESULT result angle
    CALL c0de/math/ROUND
        PARAMETER value angle * 180 / c0de/math/PI ;
        RESULT result degrees
    CALL PRINT
        PARAMETER value rise ⊕ " up over " ⊕ run ⊕ ": " ⊕ degrees ⊕ "°" ;
END FUNCTION

Building and running it ​

Bind the display block to the program ramps and the function you want, and set the trigger block to the same function.

  • height without items: a ramp of 10 blocks at 30 degrees rises 5.77 blocks; blocks is 6, and the display shows 10 long at 30.0°: 6 blocks high. With length = 8 and degrees = 45, it rises 8 blocks – like stairs.
  • slope with run = 2 and rise = 1, a ramp of slabs: degrees is 27, and the display shows 1 up over 2: 27°. With rise = -1 and run = 4, the slope goes down: -14°. With run = 0, it is a wall: 90°.

How it works ​

  • Radians in, FLOAT out. TAN gets degrees * c0de/math/PI / 180. Its result is a FLOAT, and so is rise: for 45 degrees it is 7.999999999999999, not 8, because PI and the tangent are only nearly exact. ROUND makes the 8 blocks of it you can build.
  • rise / run gives a FLOAT, because / always does, even for two INTEGERs. ATN takes it and gives the angle in radians, which angle * 180 / c0de/math/PI turns into degrees.
  • The wall. A run of 0 would divide by zero and raise DIVISION_BY_ZERO. The IF answers 90 degrees first and leaves the function with RETURN, before the PRINT.
  • Negative slopes give negative angles: ATN keeps the sign. Its result is always between -90 and 90 degrees.
  • The constant c0de/math/PI comes with INCLUDE c0de/math, like the functions. A CONSTANT needs a literal value, so the program writes the conversion into the expressions instead of a constant of its own.

Try it yourself ​

  1. Your castle gate is 5 blocks above the ground. How long must a ramp at 30 degrees be to reach it?
Solution

The opposite of height: length = height / tan(angle). For 5 blocks at 30 degrees, blocks is 9 (8.66, rounded).

c0de
PROGRAM ramplength
NOOP
INCLUDE c0de/math
NOOP
FUNCTION length
    PARAMETER height INTEGER
    PARAMETER degrees FLOAT 30.0
    RESULT blocks INTEGER
BEGIN
    VARIABLE tangent FLOAT
    CALL c0de/math/TAN
        PARAMETER value degrees * c0de/math/PI / 180 ;
        RESULT result tangent
    CALL c0de/math/ROUND
        PARAMETER value height / tangent ;
        RESULT result blocks
END FUNCTION

At 0 degrees, the tangent is 0 and the program crashes with DIVISION_BY_ZERO: a flat ramp never gets anywhere.

  1. What does height say for 90 degrees, and why?
Solution

A huge number: TAN of 90 degrees is infinite in theory, but 90 * PI / 180 is a tiny bit less than a right angle, so TAN gives about 16 million billion, and the display says 10 long at 90.0°: 163312393531953696 blocks high. ROUND can still make an INTEGER of that, but no ramp is that high: at 90 degrees, it is a wall. Keep the angles of ramps well below 90 degrees, and use ATN, which never has this problem, to measure steep slopes.

Read more ​