[Unity] Common geometric knowledge: get the coordinates of a point on a circle according to the angle


Get the coordinates of a point on a circle according to the angle

The formula for calculating the coordinates of a point on a circle:

  x = c e n t e r X + r a d i u s ∗ c o s ( a n g l e ∗ P I / 180 ) \ x = centerX + radius * cos(angle * PI / 180)  x=centerX+radiuscos(anglePI/180)
  y = c e n t e r Y + r a d i u s ∗ s i n ( a n g l e ∗ P I / 180 ) \ y = centerY + radius * sin(angle * PI / 180)  and=centerY+radiussin(anglePI/180)

centerX centerY The center point of the circle
radius: radius
angle: rotation angle (starting direction is 3 o'clock direction)

Example: Dynamically change button position

In this case, the position of the button is designed to rotate around the generator. When the number of buttons is 3, put a button every 120°; when the number of buttons is 4, put a button every 90°.

The code shows:

		// 根据防御塔数量决定按钮的旋转角度
        for (int i = 0; i < _createBtnList.Count; i++)
        {
    
    
            // 计算旋转角度
            float angle = 360 / _createBtnList.Count * i + 90;
            // 使用公式算出按钮坐标
            //x = centerX + radius * cos(angle * 3.14 / 180)
            //y = centerY + radius * sin(angle * 3.14 / 180)
            _createBtnList[i].position = new Vector3(100 * Mathf.Cos(angle * Mathf.PI / 180), 100 * Mathf.Sin(angle * Mathf.PI / 180), 0);
        }

Calculate the rotation angle of each button by traversing the button list List _createBtnList, and then calculate the coordinates of the button according to the angle. Finally, add 90° to this angle to change the starting coordinates from 3 o'clock to 12 o'clock.

final effect

insert image description here


For more information, please check the general catalog [Unity] Unity study notes catalog arrangement

Guess you like

Origin blog.csdn.net/xiaoyaoACi/article/details/127550804