Trace lines from a specific point and angle

4

I'm looking for a way to draw a line from a point and an angle (azimuth) and then build a polygon with the intersection of these lines.

  base <- data_frame(
    id = c(1, 2, 3),
    x = c(10, 5, 3),
    y = c(5, 10, 4),
    azimute = c(330, 120, 45),
  )

In the example below, each id has an x / y coordinate and an angle of view (azimuth, where 0/360 is north, 90 is west, and so on).

The example lines were built using trigonometry ('on hand'), but my interest is to do in a SIG (sf or sp) package as the project involves points within a Brazilian state and spatial reference is important. p>

The challenge is to build the rows from every id using the azimuths. the length of the line does not matter, it will be a big value to make sure they intersect.

The end goal is to create a polygon created from the intersection of the segments.

    
asked by anonymous 29.12.2017 / 13:32

1 answer

0

As challenges are composed of two parts, the challenge is to define them. These two parts are the intercept and slope (which are also the% required% for aes() ).

The first step is to achieve tilt based on an angle in degrees. For this we can calculate the tangent with geom_abline() . It deserves attention the fact that the argument passed to it must be in radians.

inclinacao <- function(graus) {
  radiano <- x  * pi / 180
  tan(radiano)
}
To define the intercept based on the information we have so far (coordinates tan() and x of point and slope of the line), we can define the following function:

intercepto <- function(x, y, b) {
  y - x * b
}

Now, with this tooling in hand, we can add the intercept and slope to our table and then create the chart.

library(tidyverse)

base2 <- base %>% 
  mutate(b = inclinacao(azimute),
         a = intercepto(x, y, b)) 
base2

# A tibble: 3 x 6
     id     x     y azimute      b     a
  <dbl> <dbl> <dbl>   <dbl>  <dbl> <dbl>
1     1    10     5     330 -0.577  10.8
2     2     5    10     120 -1.73   18.7
3     3     3     4      45  1.000   1. 

ggplot(base2, aes(x, y)) +
  geom_point() +
  geom_abline(aes(intercept = a, slope = b), linetype = 2) +
  geom_label(aes(label = glue::glue("id:{id}\nAz:{azimute}"), 
                 y = y + c(0.5, -0.5, 0.5)))
  coord_cartesian(xlim = c(2, 11), ylim = c(3, 11))

    
12.12.2018 / 11:38