19

I have requirement that a circle should be divided into N equal parts based on number(2,3...n. But I want the coordinates of dividing points.

I have a circle whose centre(x,y) and radius(150) are known.

Question:

Is there any formula which gives me the coordinates of dividing points as shown in figure. Can anyone please tell me the formula. I want to implement it in Java.

Circle image for refrence:

image

Cœur
  • 37,241
  • 25
  • 195
  • 267
TheFlash
  • 5,997
  • 4
  • 41
  • 46

2 Answers2

24

I have already accepted answer... the formula works perfectly. Here is the solution coded in Java. It will help other developers.

    private int x[];  // Class variable
    private int y[];  // Class variable

    private void getPoints(int x0,int y0,int r,int noOfDividingPoints)
    {

        double angle = 0;

        x = new int[noOfDividingPoints];
        y = new int[noOfDividingPoints];

        for(int i = 0 ; i < noOfDividingPoints  ;i++)
        {
            angle = i * (360/noOfDividingPoints);

            x[i] = (int) (x0 + r * Math.cos(Math.toRadians(angle)));
            y[i] = (int) (y0 + r * Math.sin(Math.toRadians(angle)));

        }

        for(int i = 0 ; i < noOfDividingPoints  ;i++)
        {
            Log.v("x",""+i+": "+x[i]);
            Log.v("y",""+i+": "+y[i]);

        }
    }

Where x0 and y0 are co ordinates of circle's centre.and r is radius.

In my case:

Input x0 = 0 , y0 = 0 and r = 150 , noOfDividingPoints = 5

output

point1: (150,0)

point2: (46,142)

point3: (-121,88)

point4: (-121,-88)

point5: (46,-142)

User
  • 31,811
  • 40
  • 131
  • 232
TheFlash
  • 5,997
  • 4
  • 41
  • 46
  • the function does not provide accurate results always, like i want a circle to radius 100 with 5 noOfDividingPoints, when you are moving object on touch – Androider Jan 30 '14 at 11:31
  • @Syed Zahid Ali if you change the radius then points will also change...I have tested the formula for different points and it is working perfect. – TheFlash Jan 31 '14 at 06:50
  • I was having wrong results when I was selecting odd noOfDividingPoints – Androider Jan 31 '14 at 16:06
21

You need to convert between polar and Cartesian coordinates. The angle you need is the angle between the (imaginary) vertical line that splits the circle in half and the line that connects the center with the circle's boundary. With this formula you can calculate the X and Y offsets from the center.

In your example image the first angle is 0, and the second one is 360/n. Each next is i*(360/n) where i is the index of the current line you need to draw. Applying this will give you the X and Y offsets in a clockwise order (and you can just add them to the X and Y coordinates of the center to find the coordinates of each point)

EDIT: some kind of pseudo-code:

//x0, y0 - center's coordinates
for(i = 1 to n)
{
    angle = i * (360/n);
    point.x = x0 + r * cos(angle);
    point.y = y0 + r * sin(angle);
}
stan0
  • 11,549
  • 6
  • 42
  • 59
  • thanks for ur answer..+1 form me...if u give me formula which returns the array with co ordinates..it would be appreciated...i will sure accept ur answer.I want to pass number which defines no of equal parts and return array with co ordinates..! – TheFlash Sep 04 '13 at 10:15
  • 1
    @Indiandroid You should be able to apply this to fit your needs without him having to adapt it for you. If you can't do this on your own, you shouldn't be programming. – Neil Sep 04 '13 at 11:06
  • @stan0 sry for inconvenience.i haven’t time so i told u to make formula for me in java.. but i will sure try ur suggestion...and let u know. – TheFlash Sep 04 '13 at 11:21
  • @stan0 ur formula is perfect...soon i will accept ur answer..just i need to convert it into JAVA code and pass correct values....thanks. – TheFlash Sep 05 '13 at 13:19