简体   繁体   中英

Draw Radians/Degrees in Circle in R

I am having difficulty of producing X,Y coordinates of a circle and then drawing line segments to it. Basically what I want to do is draw 360 lines from the center of a circle to the outside of the circle in perfect spacing. This is how I am currently doing it but it is not working. If there is a different way to do this, that works great as well! Also I am hoping that degree 0 starts at the left side of the circle.

t <- seq(0,2*pi,length=360) 
coords <- t(rbind( sin(t)*127.28125, cos(t)*127.28125)) 
plot(coords,type='n',xlim=c(-63.625625,63.625625),ylim=c(0,127.28125)) 
lines(coords)

deg=data.frame(coords[,1],coords[,2])
head(deg)
deg$count=1
deg$degree=1

for(i in 1:nrow(coords)){
if(deg$count[i]<=270){
deg$degree[i]=i-1+90-45
} else {
deg$degree[i]=i-1-270-45
}
}

names(deg)[1] <- "X"
names(deg)[2] <- "Y"

i=1
for(i in 1:19){
segments(0,0,deg$X[deg$degree==((5*(i-1)))],deg$Y[deg$degree==((5*(i-1)))])
cat(((5*(i-1))),'\t')
}

Update:

I am having some issues with where the lines get drawn. Basically as we go around the circle the errors get larger so when pi/2 radians happens and it is straight up, the value is slightly to the right of x=0. This may not be possible to get but thought I would ask to see if there was anyway to fix that! The 45 90 and 135 should all match on the lines.

图形

How about this

th <- seq(0, 2*pi, length.out=360)
r <- 2
x <- r*cos(th)
y <- r*sin(th)

plot(x,y, type="n")
segments(0,0,x,y)

Basically i choose th and r in polar space and convert to Cartesian.

在此处输入图片说明

If you want to start with 0 on the left, use

    x <- -r*cos(th)

instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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