1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        int circleDiameter;

        public Form1()
        {
            InitializeComponent();

            circleDiameter = 100;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Point CenterPoint = new Point()
            {
                X = this.ClientRectangle.Width / 2,
                Y = this.ClientRectangle.Height / 2
            };
            Point topLeft = new Point()
            {
                X = (this.ClientRectangle.Width - circleDiameter) / 2,
                Y = (this.ClientRectangle.Height - circleDiameter) / 2
            };
            Point topRight = new Point()
            {
                X = (this.ClientRectangle.Width + circleDiameter) / 2,
                Y = (this.ClientRectangle.Height - circleDiameter) / 2
            };
            Point bottomLeft = new Point()
            {
                X = (this.ClientRectangle.Width - circleDiameter) / 2,
                Y = (this.ClientRectangle.Height + circleDiameter) / 2
            };
            Point bottomRight = new Point()
            {
                X = (this.ClientRectangle.Width + circleDiameter) / 2,
                Y = (this.ClientRectangle.Height + circleDiameter) / 2
            };

            e.Graphics.DrawEllipse(Pens.Red,topLeft.X, topLeft.Y, circleDiameter, circleDiameter);
            e.Graphics.DrawLine(Pens.Red, CenterPoint, topLeft);
            e.Graphics.DrawLine(Pens.Red, CenterPoint, topRight);
            e.Graphics.DrawLine(Pens.Red, CenterPoint, bottomLeft);
            e.Graphics.DrawLine(Pens.Red, CenterPoint, bottomRight); 
        }
    }
}

結果は、フォームの中央とXの内側に円がありますが、Xは円の境界から外れているので、topLeft topRightbottomLeftbottomRightの内側のXが正確に円の境界に到達するようにします。どうすれば修理できますか?

e.Graphics.DrawEllipseになる前は、e.Graphics.DrawRectangleで問題ありませんでしたが、DrawEllipseに変更すると、内側のXが円の境界線から外れます。

4

2 に答える 2

1

私がこれをしてからしばらく経ちました。CosとSinを使用して、次のような円上の点を計算できます。

    private Point GetPointOnCircle(Point centre, double angle, double diameter)
    {
        return new Point((int)(Math.Cos(angle) * diameter) + centre.X, (int)(Math.Sin(angle) * diameter) + centre.Y);
    }

cosがxかyかは思い出せませんが、円の0度の点と見なすものを変更するだけで問題ありません。とにかく、あなたの角度は常に45度であり、cos(45)とsin(45)は同じなので、距離に0.70710678118654752440084436210485を掛けるか、誰かが1 / sqrt(2)と言ったように必要です。

例えば

        Point topRight = new Point()
        {
            X = (this.ClientRectangle.Width + (int)(circleDiameter * 0.70710678118654752440084436210485)) / 2,
            Y = (this.ClientRectangle.Height - (int)(circleDiameter * 0.70710678118654752440084436210485)) / 2
        };
于 2012-09-11T03:07:39.417 に答える
-2

円の直径をこのようにexcat値に減らすと、正常にcircleDiameter = 68機能し、円になります。

int circleDiameter;

public form()
{
  InitializeComponent();

  circleDiameter = 68;
}
于 2016-10-14T10:58:41.173 に答える