Calculate continous points on a circle circumference

111 Views Asked by At

Known details

Circle radius r

Origin (x,y)

starting point in circumference say (u,v)

I want to know the next point to be drawn on the circle circumference

image

I do need to draw this point by point forming arc of this circle .

Help me out of this. I have checked some post in here itself but nothing seems working for me

2

There are 2 best solutions below

4
On

There is undoubtedly a good computationally efficient answer. This is not it. Let us suppose you are going counterclockwise. Choose a quite small angle $\theta$, maybe $\frac{1}{10}$ of a degree. You will have to experiment to see what gives smooth motion.

In principle the next point is $$(u_1,v_1)=(u\cos\theta-v\sin\theta, u\sin\theta+v\cos\theta).$$ Note that $\cos\theta$ and $\sin\theta$ can be precomputed, so the calculation is cheap. What has been done here is multiplication by a rotation matrix.

The problem is that tiny errors will accumulate. So every so often, or perhaps every time, force the point to be on the circle by computing $u_1$ as above, and letting $v_1$ be $\pm\sqrt{r^2-u_1^2}$. You will have to pick the appropriate sign, which most of the time will be the sign of $v$, but some code will have to be written for sign transitions.

But undoubtedly the problem has been solved in a more efficient way many times.

1
On

This is one possible way of coding it. I will use the form [returns] = functionname(arguments) so that you understand what is returned and what are the arguments.

[xn, qn] = nextpoint(xc, qc, xo, r)
// xc = current point in format (x,y)
// qc = current quadrant, can take values 1,2,3, or 4
// xo = origin coordinates in format (x,y)
// r  = radius (since there aren't any specifics, I am assuming it can be non-integer)
// xn = next point
// qn = next quadrant
  if (qc == 1)  // what points might be next depending on the quadrant of the current point
    g1 = (-1, 0)
    g2 = ( 0, 1)
  else if (qc == 2)
    g1 = (-1, 0)
    g2 = ( 0,-1)
  else if (qc == 3)
    g1 = ( 1, 0)
    g2 = ( 0,-1)
  else if (qc == 4)
    g1 = ( 1, 0)
    g2 = ( 0, 1)
  end
  g3 = g1 + g2
  g1 = xc + g1
  g2 = xc + g2
  g3 = xc + g3
  xn = from {g1,g2,g3} the one with smallest residual(gi,xo,r)
  if (xn.x == xo.x OR xn.y == xo.y)
    qn = qc + 1
  else
    qn = qc
  end

The idea here is to avoid expensive trigonometric functions by comparing values. The performance advantage will depend on the language and compiler you use.

For residual, you can take a measure of how close the lattice point is to the actual circumference $$r_i=|(g_i-x_o)\cdot(g_i-x_o) - r^2|$$

And you can use the previous function like

[listofpoints] = getcircumference(xo,r)
  xco = xo + (round(r),0)
  qco = 1
  [xc, qc] = nexpoint(xco, qco, xo, r)
  listofpoints = {xc}
  while (xc different from xco)
    [xc, qc] = nextpoint(xc, qc, xo, r)
    append xc to listofpoints
  end

This worked for me in Mathematica and Matlab