天天看點

擷取圓周上等分點的坐标(C#實作)

有個繪圖的需求,在一個圓上的等分點處畫出圖形,核心是求出圓周上的等分點的坐标,數學忘得差不多了,折騰了半天沒研究出來。

網上搜尋之後,一下子回憶起來了,原來是用Sin和Cos來計算,其實挺簡單,代碼如下:

panelV.Paint += new PaintEventHandler(panel1_Paint);

//...

private void panel1_Paint(object sender, PaintEventArgs e)
{
    int count = 8; //8個等分點
    var radians = (Math.PI / 180) * Math.Round(360.0 / count); //弧度

    double ox = 300.0;
    double oy = 200.0;

    int r = 150;

    for (int i = 0; i < count; i++)
    {
        double x = ox + r * Math.Sin(radians * i);
        double y = oy + r * Math.Cos(radians * i);

        Pen blackPen = new Pen(Color.Black, 3);
        Rectangle rect = new Rectangle(Convert.ToInt32(x), Convert.ToInt32(y), 20, 20);
        e.Graphics.DrawRectangle(blackPen, rect);
     }
 
}
           

效果:

擷取圓周上等分點的坐标(C#實作)

參考:https://www.cnblogs.com/xuhanwen/p/3780294.html