Ellipse: R6 class representing an ellipse

EllipseR Documentation

R6 class representing an ellipse

Description

An ellipse is given by a center, two radii (rmajor and rminor), and the angle (alpha) between the major axis and the horizontal direction.

Active bindings

center

get or set the center

rmajor

get or set the major radius of the ellipse

rminor

get or set the minor radius of the ellipse

alpha

get or set the angle of the ellipse

degrees

get or set the degrees field

Methods

Public methods


Method new()

Create a new Ellipse object.

Usage
Ellipse$new(center, rmajor, rminor, alpha, degrees = TRUE)
Arguments
center

a point, the center of the rotation

rmajor

positive number, the major radius

rminor

positive number, the minor radius

alpha

a number, the angle between the major axis and the horizontal direction

degrees

logical, whether alpha is given in degrees

Returns

A new Ellipse object.

Examples
Ellipse$new(c(1,1), 3, 2, 30)

Method print()

Show instance of an Ellipse object.

Usage
Ellipse$print(...)
Arguments
...

ignored


Method isEqual()

Check whether the reference ellipse equals an ellipse.

Usage
Ellipse$isEqual(ell)
Arguments
ell

An Ellipse object.


Method equation()

The coefficients of the implicit equation of the ellipse.

Usage
Ellipse$equation()
Details

The implicit equation of the ellipse is Ax² + Bxy + Cy² + Dx + Ey + F = 0. This method returns A, B, C, D, E and F.

Returns

A named numeric vector.


Method includes()

Check whether a point lies on the reference ellipse.

Usage
Ellipse$includes(M)
Arguments
M

a point


Method contains()

Check whether a point is contained in the reference ellipse.

Usage
Ellipse$contains(M)
Arguments
M

a point


Method matrix()

Returns the 2x2 matrix S associated to the reference ellipse. The equation of the ellipse is t(M-O) %*% S %*% (M-O) = 1.

Usage
Ellipse$matrix()
Examples
ell <- Ellipse$new(c(1,1), 5, 1, 30)
S <- ell$matrix()
O <- ell$center
pts <- ell$path(4L) # four points on the ellipse
apply(pts, 1L, function(M) t(M-O) %*% S %*% (M-O))

Method path()

Path that forms the reference ellipse.

Usage
Ellipse$path(npoints = 100L, closed = FALSE, outer = FALSE)
Arguments
npoints

number of points of the path

closed

Boolean, whether to return a closed path; you don't need a closed path if you want to plot it with polygon

outer

Boolean; if TRUE, the ellipse will be contained inside the path, otherwise it will contain the path

Returns

A matrix with two columns x and y of length npoints.

Examples
library(PlaneGeometry)
ell <- Ellipse$new(c(1, -1), rmajor = 3, rminor = 2, alpha = 30)
innerPath <- ell$path(npoints = 10)
outerPath <- ell$path(npoints = 10, outer = TRUE)
bbox <- ell$boundingbox()
plot(NULL, asp = 1, xlim = bbox$x, ylim = bbox$y, xlab = NA, ylab = NA)
draw(ell, border = "red", lty = "dashed")
polygon(innerPath, border = "blue", lwd = 2)
polygon(outerPath, border = "green", lwd = 2)

Method diameter()

Diameter and conjugate diameter of the reference ellipse.

Usage
Ellipse$diameter(t, conjugate = FALSE)
Arguments
t

a number, the diameter only depends on t modulo pi; the axes correspond to t=0 and t=pi/2

conjugate

logical, whether to return the conjugate diameter as well

Returns

A Line object or a list of two Line objects if conjugate = TRUE.

Examples
ell <- Ellipse$new(c(1,1), 5, 2, 30)
diameters <- lapply(c(0, pi/3, 2*pi/3), ell$diameter)
plot(NULL, asp = 1, xlim = c(-4,6), ylim = c(-2,4),
     xlab = NA, ylab = NA)
draw(ell)
invisible(lapply(diameters, draw))

Method perimeter()

Perimeter of the reference ellipse.

Usage
Ellipse$perimeter()

Method pointFromAngle()

Intersection point of the ellipse with the half-line starting at the ellipse center and forming angle theta with the major axis.

Usage
Ellipse$pointFromAngle(theta, degrees = TRUE)
Arguments
theta

a number, the angle, or a numeric vector

degrees

logical, whether theta is given in degrees

Returns

A point of the ellipse if length(theta)==1 or a two-column matrix of points of the ellipse if length(theta) > 1 (one point per row).


Method pointFromEccentricAngle()

Point of the ellipse with given eccentric angle.

Usage
Ellipse$pointFromEccentricAngle(t)
Arguments
t

a number, the eccentric angle in radians, or a numeric vector

Returns

A point of the ellipse if length(t)==1 or a two-column matrix of points of the ellipse if length(t) > 1 (one point per row).


Method semiMajorAxis()

Semi-major axis of the ellipse.

Usage
Ellipse$semiMajorAxis()
Returns

A segment (Line object).


Method semiMinorAxis()

Semi-minor axis of the ellipse.

Usage
Ellipse$semiMinorAxis()
Returns

A segment (Line object).


Method foci()

Foci of the reference ellipse.

Usage
Ellipse$foci()
Returns

A list with the two foci.


Method tangent()

Tangents of the reference ellipse at a point given by its eccentric angle.

Usage
Ellipse$tangent(t)
Arguments
t

eccentric angle, there is one tangent for each value of t modulo 2*pi; for t = 0, pi/2, pi, -pi/2, these are the tangents at the vertices of the ellipse

Examples
ell <- Ellipse$new(c(1,1), 5, 2, 30)
tangents <- lapply(c(0, pi/3, 2*pi/3, pi, 4*pi/3, 5*pi/3), ell$tangent)
plot(NULL, asp = 1, xlim = c(-4,6), ylim = c(-2,4),
     xlab = NA, ylab = NA)
draw(ell, col = "yellow")
invisible(lapply(tangents, draw, col = "blue"))

Method normal()

Normal unit vector to the ellipse.

Usage
Ellipse$normal(t)
Arguments
t

a number, the eccentric angle in radians of the point of the ellipse at which we want the normal unit vector

Returns

The normal unit vector to the ellipse at the point given by eccentric angle t.

Examples
ell <- Ellipse$new(c(1,1), 5, 2, 30)
t_ <- seq(0, 2*pi, length.out = 13)[-1]
plot(NULL, asp = 1, xlim = c(-5,7), ylim = c(-3,5),
     xlab = NA, ylab = NA)
draw(ell, col = "magenta")
for(i in 1:length(t_)){
  t <- t_[i]
  P <- ell$pointFromEccentricAngle(t)
  v <- ell$normal(t)
  draw(Line$new(P, P+v, FALSE, FALSE))
}

Method theta2t()

Convert angle to eccentric angle.

Usage
Ellipse$theta2t(theta, degrees = TRUE)
Arguments
theta

angle between the major axis and the half-line starting at the center of the ellipse and passing through the point of interest on the ellipse

degrees

logical, whether theta is given in degrees

Returns

The eccentric angle of the point of interest on the ellipse, in radians.

Examples
O <- c(1, 1)
ell <- Ellipse$new(O, 5, 2, 30)
theta <- 20
P <- ell$pointFromAngle(theta)
t <- ell$theta2t(theta)
tg <- ell$tangent(t)
OP <- Line$new(O, P, FALSE, FALSE)
plot(NULL, asp = 1, xlim = c(-4,6), ylim = c(-2,5),
     xlab = NA, ylab = NA)
draw(ell, col = "antiquewhite")
points(P[1], P[2], pch = 19)
draw(tg, col = "red")
draw(OP)
draw(ell$semiMajorAxis())
text(t(O+c(1,0.9)), expression(theta))

Method regressionLines()

Regression lines. The regression line of y on x intersects the ellipse at its rightmost point and its leftmost point. The tangents at these points are vertical. The regression line of x on y intersects the ellipse at its topmost point and its bottommost point. The tangents at these points are horizontal.

Usage
Ellipse$regressionLines()
Returns

A list with two Line objects: the regression line of y on x and the regression line of x on y.

Examples
ell <- Ellipse$new(c(1,1), 5, 2, 30)
reglines <- ell$regressionLines()
plot(NULL, asp = 1, xlim = c(-4,6), ylim = c(-2,4),
     xlab = NA, ylab = NA)
draw(ell, lwd = 2)
draw(reglines$YonX, lwd = 2, col = "blue")
draw(reglines$XonY, lwd = 2, col = "green")

Method boundingbox()

Return the smallest rectangle parallel to the axes which contains the reference ellipse.

Usage
Ellipse$boundingbox()
Returns

A list with two components: the x-limits in x and the y-limits in y.

Examples
ell <- Ellipse$new(c(2,2), 5, 3, 40)
box <- ell$boundingbox()
plot(NULL, asp = 1, xlim = box$x, ylim = box$y, xlab = NA, ylab = NA)
draw(ell, col = "seaShell", border = "blue")
abline(v = box$x, lty = 2); abline(h = box$y, lty = 2)

Method randomPoints()

Random points on or in the reference ellipse.

Usage
Ellipse$randomPoints(n, where = "in")
Arguments
n

an integer, the desired number of points

where

"in" to generate inside the ellipse, "on" to generate on the ellipse

Returns

The generated points in a two columns matrix with n rows.

Examples
ell <- Ellipse$new(c(1,1), 5, 2, 30)
pts <- ell$randomPoints(100)
plot(NULL, type="n", asp=1, xlim = c(-4,6), ylim = c(-2,4),
     xlab = NA, ylab = NA)
draw(ell, lwd = 2)
points(pts, pch = 19, col = "blue")

Method clone()

The objects of this class are cloneable with this method.

Usage
Ellipse$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `Ellipse$new`
## ------------------------------------------------

Ellipse$new(c(1,1), 3, 2, 30)

## ------------------------------------------------
## Method `Ellipse$matrix`
## ------------------------------------------------

ell <- Ellipse$new(c(1,1), 5, 1, 30)
S <- ell$matrix()
O <- ell$center
pts <- ell$path(4L) # four points on the ellipse
apply(pts, 1L, function(M) t(M-O) %*% S %*% (M-O))

## ------------------------------------------------
## Method `Ellipse$path`
## ------------------------------------------------

library(PlaneGeometry)
ell <- Ellipse$new(c(1, -1), rmajor = 3, rminor = 2, alpha = 30)
innerPath <- ell$path(npoints = 10)
outerPath <- ell$path(npoints = 10, outer = TRUE)
bbox <- ell$boundingbox()
plot(NULL, asp = 1, xlim = bbox$x, ylim = bbox$y, xlab = NA, ylab = NA)
draw(ell, border = "red", lty = "dashed")
polygon(innerPath, border = "blue", lwd = 2)
polygon(outerPath, border = "green", lwd = 2)

## ------------------------------------------------
## Method `Ellipse$diameter`
## ------------------------------------------------

ell <- Ellipse$new(c(1,1), 5, 2, 30)
diameters <- lapply(c(0, pi/3, 2*pi/3), ell$diameter)
plot(NULL, asp = 1, xlim = c(-4,6), ylim = c(-2,4),
     xlab = NA, ylab = NA)
draw(ell)
invisible(lapply(diameters, draw))

## ------------------------------------------------
## Method `Ellipse$tangent`
## ------------------------------------------------

ell <- Ellipse$new(c(1,1), 5, 2, 30)
tangents <- lapply(c(0, pi/3, 2*pi/3, pi, 4*pi/3, 5*pi/3), ell$tangent)
plot(NULL, asp = 1, xlim = c(-4,6), ylim = c(-2,4),
     xlab = NA, ylab = NA)
draw(ell, col = "yellow")
invisible(lapply(tangents, draw, col = "blue"))

## ------------------------------------------------
## Method `Ellipse$normal`
## ------------------------------------------------

ell <- Ellipse$new(c(1,1), 5, 2, 30)
t_ <- seq(0, 2*pi, length.out = 13)[-1]
plot(NULL, asp = 1, xlim = c(-5,7), ylim = c(-3,5),
     xlab = NA, ylab = NA)
draw(ell, col = "magenta")
for(i in 1:length(t_)){
  t <- t_[i]
  P <- ell$pointFromEccentricAngle(t)
  v <- ell$normal(t)
  draw(Line$new(P, P+v, FALSE, FALSE))
}

## ------------------------------------------------
## Method `Ellipse$theta2t`
## ------------------------------------------------

O <- c(1, 1)
ell <- Ellipse$new(O, 5, 2, 30)
theta <- 20
P <- ell$pointFromAngle(theta)
t <- ell$theta2t(theta)
tg <- ell$tangent(t)
OP <- Line$new(O, P, FALSE, FALSE)
plot(NULL, asp = 1, xlim = c(-4,6), ylim = c(-2,5),
     xlab = NA, ylab = NA)
draw(ell, col = "antiquewhite")
points(P[1], P[2], pch = 19)
draw(tg, col = "red")
draw(OP)
draw(ell$semiMajorAxis())
text(t(O+c(1,0.9)), expression(theta))

## ------------------------------------------------
## Method `Ellipse$regressionLines`
## ------------------------------------------------

ell <- Ellipse$new(c(1,1), 5, 2, 30)
reglines <- ell$regressionLines()
plot(NULL, asp = 1, xlim = c(-4,6), ylim = c(-2,4),
     xlab = NA, ylab = NA)
draw(ell, lwd = 2)
draw(reglines$YonX, lwd = 2, col = "blue")
draw(reglines$XonY, lwd = 2, col = "green")

## ------------------------------------------------
## Method `Ellipse$boundingbox`
## ------------------------------------------------

ell <- Ellipse$new(c(2,2), 5, 3, 40)
box <- ell$boundingbox()
plot(NULL, asp = 1, xlim = box$x, ylim = box$y, xlab = NA, ylab = NA)
draw(ell, col = "seaShell", border = "blue")
abline(v = box$x, lty = 2); abline(h = box$y, lty = 2)

## ------------------------------------------------
## Method `Ellipse$randomPoints`
## ------------------------------------------------

ell <- Ellipse$new(c(1,1), 5, 2, 30)
pts <- ell$randomPoints(100)
plot(NULL, type="n", asp=1, xlim = c(-4,6), ylim = c(-2,4),
     xlab = NA, ylab = NA)
draw(ell, lwd = 2)
points(pts, pch = 19, col = "blue")

PlaneGeometry documentation built on Aug. 10, 2023, 1:09 a.m.