Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing circle in R

Tags:

plot

r

I don't know why the following code doesn't give me the complete circle and gives only parts of it. Also I don't know how I can show my points on the circle or outside of it within a square both centered at (0,0) with r=1 and a=2.

library("plotrix")
n<-1000
plot.new()
frame()
x<-runif(n,-1,1)
y<-runif(n,-1,1)
for (i in 1:n) { plot(x[i],y[i])}
draw.circle(0,0,1,nv=1000,border=NULL,col=NA,lty=1,lwd=1)

Here's the outputenter image description here

So I fixed it to the following and when I have 100 points the graph looks the following. Why the complete circle isn't shown?

plot(x,y)
draw.circle(0,0,1,nv=1000,border=NULL,col=NA,lty=1,lwd=1)

enter image description here

So thanks to Fernando I fixed the plot and now it looks like this but I want it to have range from (-1 to 1) for x like it is for y. xlim didn't work. Do you know what's wrong?

magnitude = function(x, y) {
  stopifnot(isTRUE(all.equal(length(x),length(y))))
  return (sqrt(x^2 + y^2))
}
library("plotrix")
monte.carlo.pi<-function(n,draw=FALSE)
{
  circle.points<-0
  square.points<-0
  x<-runif(n,-1,1)
  y<-runif(n,-1,1)
  for (i in 1:n)
  {
    #if ((x[i])^2 + (y[i])^2 <=1)
    if (magnitude(x[i],y[i])<=1)
    {
      circle.points<-circle.points+1
      square.points<-square.points+1
    } else
    {
      square.points<-square.points+1
    }
  }
  if (draw==TRUE)
  {
    plot.new()
    frame()
    plot(x,y,asp=1,xlim=c(-1,1),ylim=c(-1,1))
    draw.circle(0,0,1,nv=1000,border=NULL,col=NA,lty=1,lwd=1)
    rect(-1,-1,1,1)
    return(4*circle.points / square.points)
  }
}

and call the function like the following:

monte.carlo.pi(100,T)

current plot is like following: enter image description here

like image 386
Mona Jalal Avatar asked Mar 08 '14 05:03

Mona Jalal


People also ask

How do you draw a circle in a function?

Syntax : circle(x, y, radius); where, (x, y) is center of the circle. 'radius' is the Radius of the circle.

How do you draw a circle in Open GL?

Approach: The idea is to use the below inbuilt function to draw the circle using single click in OpenGL: glMatrixMode(GL_PROJECTION): This function sets the current matrix to projection. glLoadIdentity(): The function is used to multiply the current matrix by identity matrix.


2 Answers

Fernando's answer is good if you want the circle to actually look like a circle to the user. This answer covers drawing a circle in data dimensions.

If your x and y axes are scaled the same, e.g., if you set your aspect ratio to 1 (asp = 1), then the two methods are equivalent.

# initialize a plot
plot(c(-1, 1), c(-1, 1), type = "n")

# prepare "circle data"
radius = 1
center_x = 0
center_y = 0
theta = seq(0, 2 * pi, length = 200) # angles for drawing points around the circle

# draw the circle
lines(x = radius * cos(theta) + center_x, y = radius * sin(theta) + center_y)
like image 196
Gregor Thomas Avatar answered Nov 07 '22 07:11

Gregor Thomas


You need to specify asp = 1:

x = runif(100, -1, 1)
y = runif(100, -1, 1)
plot(x, y, asp = 1, xlim = c(-1, 1))
draw.circle(0, 0, 1, nv = 1000, border = NULL, col = NA, lty = 1, lwd = 1)

enter image description here

EDIT: Just a side note, you can make your Monte Carlo function more efficient:

mc.pi = function(n) {

  x = runif(n, -1, 1)
  y = runif(n, -1, 1)
  pin = sum(ifelse(sqrt(x^2 + y^2 <= 1), 1, 0))
  4 * pin/n
}
like image 34
Fernando Avatar answered Nov 07 '22 09:11

Fernando