get_rectangle: Convert quadrangle to rectangle

Description Usage Arguments Value Examples

View source: R/anem_geoprocessing.R

Description

Convert 4 lines to a rectangular box with only right angles

Usage

1
get_rectangle(bounds)

Arguments

bounds

a data.frame containing 4 lines (rows) defined by columns bID and, either x1, y1, x2, and y2, or m and b

Value

Returns a data.frame containing slope and intercept for four edges of a rectangle. The rectangle is determined by (1) identifying the quadrangle of the input bounds, (2) selecting the midpoints of each edge of the quadrangle, (3) determining the "long" axis of the quadrangle, which becomes the long axis of the rectangle, (4) calculating the slope of the long and short axes of the rectangle (at right angles), then generating lines with these slopes through the midpoints.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
## Not run: 
# Example 1
bounds <- data.frame(x1=c(0,10,13,1),y1=c(0,10,9,-1),
  x2=c(10,13,1,0),y2=c(10,9,-1,0))
rect_boundaries <- get_rectangle(bounds)

library(ggplot2)
ggplot() + geom_segment(data=bounds,aes(x1,y1,xend=x2,yend=y2)) +
  geom_segment(data=rect_boundaries,
    aes(x1,y1,xend=x2,yend=y2,color=as.factor(bID))) +
  coord_equal()

# Example 2
bounds2 <- data.frame(m=c(2,4,-1,-1),b=c(0,4,3,20))
rect_boundaries2 <- get_rectangle(bounds2)
ggplot() + geom_abline(data=bounds2,aes(slope=m,intercept=b)) +
  geom_segment(data=rect_boundaries2,
    aes(x1,y1,xend=x2,yend=y2,color=as.factor(bID))) +
  coord_equal()

# Example 3
bounds3 <- data.frame(m=c(Inf,0,Inf,0),b=c(0,4,4,0))
rect_boundaries3 <- get_rectangle(bounds3)
ggplot() +
  geom_abline(data=bounds3[bounds3$m!=Inf,],
    aes(slope=m,intercept=b),linetype="dashed") +
  geom_vline(data=bounds3[bounds3$m==Inf,],
    aes(xintercept=b),linetype="dashed") +
  geom_segment(data=rect_boundaries3,
    aes(x1,y1,xend=x2,yend=y2,color=as.factor(bID))) +
  coord_equal()

# Example 4
bounds4 <- data.frame(bID=1:4,x1=c(0,0,10,10),y1=c(0,10,10,0),
  x2=c(0,10,10,0),y2=c(10,10,0,0))
rect_boundaries4 <- get_rectangle(bounds4)
ggplot() + geom_segment(data=bounds4,aes(x1,y1,xend=x2,yend=y2)) +
  geom_segment(data=rect_boundaries4,
    aes(x1,y1,xend=x2,yend=y2,color=as.factor(bID))) +
  coord_equal()

## End(Not run)

gopalpenny/anem documentation built on Dec. 20, 2020, 5:27 a.m.