Piero Alessio
Piero Alessio

Reputation: 13

Coordinates of an n-sided polygon in R

My goal is to develope a R script that returns the set of (s, c) coordinates necessary to build an n-sided polygon inscribed in a unit circle.

I am following this article by Phillip Burger on how to create a radar-chart in Tableau. His approach uses R in order to calculate a background image. His code to return the set of (s, c) coordinates necessary to build a 5-sided polygon can be found here.

Building on Phillip Burger's code, does anyone has a suggestion on how to modify his code to generate n-sided polygon coordinates?

I am working on a business case where I need n=3 and n=10 set of coordinates.

Thank you very much,

Piero

Edit: I the desired output format is a data frame that contains the columns pathID;pathOrder;xCoordinate;yCoordinate - it is necessary for drawing the lines in Tableau later on.

Here is the sample output for a 5-sided polygon

pathID;pathOrder;xCoordinate;yCoordinate
61;1;0;0
62;1;0;0
63;1;0;0
64;1;0;0
65;1;0;0
61;2;0;1,1
62;2;1,046162168;0,339918694
63;2;0,646563778;-0,889918694
64;2;-0,646563778;-0,889918694
65;2;-1,046162168;0,339918694
0;1;0;0
0.25;1;0;0,25
0.5;1;0;0,5
0.75;1;0;0,75
1;1;0;1
0;2;0;0
0.25;2;0,237764129;0,077254249
0.5;2;0,475528258;0,154508497
0.75;2;0,713292387;0,231762746
1;2;0,951056516;0,309016994
0;3;0;0
0.25;3;0,146946313;-0,202254249
0.5;3;0,293892626;-0,404508497
0.75;3;0,440838939;-0,606762746
1;3;0,587785252;-0,809016994
0;4;0;0
0.25;4;-0,146946313;-0,202254249
0.5;4;-0,293892626;-0,404508497
0.75;4;-0,440838939;-0,606762746
1;4;-0,587785252;-0,809016994
0;5;0;0
0.25;5;-0,237764129;0,077254249
0.5;5;-0,475528258;0,154508497
0.75;5;-0,713292387;0,231762746
1;5;-0,951056516;0,309016994
0;6;0;0
0.25;6;0;0,25
0.5;6;0;0,5
0.75;6;0;0,75
1;6;0;1

Here is a R-code snippet that I want to modify

# Name: radar-chart-pentagon.R
# Author: Phillip Burger
# Date: August 11, 2013
# Purpose: return the set of (s, c) coordinates necessary to build a pentagon 
# inscribed in a unit circle. Includes three, inner polygons at 75 percent,
# 50 percent, and 25 percent of unit radius. Shape is oriented with center point
# at (0, 0). Original inspiration was to provide coordinates necessary to build
# a pentagon-shaped radar chart in Tableau, where generated points are used to 
# define a path within Tableau. Includes path coordinates for three sample 
# departments. 
# Posted: http://www.phillipburger.net/wordpress/2013/08/11/radar-chart-in-tableau/
# Reference: For pentagon coordinates, http://mathworld.wolfram.com/Pentagon.html
# ToDo: Curently working only working for the shape of a pentagon. Next steps:
# 1) extend to general case of n-sided polygon
# 2) extend to t-intermediate (inner) polygons.

suppressPackageStartupMessages(require(aspace))

SHAPE <- "pentagon"
LINE <- "line"
RADIUS <- 1 # unit radius circle
CENTERX <- 0  # in radians
CENTERY <- 0  # in radians

setwd("C:/Users/Dropbox")
outputFile <- paste("radar-chart-pentagon-with-deptsample", ".txt", sep = "")	

shapePoints <- matrix(NA, 1, 4, dimnames = NULL)
linePoints <- matrix(NA, 1, 4, dimnames = NULL)


polyCoords<-function(n){
	sq<-2*pi*(0:n)/n
	cbind(sin(sq),cos(sq))
}
plot(polyCoords(10),type='l')


# Function: coordinates
# Purpose: calculate the coordinates of a polygon
# Parameters: radius - radius of the polygon
# Returns: matrix containing coordinates of five polygons including origin
coordinates <- function(radius) {	
	cCoord1 <- radius * cos(2 * pi / 5)
	cCoord2 <- radius * cos(pi / 5)
	sCoord1 <- radius * sin(2 * pi / 5)
	sCoord2 <- radius * sin(4 * pi / 5)
	cat("c and s values for radius: ", radius, " \n", sep = "")
	cat("c1: ", cCoord1, " \n", sep = "") 
	cat("c2: ", cCoord2, " \n", sep = "") 
	cat("s1: ", sCoord1, " \n", sep = "") 
	cat("s2: ", sCoord2, " \n\n", sep = "")
	pointsBuild <- matrix(NA, 1, 4, dimnames = NULL)
	pointsBuild <- rbind(pointsBuild, c(radius, 1, 0, radius))
	pointsBuild <- rbind(pointsBuild, c(radius, 2, sCoord1, cCoord1))
	pointsBuild <- rbind(pointsBuild, c(radius, 3, sCoord2, -cCoord2)) 
	pointsBuild <- rbind(pointsBuild, c(radius, 4, -sCoord2, -cCoord2))
	pointsBuild <- rbind(pointsBuild, c(radius, 5, -sCoord1, cCoord1)) 
	pointsBuild <- rbind(pointsBuild, c(radius, 6, 0, radius))
	return(pointsBuild)
}

#
# Create five polygons
#
for(radius in c(1.0, 0.75, 0.50, 0.25, 0)) {
	shapePoints <- rbind(shapePoints, coordinates(radius = radius))
}

# Initialize output data
linePoints <- rbind(linePoints, coordinates(radius = RADIUS))

shapePoints <- cbind(rep(SHAPE, nrow(shapePoints)), shapePoints)
linePoints <- cbind(rep(LINE, nrow(linePoints)), linePoints)
dfShapePoints <- as.data.frame(na.omit(rbind(shapePoints, linePoints)))

names <- c("shape",
		"pathID",
		"pathOrder",
		"xCoordinate",
		"yCoordinate")
names(dfShapePoints) <- names

# Write output file
write.table(dfShapePoints[ , ], 
		file = outputFile,
		sep = "\t",
		append = FALSE,
		col.names = TRUE,
		row.names = FALSE,
		quote = FALSE)

Upvotes: 1

Views: 1017

Answers (1)

mrip
mrip

Reputation: 15163

This code will draw an n-sided polygon with radius 1.

polyCoords<-function(n){
  sq<-2*pi*(0:n)/n
  cbind(sin(sq),cos(sq))
}
plot(polyCoords(10),type='l')

Note that it repeats the first coordinate, so that the plot will be closed. If you don't want this, then change the 0:n to 1:n.

enter image description here

Upvotes: 5

Related Questions