VPPS-SS14-W05-1-Supernasen



# calculates the x and y coordinates of the missing corner points of a
# rectangle based on the given coordinates of one corner point
# 
# Args: x.known: the x-coordinate of the given corner point y.known: the
# y-coordinate of the given corner point orientation: bearing from
# magnetic north decliantion: difference between magnetic north and grid
# north size.x: the length of the rectangle border in x-direction size.y:
# the length of the rectangle border in y-direction
calcCorners <- function(x.known, y.known, orientation, declination, size.x, 
    size.y) {

    calculateTriangle <- function(alpha, c) {
        alphaRad <- alpha * pi/180
        a <- sin(alphaRad) * c
        b <- cos(alphaRad) * c
        result <- data.frame(x = round(a, 2), y = round(b, 2))
        return(result)
    }

    alpha <- orientation + declination
    alpha2 <- alpha + 90
    alpha3 <- alpha + 270
    distAB <- calculateTriangle(alpha, size.y)
    distAD <- calculateTriangle(alpha, size.x)
    distBC <- calculateTriangle(alpha2, size.x)
    distDC <- calculateTriangle(alpha3, size.y)
    A <- (c(x.known, y.known))
    B <- (c(y.known + distAB, x.known))
    D <- (c(x.known + distAD, y.known))
    C <- (c(B[1] + distBC, D[1] + distDC))
    Corners <- (c(A, B, C, D))
    Corners
}

# example of function usage
calcCorners(481054, 5645540, 6, 1.48, 48, 30)
## [[1]]
## [1] 481054
## 
## [[2]]
## [1] 5645540
## 
## $x
## [1] 5645544
## 
## $y
## [1] 5645570
## 
## [[5]]
## [1] 481054
## 
## $x
## [1] 5645592
## 
## $y
## [1] 5645538
## 
## $x
## [1] 481031
## 
## $y
## [1] 481064
## 
## $x
## [1] 481060
## 
## $y
## [1] 481102
## 
## [[12]]
## [1] 5645540

Leave a Reply