CGA Code Lesson #2: Basic Split Concepts

In this exercise, we will model a rooftop antenna from my Willis/Sears Tower Model by segmenting a primitive cylinder and working with each section.  We will introduce the color operation by coloring each section, and we will show the importance of the NIL value when empty areas are required in a model.

CGA OPERATIONS COVERED

split()
color(r,g,b,o) // or 
color("#hexvalue")
NIL

EXERCISE

Outputs are seen in Figure 1, The lesson code begins with two attributes, one for the total height of the antenna (278 feet) and the other for the number of sides used to draw each section. I’ve also added four attributes for vertical height values (v1 thru v4) to make it easier to change values deep in the code.

Copy the attributes and then add the code blocks to your rule file one at a time, select the shape you have created and then use CTL + G to regenerate it.

  1. In Code Block 1, we draw the primitive cylinder to the maximum height and width (12 feet).
  2. In Code Block 2, we split the cylinder into four sections on the y axis. In each of the four size : operation  instances between the curly braces, we set a height and name a subsequent rule for each section.  Because we are using absolute height values, the four numbers must add up to 278.
  3. In Code Block 3, we create the rules for each section as they were named in Code Block 2. All sections are colored white.  Note we taper the overall shape with decreasing width values in each primitiveCylinder operation.
  4. Change color values in for each cylinder section in Code Block 3 to the first alternate values to get the colors shown in Figure 4 of the illustration.
  5. In the AntMid1 rule, change the color value to one of the zero opacity alternate values (preceded by //).  As seen in Figure 5, though the section has essentially been made transparent, there is still something there.
  6. To create a space in the cylinder, go to the split operation in Code Block 2 and change the  80 : AntMid1  code to 80 : NIL. Remember to leave the “|” character afterwards.  A better way to do this is by changing the AntMid1 rule to read: AntMid1 –> NIL.

What happens when we change values in the “v” attributes? See what happens when you change v1, v2, v3 and/or v4; see what happens when you change numeric values within the split operations. We will explain these in a later exercise.

Figure 1: Exercise Outputs (click to view a larger image)

CODE

/**
 * File: lesson_02.cga
 * Created: 6 Feb 2017 22:03:50 GMT
 * Author: Jeff Herzer | jeffherzer.com
 */

version "2016.1"

// Set Attribute Variables
attr Antenna_Height = 278
attr numSides = 12
attr v1 = 50
attr v2 = 40
attr v3 = 20
attr v4 = 58

// BLOCK 1

AntennaStart -->
 primitiveCylinder(numSides,12,Antenna_Height)
 AntennaSplit
 
// BLOCK 2 

AntennaSplit -->
 split(y){ 
 80 : AntBase | 
 80 : AntMid1 | 
 60 : AntMid2 | 
 58 : AntTop 
 } 

// BLOCK 3

AntBase --> 
 primitiveCylinder(numSides,12,v1)
 color("ffffff")
 //color("#ff0000") and color(1,0,0) are both red
 //color(1,0,0,0.5) is red with 50% opacity
 
AntMid1 --> 
 primitiveCylinder(numSides,10,v2)
 color("#ffffff")
 //color("#00ff00")
 //color("#00ff0000") is green with zero opacity
 //color (0,1,0,0) is green with zero opacity
 
AntMid2 --> 
 primitiveCylinder(numSides,8,v3)
 color("ffffff")
 //color("#0000ff")
 
AntTop --> 
 primitiveCylinder(numSides,5,v4)
 color("#ffffff")
 //color("#ffff00")

Leave a Reply

Your email address will not be published. Required fields are marked *