Module ui.geometry

Utilities for 2D geometry.

All of these apply to full rectangles:

local Geom = require("ui/geometry")
Geom:new{ x = 1, y = 0, w = Screen:scaleBySize(100), h = Screen:scaleBySize(200), }

Some behaviour is defined for points:

Geom:new{ x = 0, y = 0, }

Some behaviour is defined for dimensions:

Geom:new{ w = Screen:scaleBySize(600), h = Screen:scaleBySize(800), }

Just use it on simple tables that have x, y and/or w, h or define your own types using this as a metatable.

Where BlitBuffer is concerned, a point at (0, 0) means the top-left corner.

Functions

Geom:copy () Makes a deep copy of itself.
Geom:offsetBy (dx, dy) Offsets rectangle or point by relative values
Geom:offsetTo (x, y) Offsets rectangle or point to certain coordinates
Geom:scaleBy (zx, zy) Scales rectangle (top-left corner is rounded down, bottom-right corner is rounded up) or dimension
Geom:transformByScale (zx, zy)

This method also takes care of x and y on top of Geom:scaleBy, c.f., fzroundrect in MµPDF,

  <https://github.com/ArtifexSoftware/mupdf/blob/d00de0e96a4a5ec90ffc30837d40cd624a6a89e0/source/fitz/geometry.c#L400-L416>
Geom:area () Returns area of itself.
Geom:changeSizeBy (dw, dh) Enlarges or shrinks dimensions or rectangles
Geom:combine (rect_b) Returns a new outer rectangle that contains both us and a given rectangle
Geom:intersect (rect_b) Returns a new rectangle for the part that we and a given rectangle share
Geom:notIntersectWith (rect_b) Returns true if self does not share any area or edge with rect_b
Geom:intersectWith (rect_b) Returns true if self geom shares area with rect_b.
Geom:notOpenIntersectWith (rect_b) Returns true if self does not share any area with rect_b
Geom:openIntersectWith (rect_b) Returns true if self geom shares area or an edge with rect_b.
Geom:setSizeTo (rect_b) Set size of dimension or rectangle to size of given dimension/rectangle.
Geom:contains (geom) Checks whether geom is within current rectangle

Works for dimensions, too.

Geom:__eq (rect_b) Checks for equality.
Geom:equalSize (rect_b) Checks the size of a dimension/rectangle for equality.
Geom:__lt (rect_b) Checks if our size is smaller than the size of the given dimension/rectangle.
Geom:__le (rect_b) Checks if our size is smaller or equal to the size of the given dimension/rectangle.
Geom:offsetWithin (rect_b, dx, dy) Offsets the current rectangle by dx, dy while fitting it into the space of a given rectangle.
Geom:centerWithin (rect_b, dx, dy) Centers the current rectangle at position x and y of a given rectangle.
Geom:distance (rect_b) Returns the Euclidean distance between two geoms.
Geom:midpoint (geom) Returns the midpoint of two geoms.
Geom:center () Returns the center point of this geom.
Geom:clear () Resets an existing Geom object to zero.
Geom:isEmpty () Checks if a dimension or rectangle is empty.
Geom.boundingBox (rectangles) Returns a bounding box which encompasses all passed rectangles.

Tables

Geom Represents a full rectangle (all fields are set), a point (x & y are set), or a dimension (w & h are set).


Functions

Geom:copy ()
Makes a deep copy of itself.

Returns:

    Geom
Geom:offsetBy (dx, dy)
Offsets rectangle or point by relative values

Parameters:

  • dx integer x delta
  • dy integer y delta
Geom:offsetTo (x, y)
Offsets rectangle or point to certain coordinates

Parameters:

  • x integer new x
  • y integer new y
Geom:scaleBy (zx, zy)
Scales rectangle (top-left corner is rounded down, bottom-right corner is rounded up) or dimension If a single factor is given, it is applied to both width and height

Parameters:

  • zx integer scale for x axis
  • zy integer scale for y axis
Geom:transformByScale (zx, zy)

This method also takes care of x and y on top of Geom:scaleBy, c.f., fzroundrect in MµPDF,

  <https://github.com/ArtifexSoftware/mupdf/blob/d00de0e96a4a5ec90ffc30837d40cd624a6a89e0/source/fitz/geometry.c#L400-L416>

Parameters:

  • zx integer scale for x axis
  • zy integer scale for y axis
Geom:area ()
Returns area of itself.

Returns:

    int
Geom:changeSizeBy (dw, dh)
Enlarges or shrinks dimensions or rectangles Note that for rectangles the offset stays the same

Parameters:

  • dw integer width delta
  • dh integer height delta
Geom:combine (rect_b)
Returns a new outer rectangle that contains both us and a given rectangle Works for rectangles, dimensions and points

Parameters:

Returns:

    Geom
Geom:intersect (rect_b)
Returns a new rectangle for the part that we and a given rectangle share

Parameters:

Returns:

    Geom
Geom:notIntersectWith (rect_b)
Returns true if self does not share any area or edge with rect_b

Parameters:

Geom:intersectWith (rect_b)
Returns true if self geom shares area with rect_b.

Parameters:

Geom:notOpenIntersectWith (rect_b)
Returns true if self does not share any area with rect_b

Parameters:

Geom:openIntersectWith (rect_b)
Returns true if self geom shares area or an edge with rect_b.

Parameters:

Geom:setSizeTo (rect_b)
Set size of dimension or rectangle to size of given dimension/rectangle.

Parameters:

Geom:contains (geom)
Checks whether geom is within current rectangle

Works for dimensions, too. For points, it is basically an equality check.

Parameters:

Geom:__eq (rect_b)
Checks for equality.

Works for rectangles, points, and dimensions.

Parameters:

Geom:equalSize (rect_b)
Checks the size of a dimension/rectangle for equality.

Parameters:

Geom:__lt (rect_b)
Checks if our size is smaller than the size of the given dimension/rectangle.

Parameters:

Geom:__le (rect_b)
Checks if our size is smaller or equal to the size of the given dimension/rectangle.

Parameters:

Geom:offsetWithin (rect_b, dx, dy)
Offsets the current rectangle by dx, dy while fitting it into the space of a given rectangle.

This can also be called with dx=0 and dy=0, which will fit the current rectangle into the given rectangle.

Parameters:

  • rect_b Geom
  • dx integer
  • dy integer
Geom:centerWithin (rect_b, dx, dy)
Centers the current rectangle at position x and y of a given rectangle.

Parameters:

  • rect_b Geom
  • dx integer
  • dy integer
Geom:distance (rect_b)
Returns the Euclidean distance between two geoms.

Parameters:

Geom:midpoint (geom)
Returns the midpoint of two geoms.

Parameters:

Returns:

    Geom
Geom:center ()
Returns the center point of this geom.

Returns:

    Geom
Geom:clear ()
Resets an existing Geom object to zero.

Returns:

    Geom
Geom:isEmpty ()
Checks if a dimension or rectangle is empty.

Returns:

    bool
Geom.boundingBox (rectangles)
Returns a bounding box which encompasses all passed rectangles.

Parameters:

  • rectangles Geom to encompass

Returns:

    Geom bounding box or nil if no rectangles passed

Tables

Geom
Represents a full rectangle (all fields are set), a point (x & y are set), or a dimension (w & h are set).

Fields:

  • x left origin
  • y top origin
  • w width
  • h height
generated by LDoc 1.5.0 Last updated 2024-11-30 18:55:14