Objects

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

Program Object Types

Fractal Science Kit program object types are defined using an Object statement.

Object statements are only valid where macros are defined:

  • Built-in Macros - This contains the built-in object definitions.
  • My Macros - This is where you should place your object definitions.
  • macros section - Here you can place object definitions specific to one program.

The syntax of the Object statement is:

Object <ObjectName> {
  <FieldName>
  <FieldName>
  ...
}

Both the object name and the field names must be valid identifiers.

For example, the built-in Mobius object is defined using the statement:

Object Mobius {
  A
  B
  C
  D
}

Alternatively, you can use the statement separator character (:) to place the fields on the same line as shown below:

Object Mobius { A : B : C : D }

These declare a new type named Mobius which is composed of 4 fields named A, B, C, and D. Each of these fields represents a complex number. Objects can contain only complex fields. An object field that is itself an object is not supported.

To refer to a field within an object, you name the object followed by a period (.) and the field name.

For example, the following statement assigns the value 0 to the field A of a Mobius object named myMobius:

myMobius.A = 0

In rare cases, it is desirable to refer to the same object field using different names. For example, the Color object's 1st component can be referenced using any of the names R, Red, H, or Hue. This allows the same object to support the RGB, HSL, and HSV color models.

The built-in Color object is defined using the statement:

Object Color {
  R|Red  |H|Hue
  G|Green|S|Saturation
  B|Blue |L|Lightness |V|Value
  A|Alpha
}

In most cases, fields are given a single name as in the Mobius object. However, in those rare cases where multiple names for a field is required, the field can be defined by including each of the names separated by the vertical bar character (|) as in the Color object definition above.

Example:

Color rgbColor = Color(0.8, 0.2, 0.2, 1)
Color hslColor = Color.RGBToHSL(rgbColor)

In the above example, hslColor is the HSL representation of rgbColor so in the interest of code clarity, we would access the hue of hslColor using hslColor.Hue even though this is equivalent to using hslColor.Red since both access the 1st component!

Each object is provided a constructor which can be used to create the object. The constructor name is the name of the object. To create an object, you invoke the constructor with arguments for each of the object's fields. When an Object statement is compiled, the object constructor is automatically created by the compiler.

Example:

Mobius identity = Mobius(1, 0, 0, 1)

This example uses the Mobius constructor to create a Mobius object and assigns it to the variable identity. The arguments to the constructor are used to initialize the fields A, B, C, and D, respectively.

The number of fields associated with a given object type can be found using the Object.Size function.

Example:

size = Object.Size(Mobius)

This sets size equal to 4, the number of fields in a Mobius object.

 

Copyright © 2004-2017 Hilbert, LLC
All rights reserved