#==========================================================================================#
#==========================================================================================#
# Polygon in clockwise direction. #
#------------------------------------------------------------------------------------------#
poly.clockwise <<- function(x,y=NULL){
#----- Check whether y is null or is given as data frame. ------------------------------#
if (is.null(y)){
#----- Find x and y. ----------------------------------------------------------------#
ix = which(names(x) %in% c("x","X"))
iy = which(names(x) %in% c("y","Y"))
y = x[[iy]]
x = x[[ix]]
#------------------------------------------------------------------------------------#
}else if (length(x) != length(y)){
stop("x and y must have the same length!")
}#end if
#---------------------------------------------------------------------------------------#
#------ Make sure this is a closed path. -----------------------------------------------#
nxy = length(x)
if (! ( x[1] %==% x[nxy] && y[1] %==% y[nxy] ) ){
x = c(x,x[1])
y = c(y,y[1])
nxy = nxy + 1
}#end if (! ( x[1] %==% x[nxy] && y[1] %==% y[nxy] ) )
#---------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------#
# Normalise the coordinates to reduce numerical erros whilst finding the #
# determinant of the orientation matrix. #
#---------------------------------------------------------------------------------------#
xyscale = max(diff(range(x,finite=TRUE)),diff(range(y,finite=TRUE)))
xnorm = ( x - min(x,na.rm=TRUE) ) / xyscale
ynorm = ( y - min(y,na.rm=TRUE) ) / xyscale
#---------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------#
# Find the sum of the ratio of differential x and sum of y. #
#---------------------------------------------------------------------------------------#
aa = sequence(nxy-2)
bb = aa + 1
cc = aa + 2
deto = ( (xnorm[bb] - xnorm[aa])*(ynorm[cc]-ynorm[aa])
- (xnorm[cc] - xnorm[aa])*(ynorm[bb]-ynorm[aa]) )
orient = sum(deto,na.rm=TRUE)
#---------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------#
# Revert the polygon if it is anti-clockwise. #
#---------------------------------------------------------------------------------------#
if (orient >= 0.0){
ans = data.frame( X = rev(x), Y = rev(y) )
}else{
ans = data.frame( X = x , Y = y )
}#end if
#---------------------------------------------------------------------------------------#
#----- Return the answer. --------------------------------------------------------------#
return(ans)
#---------------------------------------------------------------------------------------#
}#end function poly.clockwise
#==========================================================================================#
#==========================================================================================#
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.