Pentagram: Difference between revisions

No edit summary
Line 812:
lines(p)</lang>
 
==={{header|Using circle equation}}===
A better more accurate approach utilising equation of a circle using polar coordinates.[http://www.math.com/tables/geometry/circles.htm]
5 points are required to draw a pentagram. a circle with centre coordinates x=10 and y=10 with radius 10 was chosen for this example.
In order to find 5 equal points circle needs to be divided by 5 i.e 360/5 = 72
each point on the circumference is 72 degrees apart,
5 points on the circles circumference are calculated and than plotted and line drawn in-between to produce pentagram
<lang R>#Circle equation
#x = r*cos(angle) + centre_x
#y = r*sin(angle) + centre_y
 
#centre points
centre_x = 10
centre_y = 10
#radius
r = 10
 
deg2rad <- function(d){
return((d*pi)/180)
} #Converts Degrees to radians
X_coord <- function(r=10,centre_x=10,angle) #Finds Xcoordinate on the circumference
{
return(r*cos(deg2rad(angle)) + centre_x)
}
Y_coord <- function(r=10,centre_y=10,angle) #Finds Ycoordinate on the circumference
{
return(r*sin(deg2rad(angle)) + centre_x)
}
 
# series of angles after dividing the circle in to 5
angles <- list()
for(i in 1:5)
{
angles[i] <- 72*i
}
angles <- unlist(angles) #flattening the list
 
for(i in seq_along(angles)){
print(i)
print(angles[i])
if(i == 1)
{
coordinates <-
cbind(c(
x = X_coord(angle = angles[i]),
y = Y_coord(angle = angles[i]))
)
}
else{
coordinates <- cbind(coordinates,cbind(c(
x = X_coord(angle = angles[i]),
y = Y_coord(angle = angles[i]))))
}
}
plot(xlim = c(0,30), ylim = c(0,30),x = coordinates[1,], y=coordinates[2,])
 
polygon(x = coordinates[1,c(1,3,5,2,4,1)],
y=coordinates[2,c(1,3,5,2,4,1)],
col = "#1b98e0",
border = "red",
lwd = 5)</lang>
 
=={{header|Racket}}==