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

后端 未结 2 1574
温柔的废话
温柔的废话 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: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);
    }
    

提交回复
热议问题