Skip to content

Instantly share code, notes, and snippets.

@tim-salabim
Last active October 28, 2017 11:56
Show Gist options
  • Save tim-salabim/387c6179faf826f810938a542d1f55ca to your computer and use it in GitHub Desktop.
Save tim-salabim/387c6179faf826f810938a542d1f55ca to your computer and use it in GitHub Desktop.
perp points R version
## helper functions to convert degrees to radians and vice versa =============
deg_r = function(radians) {
180 * radians / pi
}
rad_r = function(degrees) {
degrees * pi / 180
}
## ===========================================================================
## convert delta x and delta y to direction and length (or u & v to wd & ws)
## ===========================================================================
calc_dir = function(x, y) {
w = deg_r(atan2(y, x))
w = ifelse(w <= 0, w + 360, w)
dir = 90 - w
dir = ifelse(w >= 90, dir + 360, dir)
return(dir)
}
calc_len = function(x, y) {
sqrt(x*x + y*y)
}
## ===========================================================================
## convert direction and length to delta x and delta y (or wd & ws to u & v)
## ===========================================================================
calc_delta_x = function(dir, len) {
len * sin(rad_r(dir))
}
calc_delta_y = function(dir, len) {
len * cos(rad_r(dir))
}
## ===========================================================================
## perp point calculation
## ===========================================================================
## delta x on the right side (to be added to midpoint)
perp_rx = function(dir, len) {
right = dir + 90
calc_delta_x(right, len)
}
## delta y on the right side (to be added to midpoint)
perp_ry = function(dir, len) {
left = dir + 90
calc_delta_y(left, len)
}
## delta x on the left side (to be added to midpoint)
perp_lx = function(dir, len) {
right = dir + 270
calc_delta_x(right, len)
}
## delta y on the left side (to be added to midpoint)
perp_ly = function(dir, len) {
left = dir + 270
calc_delta_y(left, len)
}
## calculate perp points
calc_perp_point_r = function(x_offset, y_offset, dist) {
dir = calc_dir(x_offset, y_offset)
len = calc_len(x_offset, y_offset)
x1_offset = calc_delta_x(dir, len * 1) + perp_rx(dir, dist)
y1_offset = calc_delta_y(dir, len * 1) + perp_ry(dir, dist)
mat = cbind(x1_offset, y1_offset)
colnames(mat) = NULL
return(mat)
}
calc_perp_point_l = function(x_offset, y_offset, dist) {
dir = calc_dir(x_offset, y_offset)
len = calc_len(x_offset, y_offset)
x1_offset = calc_delta_x(dir, len * 1) + perp_lx(dir, dist)
y1_offset = calc_delta_y(dir, len * 1) + perp_ly(dir, dist)
mat = cbind(x1_offset, y1_offset)
colnames(mat) = NULL
return(mat)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment