英文:
Create sequence of degrees and radii that may contain 0 (degrees)
问题
我需要创建两个已知距离和角度的点之间的路径,然后使用 R 将结果转换为笛卡尔坐标。这两个点的原点坐标都是 (0,0)。半径的长度始终不同,方向始终为逆时针。
我无法弄清如何处理度数序列,因为范围有时可能包括 0 度。起始和结束的度数会变化,所以我需要一种方法,可以处理任何范围小于等于 180 度的情况。换句话说,起始和结束角度之间的差值永远不会超过 180 度。
问题在下面的代码中得到了说明:
rad <- seq(943.0975, 939.5975, length.out = 1000)
deg <- seq(67.8352, 247.8352, length.out = 1000)
rad
向量的工作是正确的,但 deg
向量是不正确的,因为它最终会按顺时针方向前进。使用降序的 seq()
意味着它不会穿越 0 度。
在这种情况下,如何处理包含 0 度的度数范围?
英文:
I am needing to create a path between two points for which distance and degrees are known, and then convert the result to Cartesian coordinates using R. The origin for both points is 0,0. The radii always vary in length. The direction is always anti-clockwise.
I have been unable to work out how to do the degrees sequence as the range sometimes may include 0°. The start and end degrees will vary so I need method that can handle any range <= 180°. In other words, the difference between the start and end angle will never exceed 180°.
The issue is illustrated in the code below:
rad <- seq(943.0975, 939.5975, length.out = 1000)
deg <- seq(67.8352, 247.8352, length.out = 1000)
The rad vector works correctly but the deg vector is incorrect as it ends up following a clockwise direction. Doing a descending seq()
means it doesn't cross 0°.
How is a range of degree values that contains 0° handled in this instance?
答案1
得分: 0
以下是您要翻译的内容:
"Unable to find a concise method, this is how I handled the issue. It involves a process that generates two vectors for degrees - one for everything to the "left" of 0° and one for everything to the "right" of 0°. It is "messy" around the 0° in that the difference between the two values directly to the left and right of 0° may not be == turni
, but given the size of endturnp
the issue is not perceptible. This code only works if degdiff
<= 180°.
Declared variables
degA <- 67.8352 # 起始点的度数
radA <- 943.0975 # 起始点的半径
degB <- 247.8352 # 终点的度数
radB <- 939.5975 # 终点的半径
endturnp <- 2030 # 在起始点和终点之间绘制点的数量
Derived variables
degdiff <- (outdegs - outdege + 180) %% 360 - 180 # 起始点和终点之间的度数差异
degdiff <- ifelse(degdiff < 0, degdiff * -1, degdiff) # 如果为负数,则转换为正数
turni <- degdiff / endturnp # 基于 "endturnp" 获取从 degA 到 degB 的度数增量
Generate degree vector from degA to degB
if(degA < degB) { # 如果范围包括 0 度
leftlen <- 0:floor((360 - degB) / turni) # 返回度数 > 180 且 < 360 的长度向量
leftdeg <- sort(degB + (leftlen * turni), decreasing = T) # 计算度数
rightlen <- 0:floor(degA/turni) # 返回长度向量
rightdeg <- degA - (turni * rightlen) # 计算度数
indexdeg <- append(rightdeg,leftdeg) # 合并度数向量
} else {
indexdeg <- seq(degA, degB, length.out = endturnp) # 创建索引
}
Generate radii vector from degA to degB
indexrad <- seq(radA, radB, length.out = length(indexdeg))
Combine radii and degrees vectors and calculate xy coordinates
arcpath <- data.frame(rad = indexrad,
deg = indexdeg,
x = NA,
y = NA)
arcpath$x <- arcpath$rad * sin(pi * 2 * arcpath$deg / 360)
arcpath$y <- arcpath$rad * cos(pi * 2 * arcpath$deg / 360)
And finally, plot result
ggplot() +
geom_path(data = arcpath, aes(x = x, y = y),
linewidth = .4,
lineend = "square")"
英文:
Unable to find a concise method, this is how I handled the issue. It involves a process that generates two vectors for degrees - one for everything to the "left" of 0° and one for everything to the "right" of 0°. It is "messy" around the 0° in that the difference between the two values directly to the left and right of 0° may not be == turni
, but given the size of endturnp
the issue is not perceptible. This code only works if degdiff
<= 180°.
Declared variables
degA <- 67.8352 # Start point degrees
radA <- 943.0975 # Start point radius
degB <- 247.8352 # End point degrees
radB <- 939.5975 # End point radius
endturnp <- 2030 # Number of points to plot between start and end point
Derived variables
degdiff <- (outdegs - outdege + 180) %% 360 - 180 # Degrees difference between start and end
degdiff <- ifelse(degdiff < 0, degdiff * -1, degdiff) # Convert to positive number if negative
turni <- degdiff / endturnp # Get degrees increment between degA and degB based on "endturnp"
Generate degree vector from degA to degB
if(degA < degB) { # If range includes 0 degrees
leftlen <- 0:floor((360 - degB) / turni) # Return length vector for degrees > 180 & < 360
leftdeg <- sort(degB + (leftlen * turni), decreasing = T) # Calculate degrees
rightlen <- 0:floor(degA/turni) # Return length vector
rightdeg <- degA - (turni * rightlen) # calculate degree
indexdeg <- append(rightdeg,leftdeg) # Combine degrees vectors
} else {
indexdeg <- seq(degA, degB, length.out = endturnp) # Create index
}
Generate radii vector from degA to degB
indexrad <- seq(radA, radB, length.out = length(indexdeg))
Combine radii and degrees vectors and calculate xy coordinates
arcpath <- data.frame(rad = indexrad,
deg = indexdeg,
x = NA,
y = NA)
arcpath$x <- arcpath$rad * sin(pi * 2 * arcpath$deg / 360)
arcpath$y <- arcpath$rad * cos(pi * 2 * arcpath$deg / 360)
And finally, plot result
ggplot() +
geom_path(data = arcpath, aes(x = x, y = y),
linewidth = .4,
lineend = "square")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论