#hs.geometry

Utility object to represent points, sizes and rects in a bidimensional plane

An hs.geometry object can be:

  • a point, or vector2, with x and y fields for its coordinates
  • a size with w and h fields for width and height respectively
  • a rect, which has both a point component for one of its corners, and a size component - so it has all 4 fields
  • a unit rect, which is a rect with all fields between 0 and 1; it represents a "relative" rect within another (absolute) rect (e.g. a unit rect x=0,y=0 , w=0.5,h=0.5 is the quarter portion closest to the origin); please note that hs.geometry makes no distinction internally between regular rects and unit rects; you can convert to and from as needed via the appropriate methods

You can create these objects in many different ways, via my_obj=hs.geometry.new(...) or simply my_obj=hs.geometry(...) by passing any of the following:

  • 4 parameters X,Y,W,H for the respective fields - W and H, or X and Y, can be nil:
    • hs.geometry(X,Y) creates a point
    • hs.geometry(nil,nil,W,H) creates a size
    • hs.geometry(X,Y,W,H) creates a rect given its width and height from a corner
  • a table {X,Y} creates a point
  • a table {X,Y,W,H} creates a rect
  • a table {x=X,y=Y,w=W,h=H} creates a rect, or if you omit X and Y, or W and H, creates a size or a point respectively
  • a table {x1=X1,y1=Y1,x2=X2,y2=Y2} creates a rect, where X1,Y1 and X2,Y2 are the coordinates of opposite corners
  • a string:
    • "X Y" or "X,Y" creates a point
    • "WxH" or "W*H" creates a size
    • "X Y/WxH" or "X,Y W*H" (or variations thereof) creates a rect given its width and height from a corner
    • "X1,Y1>X2,Y2" or "X1 Y1 X2 Y2" (or variations thereof) creates a rect given two opposite corners
    • "[X,Y WxH]" or "[X1,Y1 X2,Y2]" or variations (note the square brackets) creates a unit rect where x=X/100, y=Y/100, w=W/100, h=H/100
  • a point and a size "X Y","WxH" or {x=X,y=Y},{w=W,h=H} create a rect

You can use any of these anywhere an hs.geometry object is expected in Hammerspoon; the constructor will be called for you.


#API Overview

Constructors - API calls which return an object, typically one that offers API methods

Fields - Variables which can only be accessed from an object returned by a constructor

Methods - API calls which can only be made on an object returned by a constructor


#API Documentation

#Constructors

Signaturehs.geometry.copy(geom) -> hs.geometry object
TypeConstructor
DescriptionCreates a copy of an hs.geometry object
Parameters
  • geom - an hs.geometry object to copy
Returns
  • a newly created copy of the hs.geometry object
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 151

Signaturehs.geometry.new(...) -> hs.geometry object
TypeConstructor
DescriptionCreates a new hs.geometry object
Parameters
  • ... - see the module description at the top
Returns
  • a newly created hs.geometry object
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 141

Signaturehs.geometry.point(x, y) -> hs.geometry point
TypeConstructor
DescriptionConvenience function for creating a point object
Parameters
  • x - A number containing the horizontal co-ordinate of the point
  • y - A number containing the vertical co-ordinate of the point
Returns
  • An hs.geometry point object
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 732

Signaturehs.geometry.rect(x, y, w, h) -> hs.geometry rect
TypeConstructor
DescriptionConvenience function for creating a rect-table
Parameters
  • x - A number containing the horizontal co-ordinate of the top-left point of the rect
  • y - A number containing the vertical co-ordinate of the top-left point of the rect
  • w - A number containing the width of the rect
  • h - A number containing the height of the rect
Returns
  • An hs.geometry rect object
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 717

Signaturehs.geometry.size(w, h) -> hs.geometry size
TypeConstructor
DescriptionConvenience function for creating a size object
Parameters
  • w - A number containing a width
  • h - A number containing a height
Returns
  • An hs.geometry size object
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 744

#Fields

Signaturehs.geometry.area
TypeField
DescriptionA number representing the area of this rect or size; changing it will scale the rect/size - see hs.geometry:scale()
NotesNone
Sourceextensions/geometry/geometry.lua line 333

Signaturehs.geometry.aspect
TypeField
DescriptionA number representing the aspect ratio of this rect or size; changing it will reshape the rect/size, keeping its area and center constant
NotesNone
Sourceextensions/geometry/geometry.lua line 352

Signaturehs.geometry.bottomright
TypeField
DescriptionAlias for x2y2
NotesNone
Sourceextensions/geometry/geometry.lua line 267

Signaturehs.geometry.center
TypeField
DescriptionA point representing the geometric center of this rect or the midpoint of this vector2; changing it will move the rect/vector accordingly
NotesNone
Sourceextensions/geometry/geometry.lua line 305

Signaturehs.geometry.h
TypeField
DescriptionThe height of this rect or size; changing it will keep the rect's x,y corner constant
NotesNone
Sourceextensions/geometry/geometry.lua line 218

Signaturehs.geometry.length
TypeField
DescriptionA number representing the length of the diagonal of this rect, or the length of this vector2; changing it will scale the rect/vector - see hs.geometry:scale()
NotesNone
Sourceextensions/geometry/geometry.lua line 318

Signaturehs.geometry.string
TypeField
DescriptionThe "X,Y/WxH" string for this hs.geometry object (reduced precision); useful e.g. for logging
NotesNone
Sourceextensions/geometry/geometry.lua line 281

Signaturehs.geometry.table
TypeField
DescriptionThe {x=X,y=Y,w=W,h=H} table for this hs.geometry object; useful e.g. for serialization/deserialization
NotesNone
Sourceextensions/geometry/geometry.lua line 275

Signaturehs.geometry.topleft
TypeField
DescriptionAlias for xy
NotesNone
Sourceextensions/geometry/geometry.lua line 206

Signaturehs.geometry.w
TypeField
DescriptionThe width of this rect or size; changing it will keep the rect's x,y corner constant
NotesNone
Sourceextensions/geometry/geometry.lua line 214

Signaturehs.geometry.wh
TypeField
DescriptionThe size component for this hs.geometry object; setting this to a new size will keep the rect's x,y corner constant
NotesNone
Sourceextensions/geometry/geometry.lua line 251

Signaturehs.geometry.x
TypeField
DescriptionThe x coordinate for this point or rect's corner; changing it will move the rect but keep the same width and height
NotesNone
Sourceextensions/geometry/geometry.lua line 173

Signaturehs.geometry.x1
TypeField
DescriptionAlias for x
NotesNone
Sourceextensions/geometry/geometry.lua line 181

Signaturehs.geometry.x2
TypeField
DescriptionThe x coordinate for the second corner of this rect; changing it will affect the rect's width
NotesNone
Sourceextensions/geometry/geometry.lua line 231

Signaturehs.geometry.x2y2
TypeField
DescriptionThe point denoting the other corner of this hs.geometry object; setting this to a new point will change the rect's width and height
NotesNone
Sourceextensions/geometry/geometry.lua line 263

Signaturehs.geometry.xy
TypeField
DescriptionThe point component for this hs.geometry object; setting this to a new point will move the rect but keep the same width and height
NotesNone
Sourceextensions/geometry/geometry.lua line 202

Signaturehs.geometry.y
TypeField
DescriptionThe y coordinate for this point or rect's corner; changing it will move the rect but keep the same width and height
NotesNone
Sourceextensions/geometry/geometry.lua line 177

Signaturehs.geometry.y1
TypeField
DescriptionAlias for y
NotesNone
Sourceextensions/geometry/geometry.lua line 185

Signaturehs.geometry.y2
TypeField
DescriptionThe y coordinate for the second corner of this rect; changing it will affect the rect's height
NotesNone
Sourceextensions/geometry/geometry.lua line 235

#Methods

Signaturehs.geometry:angle() -> number
TypeMethod
DescriptionReturns the angle between the positive x axis and this vector2
Parameters
  • None
Returns
  • a number representing the angle in radians
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 516

Signaturehs.geometry:angleTo(point) -> number
TypeMethod
DescriptionReturns the angle between the positive x axis and the vector connecting this point or rect's center to another point or rect's center
Parameters
  • point - an hs.geometry object, or a table or string or parameter list to construct one; if a rect, uses the rect's center
Returns
  • a number representing the angle in radians
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 529

Signaturehs.geometry:distance(point) -> number
TypeMethod
DescriptionFinds the distance between this point or rect's center and another point or rect's center
Parameters
  • point - an hs.geometry object, or a table or string or parameter list to construct one; if a rect, uses the rect's center
Returns
  • a number indicating the distance
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 542

Signaturehs.geometry:equals(other) -> boolean
TypeMethod
DescriptionChecks if two geometry objects are equal
Parameters
  • other - another hs.geometry object, or a table or string or parameter list to construct one
Returns
  • true if this hs.geometry object perfectly overlaps other, false otherwise
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 390

Signaturehs.geometry:fit(bounds) -> hs.geometry object
TypeMethod
DescriptionEnsure this rect is fully inside bounds, by scaling it down if it's larger (preserving its aspect ratio) and moving it if necessary
Parameters
  • bounds - an hs.geometry rect object, or a table or string or parameter list to construct one, indicating the rect that must fully contain this rect
Returns
  • this hs.geometry object for method chaining
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 448

Signaturehs.geometry:floor() -> hs.geometry object
TypeMethod
DescriptionTruncates all coordinates in this object to integers
Parameters
  • None
Returns
  • this hs.geometry point for method chaining
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 483

Signaturehs.geometry:fromUnitRect(frame) -> hs.geometry rect
TypeMethod
DescriptionConverts a unit rect within a given frame into a rect
Parameters
  • frame - an hs.geometry rect (with w and h >0)
Returns
  • An hs.geometry rect object
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 672

Signaturehs.geometry:inside(rect) -> boolean
TypeMethod
DescriptionChecks if this hs.geometry object lies fully inside a given rect
Parameters
  • rect - an hs.geometry rect, or a table or string or parameter list to construct one
Returns
  • true if this point/rect lies fully inside the given rect, false otherwise
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 570

Signaturehs.geometry:intersect(rect) -> hs.geometry rect
TypeMethod
DescriptionReturns the intersection rect between this rect and another rect
Parameters
  • rect - an hs.geometry rect, or a table or string or parameter list to construct one
Returns
  • a new hs.geometry rect
Notes
  • If the two rects don't intersect, the result rect will be a "projection" of the second rect onto this rect's
  • closest edge or corner along the x or y axis; the w and/or h fields in the result rect will be 0.
ExamplesNone
Sourceextensions/geometry/geometry.lua line 585

Signaturehs.geometry:move(point) -> hs.geometry object
TypeMethod
DescriptionMoves this point/rect
Parameters
  • point - an hs.geometry object, or a table or string or parameter list to construct one, indicating the x and y displacement to apply
Returns
  • this hs.geometry object for method chaining
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 403

Signaturehs.geometry:normalize() -> point
TypeMethod
DescriptionNormalizes this vector2
Parameters
  • None
Returns
  • this hs.geometry point for method chaining
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 468

Signaturehs.geometry:rotateCCW(aroundpoint, ntimes) -> hs.geometry point
TypeMethod
DescriptionRotates a point around another point N times
Parameters
  • aroundpoint - an hs.geometry point to rotate this point around
  • ntimes - the number of times to rotate, defaults to 1
Returns
  • A new hs.geometry point containing the location of the rotated point
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 695

Signaturehs.geometry:scale(size) -> hs.geometry object
TypeMethod
DescriptionScales this vector2/size, or this rect keeping its center constant
Parameters
  • size - an hs.geometry object, or a table or string or parameter list to construct one, indicating the factors for scaling this rect's width and height; if a number, the rect will be scaled by the same factor in both axes
Returns
  • this hs.geometry object for method chaining
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 419

Signaturehs.geometry:toUnitRect(frame) -> hs.geometry unit rect
TypeMethod
DescriptionConverts a rect into its unit rect within a given frame
Parameters
  • frame - an hs.geometry rect (with w and h >0)
Returns
  • An hs.geometry unit rect object
Notes
  • The resulting unit rect is always clipped within frame's bounds (via hs.geometry:intersect()); if frame
  • does not encompass this rect no error will be thrown, but the resulting unit rect won't be a direct match with this rect
  • (i.e. calling :fromUnitRect(frame) on it will return a different rect)
ExamplesNone
Sourceextensions/geometry/geometry.lua line 644

Signaturehs.geometry:type() -> string
TypeMethod
DescriptionReturns the type of an hs.geometry object
Parameters
  • None
Returns
  • a string describing the type of this hs.geometry object, i.e. 'point', 'size', 'rect' or 'unitrect'; nil if not a valid object
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 45

Signaturehs.geometry:union(rect) -> hs.geometry rect
TypeMethod
DescriptionReturns the smallest rect that encloses both this rect and another rect
Parameters
  • rect - an hs.geometry rect, or a table or string or parameter list to construct one
Returns
  • a new hs.geometry rect
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 555

Signaturehs.geometry:vector(point) -> point
TypeMethod
DescriptionReturns the vector2 from this point or rect's center to another point or rect's center
Parameters
  • point - an hs.geometry object, or a table or string or parameter list to construct one; if a rect, uses the rect's center
Returns
  • an hs.geometry point
NotesNone
ExamplesNone
Sourceextensions/geometry/geometry.lua line 500