The JavaSketchpad Quick Reference   part of the jsp.awk pages

5.7.12 Calculate(numX, numY, prefixStr, exprStr) (objRef1, ..., objRefN);

Constructs a measurement of the value of a calculated expression. numX, numY, and prefixStr are the standard left and top coordinates and prefix string of every measurement object; exprStr contains a representation of the expression to calculate, and objRef1 through objRefN are object identifiers of any other measurement objects the value of which act as components of the expression.

The exprStr expression is expressed in reverse Polish notation (RPN), in which arithmetic operators follow their operands. RPN eliminates the need for parentheses, because the order of evaluation is strictly left-to-right. Thus "(1+2)/3" can be phrased in RPN as "1 2 + 3 /".

Valid tokens within exprStr include:

  • numeric constants. Any real number can be expressed, with an optional preliminary negative sign (for negative numbers) and an optional single decimal point (for expressing non-integer values). A numeric constant may include a terminal space to delimit it in circumstances where two numeric constants are adjacent.
  • +, -, /, *. These operators perform their standard arithmetic operation: addition, subtraction, division, and multiplication.
  • ^: This operator performs exponentiation.
  • %: This operator performs unary negation.
  • @xxxx: This operator applies a predefined function. xxxx is a four-character sequence indicating the function, and may be one of the following: sin_ (sine), cos_ (cosine), tan_ (tangent), abs_ (absolute value), sqrt (square root), ln__ (natural log), rond (rounding), trnc (truncation toward zero), sgn_ (signnum), asin (arcsine), acos (arccosine), atan (arctangent), or log_ (base-10 logarithm).
  • x where x is in {A-Z}. This operand substitutes the value of another measurement in the sketch: A corresponds to measurement objRef1, B corresponds to measurement objRef2, etc. It is an error to pass an operator that does not have a corresponding objRef; i. e. to refer to C when you only supply two objRefs in the construction (implicitly, A and B).
  • #xy where x is in {A-Z} and y is in {1-9}. This operand substitutes the value of component y of a vector-based measurement x. x refers to an objRef according to the same rules above (i. e. A is objRef1, B is objRef2, etc.). Parent objRefx must be a vector-based measurement: in DR3 of JavaSketchpad, the only vector-based measurement is the Coordinate pair. y indicates the component of the vector to isolate. In DR3, the only legal values for y are 1 and 2: 1 indicates the x-component of a coordinate pair, 2 indicates the y-component of that pair.

Examples:

Calculate(10, 10, 'Sum is ','AB+C+') (5, 6, 7);

Calculates the sum of measurement objects #5, 6, and 7 in the sketch, and positions their sum at (10, 10) on the coordinate plane.

Calculate(20, 20, 'Distance is ','#A1#B1-2^#A2#B2-2^+@sqrt') (8, 9);

Calculates the Euclidean distance (sqrt[dX^2+dY^2]) between the two coordinate pair measurements referenced as objects #8 and 9 in the sketch.

Note that a space character may be used inside exprStr to delimit individual tokens. This allows you to express one plus three as '1 3+' where '13+' would be interpreted as thirteen plus (whatever preceded it).

Note also that the objRef list must not be empty, even if no tokens in the expression refer to object references (i. e. even if the expression's value is constant). This is a limitation of DR3, and will be fixed in the next release.


Extracted from the JavaSketchpad Construction Grammar.