android Dividing circle into N equal parts and know the coordinates of each dividing point

后端 未结 2 1573
温柔的废话
温柔的废话 2020-12-31 02:56

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 <

相关标签:
2条回答
  • 2020-12-31 03:12

    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)

    0 讨论(0)
  • 2020-12-31 03:19

    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);
    }
    
    0 讨论(0)
提交回复
热议问题