# Class Geometry

GoJS® Diagramming Components
version 3.0.13
by Northwoods Software®

The Geometry class is used to define the "shape" of a Shape. A Geometry can be simple straight lines, rectangles, or ellipses. A Geometry can also be an arbitrarily complex path, consisting of a list of PathFigures.

A Geometry must not be modified once it has been used by a Shape. However, a Geometry may be shared by multiple Shapes.

It is commonplace to create Geometries using geometry path string syntax: Geometry Path Strings. However it is much more efficient to create Geometries programmatically. One way to do that is illustrated by several of the samples that evaluate JavaScript such as:

    new go.Geometry()
      .add(new go.PathFigure(p.x, p.y)
            .add(new go.PathSegment(go.SegmentType.Arc, -sweep/2, sweep, 0, 0, radius+layerThickness, radius+layerThickness))
            .add(new go.PathSegment(go.SegmentType.Line, q.x, q.y))
            .add(new go.PathSegment(go.SegmentType.Arc, sweep/2, -sweep, 0, 0, radius, radius).close()));

See samples that make use of Geometries in the samples index.

#####
Index

### Constructors

constructor

### Accessors

bounds
defaultStretch
endX
endY
figures
spot1
spot2
startX
startY
type

### Methods

add
computeBoundsWithoutOrigin
containsPoint
copy
getAngleAlongPath
getFractionForPoint
getPointAlongPath
normalize
offset
rotate
scale
fillPath
parse
stringify
stringifyFixed

### Properties

Ellipse
Line
Path
Rectangle

## Constructors

### constructor

- new Geometry(type?: GeometryType): Geometry

- #### Parameters

  - ##### `Optional` type: GeometryType

    Construct an empty Geometry. The geometry type must be a GeometryType. The default type is GeometryType.Path.

  #### Returns Geometry

## Accessors

### `Readonly` bounds
: Rect

- This read-only property returns a rectangle that contains all points within the Geometry. The result will always contain the origin (0, 0).

### defaultStretch
: GeometryStretch

- Gets or sets the Shape.geometryStretch value the Shape should use by default when the Shape.geometryStretch value is GeometryStretch.None. The default value is GeometryStretch.Fill. Some figure generators return a Geometry with this property set to GeometryStretch.Uniform, in order to preserve its aspect ratio when used by a Shape that may have different sizes.

### endX
: number

- Gets or sets the ending X coordinate of the Geometry if it is of type Line, Rectangle, or Ellipse. The initial value is zero.

### endY
: number

- Gets or sets the ending Y coordinate of the Geometry if it is of type Line, Rectangle, or Ellipse. The initial value is zero.

### figures
: List<PathFigure>

- Gets or sets the List of PathFigures that describes the content of the path for Geometries of type Path.

### spot1
: Spot

- Gets or sets the spot an "Auto" Panel will use for the top-left corner of any panel content when the Shape.spot1 value is Spot.Default. The default value is Spot.TopLeft, at the top-left point of the bounds of the Shape.

### spot2
: Spot

- Gets or sets the spot an "Auto" Panel will use for the bottom-right corner of any panel content when the Shape.spot2 value is Spot.Default. The default value is Spot.BottomRight, at the bottom-right point of the bounds of the Shape.

### startX
: number

- Gets or sets the starting X coordinate of the Geometry if it is of type Line, Rectangle, or Ellipse. The initial value is zero.

### startY
: number

- Gets or sets the starting Y coordinate of the Geometry if it is of type Line, Rectangle, or Ellipse. The initial value is zero.

### type
: GeometryType

- Gets or sets the type of the Geometry. The default type is GeometryType.Path.

## Methods

### add

- add(figure: PathFigure): Geometry

- Add a PathFigure to the figures list.

  #### Parameters

  - ##### figure: PathFigure

    a newly allocated unshared PathFigure that will become owned by this Geometry

  #### Returns Geometry

  this

### computeBoundsWithoutOrigin

- computeBoundsWithoutOrigin(): Rect

- Computes the Geometry's bounds without adding an origin point, and returns those bounds as a rect. This method does not modify the Geometry or its bounds.

  #### Returns Rect

### containsPoint

- containsPoint(p: Point, sw?: number): boolean

- Returns true if the Geometry contains the point.

  #### Parameters

  - ##### p: Point

    in local geometry coordinates

  - ##### `Optional` sw: number

    half the stroke width that a Shape has or that you want to pretend it has

  #### Returns boolean

  `since`

  2.2

### `Virtual` copy

- copy(): Geometry

- Create a copy of this Geometry, with the same values and figures.

  #### Returns Geometry

### getAngleAlongPath

- getAngleAlongPath(fraction: number): number

- Returns the slope expressed as an angle at the fractional distance (0-1) along this Geometry's path, in local coordinates.

  #### Parameters

  - ##### fraction: number

    A fractional amount between 0 and 1, inclusive.

  #### Returns number

  `since`

  2.1

### getFractionForPoint

- getFractionForPoint(pt: Point): number

- Returns the fractional distance (0-1) along this Geometry's path for a nearby point.

  #### Parameters

  - ##### pt: Point

    A Point, in local coordinates, near this Geometry.

  #### Returns number

  A fractional amount between 0 and 1, inclusive.

  `since`

  2.1

### getPointAlongPath

- getPointAlongPath(fraction: number, result?: Point): Point

- Returns the point at the fractional distance (0-1) along this Geometry's path.

  #### Parameters

  - ##### fraction: number

    A fractional amount between 0 and 1, inclusive.

  - ##### `Optional` result: Point

    an optional Point that is modified and returned; otherwise it allocates and returns a new Point.

  #### Returns Point

  the Point, in local coordinates, of the fractional distance along the path.

  `since`

  2.1

### normalize

- normalize(): Point

- Normalizes the Geometry points in place by ensuring the top-left bounds of the geometry lines up with (0, 0), returning the Point (x, y) amount it was shifted. After this method is called there will be no negative X or Y value for the Geometry's bounds, and no empty space at the top-left of the Geometry.

  #### Returns Point

### offset

- offset(x: number, y: number): Geometry

- Offsets the Geometry in place by a given (x, y) amount

  #### Parameters

  - ##### x: number

    The x-axis offset factor.

  - ##### y: number

    The y-axis offset factor.

  #### Returns Geometry

  this

### rotate

- rotate(angle: number, x?: number, y?: number): Geometry

- Rotates the Geometry in place by a given angle, with optional x and y values to rotate the geometry about. If no x and y value are given, (0, 0) is used as the rotation point.

  #### Parameters

  - ##### angle: number

    The angle to rotate by.

  - ##### `Optional` x: number

    The optional X point to rotate the geometry about. If no point is given, this value is 0.

  - ##### `Optional` y: number

    The optional Y point to rotate the geometry about. If no point is given, this value is 0.

  #### Returns Geometry

  this

### scale

- scale(x: number, y: number): Geometry

- Scales the Geometry in place by a given (x, y) scale factor.

  If you want to flip a Geometry horizontally, call `geo.scale(-1, 1)`. If you want to flip a Geometry vertically, call `geo.scale(1, -1)`.

  #### Parameters

  - ##### x: number

    The x-axis scale factor.

  - ##### y: number

    The y-axis scale factor.

  #### Returns Geometry

  this

### `Static` fillPath

- fillPath(str: string): string

- Given a SVG or GoJS path string, returns a congruent path string with each pathfigure filled. For instance, "M0 0 L22 22 L33 0" would become "F M0 0 L22 22 L33 0".

  #### Parameters

  - ##### str: string

  #### Returns string

### `Static` parse

- parse(str: string, filled?: boolean): Geometry

- Produce a Geometry from a string that uses an SVG-like compact path geometry syntax. The syntax accepts all SVG Path Syntax (SVG Path Syntax (w3.org)), as well as three GoJS-specific tokens. Specifically, the following tokens are allowed:

  - `M (x,y)+` - Move commands
  - `L (x,y)+, H (x)+, V (y)+` Lines commands, including horizontal and vertical lines
  - `C (x1 y1 x2 y2 x y)+, S (x2 y2 x y)+` Cubic bezier curves
  - `Q (x1 y1 x y)+, T (x y)+` Quadratic bezier curves
  - `A (rx ry x-axis-rotation large-arc-flag clockwise-flag x y)+` Arcs (following SVG arc conventions)
  - `Z` Denotes that the previous subpath is closed

  Additionally there are some tokens specific to GoJS:

  - `B (startAngle, sweepAngle, centerX, centerY, radius)+` Arcs following GoJS canvas arc conventions
  - `X` Used before M-commands to denote separate PathFigures instead of a subpath
  - `F` Denotes whether the current PathFigure is filled (true if F is present)
  - `F0` (instead of `F`) Denotes that the PathFigure should use the even-odd fill rule instead of the nonzero winding number rule (setting PathFigure.isEvenOdd)
  - `U` Denotes that the PathFigure is not shadowed

  See the Introduction page on Geometry Parsing for more details.

  #### Parameters

  - ##### str: string

  - ##### `Optional` filled: boolean

    whether figures should be filled. If true, all PathFigures in the string will be filled regardless of the presence of an "F" command or not. If false, all PathFigures will determine their own filled state by the presence of an "F" command or not. Default is false.

  #### Returns Geometry

### `Static` stringify

- stringify(val: Geometry): string

- This static function can be used to write out a Geometry as a string that can be read by Geometry.parse.

  The string produced by this method is a superset of the SVG path string rules that contains some additional GoJS-specific tokens. See the Introduction page on Geometry Parsing for more details.

  #### Parameters

  - ##### val: Geometry

    a Geometry to be rendered as a string

  #### Returns string

### `Static` stringifyFixed

- stringifyFixed(digits: number): ((val: Geometry) => string)

- This static function returns a function that can be used as a back converter for a Binding to write out a Geometry as a string with its numbers at a fixed number of digits after the decimal point.

  This is useful for limiting the size of JSON output and making it more legible. It might also be useful for regression testing.

  #### Parameters

  - ##### digits: number

    must be a non-negative integer

  #### Returns ((val: Geometry) => string)

  a function that converts a Geometry to a string without so many decimals

  - - (val: Geometry): string

    - This static function returns a function that can be used as a back converter for a Binding to write out a Geometry as a string with its numbers at a fixed number of digits after the decimal point.

      This is useful for limiting the size of JSON output and making it more legible. It might also be useful for regression testing.

      #### Parameters

      - ##### val: Geometry

      #### Returns string

      a function that converts a Geometry to a string without so many decimals

      `since`

      3.0

  `since`

  3.0

## Properties

### `Static` `Readonly` Ellipse

`deprecated`

See GeometryType.Ellipse.

### `Static` `Readonly` Line

`deprecated`

See GeometryType.Line.

### `Static` `Readonly` Path

`deprecated`

See GeometryType.Path.

### `Static` `Readonly` Rectangle

`deprecated`

See GeometryType.Rectangle.

Copyright © 1998-2024 by Northwoods Software Corporation.