MODULE R2; IMPORT Math; (* Arithmetic functions on 2D real vectors. See also: "C", "Math". *) CONST Origin = (0,0); (* "Origin" is the origin in 2-space. *) PRED AtOrigin(p) IS p = Origin END; UI PointTool(AtOrigin); (* The point "p" is at the origin. The Juno-2 application arranges that the origin of the Juno coordinate system is always in the center of the drawing view, and always at the center of a printed page. *) FUNC r = Plus(p, q) IS (E px, py, qx, qy :: p = (px, py) AND q = (qx, qy) AND r = (px + qx, py + qy)) END; (* "r" is the vector sum "p" + "q". *) FUNC r = PlusX(p, x) IS (E px, py :: p = (px, py) AND r = (px + x, py)) END; (* Equivalent to "r = Plus(p, (x, 0))". *) FUNC r = PlusY(p, y) IS (E px, py :: p = (px, py) AND r = (px, py + y)) END; (* Equivalent to "r = Plus(p, (0, y))". *) FUNC r = Minus(p, q) IS (E px, py, qx, qy :: p = (px, py) AND q = (qx, qy) AND r = (px - qx, py - qy)) END; (* "r" is the vector difference "p" - "q". *) FUNC r = MinusX(p, x) IS (E px, py :: p = (px, py) AND r = (px - x, py)) END; (* Equivalent to "r = Minus(p, (x, 0))". *) FUNC r = MinusY(p, y) IS (E px, py :: p = (px, py) AND r = (px, py - y)) END; (* Equivalent to "r = Minus(p, (0, y))". *) FUNC p = Times(t, q) IS (E px, py, qx, qy :: p = (px, py) AND q = (qx, qy) AND px = t * qx AND py = t * qy) END; (* "p" is the product of the scalar "t" and the vector "q". *) FUNC len2 = Length2(v) IS (E vx, vy :: v = (vx, vy) AND len2 = (vx * vx) + (vy * vy)) END; (* "len2" is the square of the Euclidean length of the vector "v". *) FUNC len = Length(v) IS len = Math.Sqrt(Length2(v)) END; (* "len" is the Euclidean length of the vector "v". *) FUNC u = Normalize(v) IS Times(Length(v), u) = v END; (* "u" is the vector "v" normalized to have unit length. *) FUNC t = Dot(p, q) IS (E px, py, qx, qy :: p = (px, py) AND q = (qx, qy) AND t = px * qx + py * qy) END; (* The scalar "t" is the dot-product of "p" and "q". *) FUNC t = Det(p, q) IS (E px, py, qx, qy :: p = (px, py) AND q = (qx, qy) AND t = px * qy - py * qx) END; (* The scalar "t" is the determinant of the 2x2 matrix "(p q)", where "p" and "q" are column vectors. *)