Enum Statement

Home • Gallery • Tutorials • Download • Purchase • Site Map
 

Enum Statement Syntax

The Fractal Science Kit fractal generator enum statement is 1 of the statements used in the properties section of the program instructions to define the program's Properties.

An enum statement defines a set of constants that can be used to improve readability in your code. Each enum item includes a name, a label, and an optional complex expression. The enum statement is frequently used in combination with an option statement (whose type field is set to the enum name) to generate a set of selectable items presented in a combobox control.

The syntax of the enum statement is:

enum <EnumName> {
  <ItemName>, <ItemLabel> [, <Expression> ]
  <ItemName>, <ItemLabel> [, <Expression> ]
  ...
}

The enum name and each of the item names must be valid identifiers. The item label is a quoted string. The expression must evaluate to a complex constant and is optional.

A constant is defined for each enum item. These constants are accessible in the other sections of your instructions. The name of the constant is the enum name followed by a period (.) and then the item name. The value of the constant associated with the 1st item is 0, the 2nd item is 1, the 3rd item is 2, etc. The item expression can be included to associate a secondary value with an item, if required. The secondary value is accessed from the associated enum option (see example below).

Example:

enum ProjectionTypes {
  Stereographic, "Stereographic"
  AzimuthalEquidistant, "Azimuthal Equidistant"
  CircleLimit, "Circle Limit"
}

This statement defines an enum named ProjectionTypes with 3 items.

An enum can be used as an option type as in the following example:

option Projection {
  type = ProjectionTypes
  caption = "Projection"
  default = ProjectionTypes.Stereographic
}

This defines an option named Projection whose type is given by ProjectionTypes. Options based on enums result in combobox controls on the properties page. The combobox associated with the Projection option will have 3 items, one for each of the enum's items as shown below:

Enum Option Example

Note the option's default specification given above is the enum name followed by a period (.) and then the item name. This is also how you access an enum value in your program.

The following code shows how the Projection option can be used in your code:

z = Polar(Sample.TrapValue, Sample.TrapAngleRaw)

switch (Projection) {
 
  case ProjectionTypes.Stereographic
    r = z.r*0.5
    w = 0.5 - Sqrt(0.25-r^2)
    z = PolarToComplex(z)/(1-w)
   
  case ProjectionTypes.AzimuthalEquidistant
    z.r = Asin(z.r)
    z = PolarToComplex(z)
   
  case ProjectionTypes.CircleLimit
    z.r = Atanh(z.r)
    z = PolarToComplex(z)
}

This could just as easily been coded as:

z = Polar(Sample.TrapValue, Sample.TrapAngleRaw)

switch (Projection) {
 
  case 0
    r = z.r*0.5
    w = 0.5 - Sqrt(0.25-r^2)
    z = PolarToComplex(z)/(1-w)
   
  case 1
    z.r = Asin(z.r)
    z = PolarToComplex(z)
   
  case 2
    z.r = Atanh(z.r)
    z = PolarToComplex(z)
}

Note how the use of the enum constants in the 1st example improves the readability of the code.

Example:

enum CriticalPoints {
  P0, "1", 1
  P1, "Sqrt(-1/2)", Sqrt(-1/2)
}
option CriticalPoint {
  type = CriticalPoints
  caption = "Critical Point"
  default = CriticalPoints.P0
}

This example defines an enum named CriticalPoints and an option named CriticalPoint. An expression is included with each enum item. The expression must evaluate to a simple complex number and cannot use any of the options defined by the program. A combobox with 2 items will be created on the associated properties page to allow the user to adjust the value of the CriticalPoint option as shown below:

Enum Option Example

The following code illustrates how you can access the expression value from your instructions:

if (~IsJulia) {
  z = CriticalPoint.EnumValue
}

Here, z is set to the expression value associated with the currently selected item of the CriticalPoint option.

 

Copyright © 2004-2017 Hilbert, LLC
All rights reserved