## `plot::Rectangle2d` -- graphical primitive for a two-dimensional rectangle

#### Introduction

`plot::Rectangle2d``(p, w, h)` represents a plot of a two-dimensional rectangle with lower left corner p = (px; py), width w and height h.

#### Creating Elements

`plot::Rectangle2d(c, w, h <, option1, option2...>)`
`plot::Rectangle2d(p, w, h <, option1, option2...>)`

#### Parameters

`c` | - | a list of two arithmetical expressions
`p` | - | a two-dimensional point, i.e., an object of the domain `plot::Point` or `DOM_POINT`
`w, h` | - | arithmetical expressions
`option1, option2, ...` | - | plot option(s) of the form `OptionName = value`

#### Related Domains

`plot::Ellipse2d`, `plot::Point`, `plot::Polygon`, `plot::Scene`, `RGB`

#### Related Functions

`plot`, `plot2d`, `plot::copy`, `plot::line`

#### Details

- Objects generated by `plot::Rectangle2d` represent graphical primitives for two-dimensional rectangles that can be displayed via the clal `plot(...)`, or used with other graphical primitives of the `plot` library. See example 1.
- If the first parameter of the call of `plot::Rectangle2d` is a point `p`, then it is converted into a list of the two coordinates of `p`. Specified plot options for `p` are ignored! (Cf. example 2.)
- The values for the width `w` and height `h` can also be negative numbers. The rectangle drawn consists of the four points p = (px; py), (px; py + h), (px + w; py + h) and (px + w; py).
- An object of `plot::Rectangle2d` has type `"graphprim"`, i.e., if `o` is such an object, then the result of `type(o)` is the string `"graphprim"`.
- Options `option1`, `option2`... are specified by equations `OptionName = value`. The following table gives an overview of the available options:

  `OptionName` | admissible values | default value
  Color | `[Flat]`, `[Flat, [r,g,b]]`, `[Height]`, `[Height, [r,g,b], [R,G,B]]`, `[Function, f]` | `[Flat, RGB::Red]`
  Filled | `TRUE`, `FALSE` | `FALSE`
  LineStyle | SolidLines, DashedLines | SolidLines
  LineWidth | positive integers | `1`
  PointStyle | Circles, FilledCircles, FilledSquares, Squares | FilledSquares
  PointWidth | positive integers | `30`
  Title | strings |
  TitlePosition | `[x, y]` |

  See `plot2d` and `polygon` for further details on each option.

- Scene options for the parameters `option1`, `option2`... are not allowed! One may pass scene options to the call of `plot`, or use `plot::Scene` to create an object representing a graphical scene. Cf. example 1.

- A so-called attribute of a graphical primitive is a named entry of the object which can be accessed via the slot operator `::`.

  Each attribute has the property ``read'', i.e., the value of an attribute `attr` of a graphical primitive `o` can be read with `o::attr`. If the attribute also has the ``write'' property, then the value of the attribute can be changed with `o::attr := new_value`.

  The following attributes are available for a rectangle primitive:

  attribute | meaning | properties
  `corner` | a list of two arithmetical expressions being the lower left corner of the rectangle. The initial value is the parameter `c`, or the list of the coordinates of `p`, respectively. | read/write
  `dimension` | The dimension of the rectangle primitive, i.e., the integer 2. | read
  `height` | The height of the rectangle (an arithmetical expression). The initial value is the parameter `h`. | read/write
  `options` | A table of plot options of the rectangle primitive. Note that if you change the value of this attribute, the entries of the assigned table are not checked to be valid plot options for rectangle primitives. Invalid entries lead to runtime errors. The initial value of this attribute is the table stored under the domain entry `"defaultOptions"`, where such options are replaced and added, respectively, which are given with the parameters `option1`, `option2`... of the creating call. | read/write
  `plotdata` | List of the plot data of the rectangle primitive in a `plot2d` and `plot3d` conforming syntax, respectively (see the method `"getPlotdata"` below). Note that the value of this attribute should only be used if the attribute `refreshPlotdata` has the value `FALSE` (see below). | read
  `refreshPlotdata` | A boolean value which signals whether the plot data of the rectangle primitive must be (re-)build with the method `"getPlotdata"` (see below). If its value is `FALSE`, then the plot data of the rectangle primitive is stored in the attribute `plotdata`. The initial value is `TRUE`. | read/write
  `width` | The width of the rectangle (an arithmetical expression). The initial value is the parameter `w`. | read/write

  See example 2.

#### Operands

An object of `plot::Rectangle2d` consists of the three operands `c`, `w` and `h`.

#### Important Operations

Operands of a rectangle primitive can be accessed either using the system function `op`, the index operator `[ ]`, or using the attributes `corner`, `height` and `width` described above. For example, if `rectangle` is such an object, then the calls `op(rectangle,1)`, `rectangle[1]` and `rectangle::corner` return the list representing the lower left corner of `rectangle`.

Via `rectangle[1] := new_point` or `rectangle::corner := new_point`, the lower left corner of `rectangle` is replaced by the list `new_point`.

See the methods `"op"`, `"_index"`, `"set_index"` and `"slot"` below.

Use the slot operator `::` to get or set plot options of such objects afterwards, i.e., when they have been created. For example, if `rectangle` is such an object, then `rectangle::Color := RGB::Red` changes the color of of `rectangle` to red.

#### Result of Evaluation

Evaluating an object of type `plot::Rectangle2d` returns itself.

#### Function Call

Calling an object of `plot::Rectangle2d` as a function yields the object itself, regardless of the arguments. The arguments are not evaluated.

#### Entries

defaultOptions

  is a table of plot options for rectangle primitives and their default values. Each entry has the form `OptionName = default`.

  When an object of the domain `plot::Rectangle2d` is created, then a copy of this table is stored under the attribute `options` (see the table of attributes above), where those options are added and replaced, respectively, which are given by the (optional) parameters `option1`, `option2`... of the creating call (see ``Creating Elements'' above).

  Plot options, which are not contained in this table, will not be included in the plot data of the object created by the method `"getPlotdata"` (see below).

optionNames

  is a set of the available options for plots of two-dimensional rectangles.

#### Method _index: indexed access to the operands of a rectangle primitive

- `_index(dom rectangle, positive integer i)`
- - Returns the `i`th operand of `rectangle`. See ``Operands'' above for a description of the operands of `curve`. If `i` is greater than 2, then `FAIL` is returned.
  - This method overloads the system function `_index`, i.e., one may use it in the form `rectangle[i]`, or in functional notation `_index(rectangle, i)`.

#### Method dimension: dimension of a rectangle primitive

- `dimension(dom rectangle)`
- - Returns the integer 2.

#### Method getPlotdata: create the plot data of a rectangle primitive

- `getPlotdata(dom rectangle)`
- - Returns a list of an inner list, where the inner list is a plot description of `rectangle` in a `plot2d` conforming syntax, i.e., it has the form `[Mode = List, [polygon(...)], ...]`.

    For example, with `s := plot::Rectangle2d::getPlotdata(rectangle)` the call `plot2d(s[1])` gives a plot of `rectangle`.

  - Only those plot options will be included in the plot data of the rectangle, that are contained in the table stored under the attribute `options` (see the table of attributes above). For any other plot option not contained in this table, the corresponding default value set by the function `plot2d` for curves is used when plotting the object.
  - The result is stored as the value of the attribute `plotdata` of `rectangle`.
  - A call of this method sets the value of the attribute `refreshPlotdata` of `rectangle` to `FALSE`.
  - This method is called from `plot::Scene` to build the plot data of the graphical scene.

#### Method nops: number of operands of a rectangle primitive

- `nops(dom rectangle)`
- - Returns the integer 3.
  - This method overloads the system function `nops`, i.e., one may use it in the form `nops(rectangle)`.

#### Method op: extract operands of a rectangle primitive

- `op(dom rectangle, positive integer i)`
- - Returns the `i`th operand of `rectangle`. See ``Operands'' above for a description of the operands of `rectangle`. If `i` is greater than 3, then `FAIL` is returned.
  - This method overloads the system function `op`, i.e., one may use it in the form `op(rectangle, i)`.

#### Method set_index: set operands of a rectangle primitive

- `set_index(dom rectangle, positive integer i, any val)`
- - Replaces the `i`th operand of `rectangle` by the value `val`. See ``Operands'' above for a description of the operands of `rectangle`.
  - If `i` is greater than 3, or if `val` is not an admissible value for the `i`th operand, then a warning message is issued. In this case the call of this method has no effect on the object `rectangle`.
  - A call of this method sets the value of the attribute `refreshPlotdata` of `rectangle` to `TRUE`.

#### Method slot: read and write attributes and plot options

- `slot(dom rectangle, string slotname)`
- - Reads the value of the slot `slotname` of `rectangle`. `slotname` may either be the name of an attribute or the name of a plot option. See the tables of available plot options and attributes above.
  - If `slotname` is the name of a plot option, but the option is not contained in the table stored under the attribute `options`, then `FAIL` is returned.

    If `slotname` is an invalid attribute or option, then an error message is issued.

  - This method overloads the system function `slot`, i.e., one may use it in the form `rectangle::slotname_id` (here, `slotname_id` must be the identifier corresponding to the string `slotname`), or in functional notation `slot(rectangle, slotname)`.
- `slot(dom rectangle, string slotname, any val)`
- - Changes the value of the attribute or option with the name `slotname` to the value `val`.
  - If there is no attribute or option with the name `slotname`, or if `val` is not an admissible value for `slotname`, then a warning message is issued. In this case, the value of `slotname` remains unchanged.
  - This method overloads the system function `slot`, i.e., one may use it in the form `rectangle::slotname_id := val` (here, `slotname_id` must be the identifier corresponding to the string `slotname`), or in functional notation `slot(rectangle, slotname, val)`.
  - The value of the attribute `refreshPlotdata` of `rectangle` is set to `TRUE`.

#### Method checkOption: check a plot option

- `checkOption(equation OptionName = value)`
- - This method checks whether `OptionName` is a known plot option for rectangle primitives (see the table of available plot options above), and `value` is an admissible value for this option.
  - If both is correct, then the list `[TRUE, OptionName, newValue]` is returned. Note that the value of the option could have been converted into an admissible format. Thus, `newValue` must be used as the value of the option `OptionName` instead of `value`.
  - Otherwise, the list `[FALSE, error_msg]` is returned. The string `error_msg` is a description of the located problem, which can be passed, for example, to the system function `error` to raise a user-specified exception.

#### Method copy: create a copy of a rectangle primitive

- `copy(dom rectangle)`
- - Returns a copy of the object `rectangle`.
  - This method is called from the function `plot::copy`. See its help page for details.

#### Method modify: modify a copy of a rectangle primitive

- `modify(dom rectangle, equation(s) Name1 = value1...)`
- - Creates a copy of the object `rectangle` and changes the slots `Name1`... of this copy to the new values `value1`...
  - The identifiers `Name1`... must be names of attributes or plot options of the domain `plot::Rectangle2d`. Otherwise a warning message is issued, and the slot remains unchanged. Also, if one of the values `value1`... is not an admissible value for the corresponding attribute or plot option, respectively, the change of the slot is ignored.

    See the tables of available options and attributes above.

  - A call of this method sets the value of the attribute `refreshPlotdata` of the copy of `rectangle` to `TRUE`.
  - This method is called from the function `plot::modify`.

#### Method print: print a rectangle primitive

- `print(dom rectangle)`
- - This method returns an unevaluated expression of the form `plot::Rectangle2d(p, w, h)`. It is used to print objects of `plot::Rectangle2d` to the screen.
  - See the system function `print` for details.

#### Example 1

We define a rectangle with lower left corner at point (1;2), width 3 and height 4, and a square of length one with lower left corner at the point (0;0):

    >> r := plot::Rectangle2d([1, 2], 3, 4);
       s := plot::Rectangle2d([0, 0], 1, 1, Filled = TRUE)

                          plot::Rectangle2d([1, 2], 3, 4)

                          plot::Rectangle2d([0, 0], 1, 1)

The area of the square is filled in the color of the border of the rectangle (which is red by default). We plot these two objects in a graphical scene, without showing axes:

    >> plot(r, s, Axes = None)

#### Example 2

The attribute `corner`, which specifies the lower left point of the rectangle, is a list of two arithmetical expressions also if the corner point of the object created was given as an object of the domain `plot::Point` or `DOM_POINT`:

    >> c := plot::Point([-1, 1]):
       r := plot::Rectangle2d(c, 2, -2):
       r::corner

                                      [-1, 1]

If you replace the value of the attribute `corner`, then the point must be given as a list of two arithmetical expressions, otherwise a warning message is issued, saying that the assignment is ignored:

    >> r::corner:= point(0, 0)

          Warning: 3rd argument: expecting a list of two arithmetical ex\
          pressions; assignment ignored [plot::Rectangle2d::slot]

                                    point(0, 0)

    >> r::corner

                                      [-1, 1]

Note that if you specify an object of the domain `plot::Point` or `DOM_POINT` as the corner of the rectangle, then plot options of the point are ignored. For example, if we change the color of the point `c` created above to blue and create a new rectangle:

    >> c::Color := RGB::Blue: r := plot::Rectangle2d(c, 1, 1):
       plot(r)

then the rectangle is still drawn in red color (the default color of objects of the domain `plot::Rectangle2d`). You must use the color option of the object `r` to change the color of the rectangle:

    >> r::Color := RGB::Blue: plot(r)

See the help page of `plot::Curve2d` for more examples for working with attributes of graphical primitives.

#### Changes

- new function

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000