CGA functions

You can use functions to encapsulate evaluations that are used several times in the rules. Unlike rules, functions are typed (in other words, they return a value) and do not change the current shape. Functions can be parameterized, conditional, and stochastic.

Example

getHeight(area) =
   case area > 1000 : 300
   case area > 500 :
      20% : 200
      50% : 150
      else : 100
   else : rand(20,50)

The getHeight function takes one float parameter (area) and returns a height depending on the parameter. If area is larger than 1000, 300 is returned. If area is larger than 500 (but smaller than, or equal to, 1000), the return value is either 200, 150, or 100 with probabilities 0.2, 0.5, and 0.3. If area is smaller than or equal to 500, a random value between 20 and 50 is returned. A rule that uses getHeight may look like the following:

Lot --> extrude(getHeight(geometry.area)) Envelope

Note:

In contrast to attributes, functions are evaluated in every call. This means that a function such as the following makes sense only for dedicated purposes, because it returns a different value every time it is used:

height = rand(30, 50)

Constant functions

You can make functions constant with the const keyword. These functions behave the same as attributes; the only difference is that const functions are internal to the rule file and cannot be mapped in the Inspector window.