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); 
        }
    }
}

The result is a circle in the center of the Form and inside an X but the X is getting out of the circle bounds and i want the X inside the topLeft topRight bottomLeft bottomRight to get exactly to the circle bounds. How can i repair it ?

Before it was e.Graphics.DrawEllipse it was e.Graphics.DrawRectangle and it was ok but once i changed it to DrawEllipse the X inside is getting out of the circle bounds borders.

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 回答