Reputation: 2593
I want to create a geometrical shape with connecting points together .
Upvotes: 0
Views: 280
Reputation: 238
For a reason I've still not caught yet, the result goes better with n...
star <- function(n){
x11(width=5, height=5)
v.x <- seq(0, 2*pi - pi/n , length.out=2*n)
v.x[seq(1, length.out=n, by=2)] <- cos(v.x[seq(1, length.out=n, by=2)])
v.x[seq(2, length.out=n, by=2)] <- cos(v.x[seq(2, length.out=n, by=2)])*0.5
v.y <- seq(0, 2*pi, length.out=2*n - 1)
v.y[seq(1, length.out=n, by=2)] <- sin(v.y[seq(1, length.out=n, by=2)])
v.y[seq(2, length.out=n, by=2)] <- sin(v.y[seq(2, length.out=n, by=2)])*0.5
plot(-1:1, -1:1, type="n", axes=FALSE, xlab="", ylab="")
polygon(x=v.x, y=v.y)
}
# par(mfrow=c(3,3))
# for (k in 1:9){
# star(k*5)
# }
n <- as.numeric(readline(prompt="Enter n corner > ")) ; star(n)
Save the script then source it. That is it. Yet the stars of 3 -> 10 branches will look awful. Hope it will give you some ideas
Upvotes: 1
Reputation: 19628
You can do some math calculations first and figured out the coordinates of all the vertices - 10 points for a five pointed star.
Then you can use segment
command or just plot(...,type='l') + polygon(..)
to make it happen.
Here is some Java hints showing you how to calculate the coordinates.
Here is an example showing you the idea:
plot(c(0,0.5,0,1,1,0), c(0,0.5,1,1,0,0), type="l")
polygon(c(0,0.5,0,1,1,0), c(0,0.5,1,1,0,0), col="grey")
Upvotes: 1