Skip to contents

The base vector type for geometries in euclid is the euclid_geometry class. While this is a virtual class (you cannot construct it, only its subclasses), many of the common vector operations are defined here. Further it is possible to check whether a vector is a geometry of any type using is_geometry(). While most geometries are atomic, a few are defined by multiples of the same geometry, e.g. segments and triangles which are defined by 2 and 3 points respectively. The cardinality of each geometry in a vector can be queried with cardinality(). The matrix conversion will place each sub-geometry of a geometry on a new row, meaning that the final number of rows in a matrix constructed from x is not length(x) but sum(cardinality(x)).




cardinality(x, ...)



An euclid_geometry object


Arguments passed on to methods

Vector behaviour

Geometry vectors in euclid are made to behave as closely as possible to what you expect from normal R vectors. However, they are implemented as external pointers to the exact C representation meaning that they cannot be restored across sessions or saved to RData/RDS files. Despite being external pointers they mimick R's copy-on-modify semantics so you should not worry about side effects when changing a geometry vector.

The following is a list of standard R methods defined for geometry vectors:

  • as.matrix() - converts the geometry to a standard R matrix of numerics

  • as.character() - provides a textual representation of the geometry

  • format() - as above

  • as.list() - splits the vector into single elements in a list

  • str() - provides a condensed view of the vector

  • length() - gives the number of geometries in the vector

  • rep() - replicates elements in the vector

  • seq() - interpolate between two geometries of the same type and dimensionality

  • dim() - gives the dimensionality of the geometry (2 or 3)

  • [[] and [[[] - extract elements from the vector

  • [[<-] and [[[<-] - assigns elements into the vector. Since NA values are not supported it is not possible to assign past the length of the vector

  • $ and $<- - will throw an error since geometry vectors are unnamed

  • c() - combine multiple geometries of the same type

  • unique() - returns the unique elements of the vector keeping the order

  • duplicated() - gives whether an element has been seen before in the vector

  • anyDuplicated() - Tells if any element in the vector is a duplicate

  • == and != - Test for equality between elements.

  • transform() - Transform geometries in the vector according to an affine_transformation

Do note that since geometries with exact representation is not easily hashable, the implementation of unique() and duplicated() is not very efficient (except for points since they can be sorted).

See also

Other Geometry methods: def(), subgeometries


p <- point(sample(10, 4), sample(10, 4))
#> <2D points [4]>
#> [1] <x:7, y:8>  <x:10, y:7> <x:6, y:9>  <x:4, y:5> 
#> <2D points [1]>
#> [1] <x:10, y:7>
p[3] <- point(14, 20)
#> <2D points [4]>
#> [1] <x:7, y:8>   <x:10, y:7>  <x:14, y:20> <x:4, y:5>  
p2 <- c(p, p)
#> <2D points [8]>
#> [1] <x:7, y:8>   <x:10, y:7>  <x:14, y:20> <x:4, y:5>   <x:7, y:8>  
#> [6] <x:10, y:7>  <x:14, y:20> <x:4, y:5>  
#> <2D points [4]>
#> [1] <x:7, y:8>   <x:10, y:7>  <x:14, y:20> <x:4, y:5>  
#> <2D points [4]>
#> [1] <x:4, y:5>   <x:7, y:8>   <x:10, y:7>  <x:14, y:20>
rep(p, each = 2)
#> <2D points [8]>
#> [1] <x:7, y:8>   <x:7, y:8>   <x:10, y:7>  <x:10, y:7>  <x:14, y:20>
#> [6] <x:14, y:20> <x:4, y:5>   <x:4, y:5>  
#> [[1]]
#> <2D points [1]>
#> [1] <x:7, y:8>
#> [[2]]
#> <2D points [1]>
#> [1] <x:10, y:7>
#> [[3]]
#> <2D points [1]>
#> [1] <x:14, y:20>
#> [[4]]
#> <2D points [1]>
#> [1] <x:4, y:5>
transform(p, affine_rotate(pi/3))
#> <2D points [4]>
#> [1] <x:-3.43, y:10.1> <x:-1.06, y:12.2> <x:-10.3, y:22.1> <x:-2.33, y:5.96>