简体   繁体   English

R中n边多边形的坐标

[英]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. 我的目标是开发一个R脚本,该脚本返回建立一个单位圆内接的n边多边形所需的(s,c)坐标集。

I am following this article by Phillip Burger on how to create a radar-chart in Tableau. 我正在关注Phillip Burger的这篇文章 ,内容涉及如何在Tableau中创建雷达图。 His approach uses R in order to calculate a background image. 他的方法使用R来计算背景图像。 His code to return the set of (s, c) coordinates necessary to build a 5-sided polygon can be found here . 他的代码可返回建立5边多边形所需的(s,c)坐标集,可在此处找到。

Building on Phillip Burger's code, does anyone has a suggestion on how to modify his code to generate n-sided polygon coordinates? 以Phillip Burger的代码为基础,有人对如何修改其代码以生成n面多边形坐标提出建议吗?

I am working on a business case where I need n=3 and n=10 set of coordinates. 我正在研究需要n = 3和n = 10组坐标的业务案例。

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. 编辑:我想要的输出格式是一个数据列,其中包含列pathID; pathOrder; xCoordinate; yCoordinate-稍后需要在Tableau中绘制线条。

Here is the sample output for a 5-sided polygon 这是5面多边形的示例输出

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 这是我要修改的R代码片段

 # 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) 

This code will draw an n-sided polygon with radius 1. 此代码将绘制一个半径为1的n边多边形。

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 . 如果您不希望这样做,则将0:n更改为1:n

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM