Logical patterns 2
Last updated
Last updated
This section demonstrates the use of conditional expressions via symbolic notation, instead of multiple components. We generate a sequence of random points and aim to select a sub-set thereof between a minimum (low) and a maximum (high) value using conditional expressions.
Using symbolic expressions is much more concise. Now let’s instead of selecting items from a list, we use conditionals for specifying visual attributes. In the example below we will assign the radius of circles for each point based on whether fall inside the [low, high] range.
Here we introduce the “if” function. It requires three arguments with first being a boolean true / false value, in addition to two more arbitrary values of our choice. The function returns either of them based on whether the boolean value is true or false. To be more specific, if the first argument’s value is true, then the function returns the second argument, otherwise the third. The exact same syntax for the “if” function is supported in spreadsheets.
Practice
Modify the above graph such that only points from a certain distance from the mean value are encircled. Compute the mean using the “average” component or sum all random values and divide the sum by the number of random values generated i.e. the count used for the series.
Use the “if” expression to apply different colours to points.
The following section demonstrates expressing more complex conditions. It builds upon the previous example of filtering but in two dimensions. We define a region of random 2D points and desire to select points within a bounding rectangle. The rectangle is expressed as a pair of points “/pmin” and “pmax” which package two orthogonal ranges of coordinates [xmin, xmax] and [ymin, ymax].
For a point to be situated within a rectangle, its coordinates must belong exclusively within both coordinate ranges. Therefore we can use the “and” operator to combine those constraints into one expression. This is equivalent of the previous low/high example only performed twice. The use of parentheses is not strictly required but it helps clarify the compound form of this expression and therefore advisable.
To avoid the verbosity of the above graph, we may directly compute the “point in rectangle” clause using the graph below. When using points instead of coordinates we use the p.X
, p.Y
and p.Z
to access the relevant components. This style avoids using the “decompose” components to extract values from semantic data.
We considered the idea of “inclusion” which lead us to the concept of compounding tests using the AND operation per direction i.e. first check if the point is within the X-interval and also check the Y-interval. How about looking the same problem from the perspective of “exclusion” i.e. a point is outside the rectangle?
The above expression: (a) flipped the direction of the greater/less than tests including removing the equality; and (b) replaced the AND with OR tests. This is the symmetric opposite of the inclusion test.
What is worth noting is that conceptually this expression does not feel as compound as the inclusion version. In other words, if any of those tests separately is TRUE then we can assure that the point is external without testing any further!
Practice
Extend the above logic to 3D by making an axis-aligned bounding box inclusion test. Create points using the “populate 3D” component.
Verify that the “point in circle” becomes “point in sphere” without change when using points in three-dimensions.
We can also express the same logic this using a python component. The syntax is a bit different but the essence of the operation is verbatim. This form of function-like “if” is also known as ternary “if”, because it has three arguments []. Do note that the use of parentheses is optional but it is highly recommended.
Random number generators (RNG), aka pseudo-random number generators (PRNG) are extensively used from video games to cryptography []. Experiment with producing random sequences. Use the “bar graph” component and observe the histogram i.e. distribution of the values generated.
Read about the separating axis theorem [] to understand why the inclusion and exclusion testing idioms while symmetric they feel so different.