Ranges and expressions 5
Spreadsheet programming
Last updated
Spreadsheet programming
Last updated
Visual Attributes
CAD entities beyond their geometry, they can be assigned with visual attributes or styles. Some of those include colour, line-weight, layer index etc. Here we will expand the graphing calculator by introducing colour. We will plot a sequence of points and colour-code each using an RGB (red-green-blue) value.
Combining Multiple Commands
To achieve this we need to compose a sequence of commands per point. First we will draw the point using the “point” command. Then we will issue the “sellast” command which selects the last object placed in the document. Finally, we will invoke the “properties” command and send a sequence of parameters to modify the colour of the currently selected object. Here we will arrange all commands per point in a horizontal mode as seen in the figure. We can still use the copy/paste approach because the spreadsheet inserts tab characters between columns, for which the CAD application understands as “proceed to the next step” instructions.
Suppressing Visual Input
The “properties” command if executed, typically brings up a pop-up dialog box expecting the user to edit an object’s attributes using the graphical user interface (GUI). After all the purpose of CAD is to be a visual medium to geometric design!
However, we wish to suppress all these click requirements and redirect all input to the command line. To achieve this we can prefix the command with a hyphen “-“. Thus instead of the “properties” command, we will use the “-properties” idiom. To terminate the “properties” command we need to issue a series of artificial keystrokes, which is done by adding a couple of “enter” instructions.
Another minor annoyance comes from the spreadsheet’s formula conventions. If we start editing a cell’s contents with the symbol “=” or any other arithmetic operator, you guessed it, including the minus “-” symbol, the application thinks we are trying to enter a formula. This causes errors because “-properties” does not make sense!
We have a conflict of text-input conventions! To overcome this we need to prefix any text content placed in a cell starting with an arithmetic operator with the single quote (‘) character. The expression for the “properties” command as entered in the spreadsheet is seen below. Note that we copy and paste the cell contents the actual hyphens are not copied!
This is the second time we encounter the problem of symbolic notation conflict, first with the double quote ordeal for string literals, now with special escape sequence characters. This is all because we expect too much form text as a representation.
Creating Colour Gradients
Colour in computers is traditionally expressed using its primary RGB components. The values we are allowed to use for each component (or channel) is typically an integer in [0, 255], which maps to 8-bits of information per channel and 16 mil colours in total.
The colour [0, 0, 0] is black and [255, 255, 255] is white, with [r, g, b] as components. Colours where R=G=B are greyscale tones. The colour red is [255, 0, 0], green [0, 255, 0] and blue [0, 0, 255]. To produce colour gradients we relate each channel to our time range as seen below:
Channel
Expression
Formula
Red
=INT( B2 * 255 )
Linear Gradient
Green
=INT( B2 * B2 * 255 )
Quadratic Gradient
Blue
=INT( SQRT( B2 ) * 255 )
Square Root Gradient
The “int” function truncates a real number’s decimal places keeping only the integer part, hence int. For example, “int( 3.1415 ) → 3” i.e. no rounding, just dropping the digits. Multiplying the time [0, 1] range by the maximum value allowed “255”, we can create a linear gradient that spans the entire range of reds [0, 255]. To create an “upward” accelerating gradient we use the square of the “time” range for the green channel, and for a “downward” accelerating gradient for the blue channel we use the square root of the “time” range. You may plot the square and square root functions in a graph to get a sense of the “upward” and “downward” notion.
Notice that colours behave like 3D points but with integer coordinates. The formula for the colour-to-text conversion seen below is actually very similar to the point conversion expression but here we drop all decimal points.
Practice
Curve functions of the f( x ) = y form, that pass through [0, 0] and [1, 1] and stay within the unit square are very useful because for any input they just redistribute the output. For the colour channels we used f( x ) = x, f( x ) = x * x and f( x ) = sqrt( x ) to create equally spaced and accelerating colour tones. What other re-distributive functions can you think of and what effects they have to points and colours? Model an ease-in / ease-out curve used for in cinema for smooth camera motion.
Understanding the concepts of arranging commands both horizontally and vertically; experiment with commands that require such multiple arguments.
Experiment with the properties command and modify other object attributes beyond colour. You can execute commands step-by-step manually before automating the process.
Executing command line drawing instructions has a short latency involved in parsing the command, going though command user interaction phases, updating the document and rendering the graphics on-screen. We can take advantage of this to create animations by issuing drawing commands, followed by a selection command such as “sellast” or “selall”, followed by a “delete” command. Experiment with animating e.g. the trajectory of projectile following a parabolic path or a video game sprite jumping over an obstacle.