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);
}
}
}
结果是在Form的中心和X内部的圆圈,但是X离开了圆圈边界,我想要在topLeft topRight bottomLeft bottomRight内部的X得到精确到圆的边界。我怎么修理它?
在它之前是e.Graphics.DrawEllipse它是e.Graphics.DrawRectangle并且没关系但是一旦我将它改为DrawEllipse,X里面就越过了圆边界。
答案 0 :(得分: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
};
答案 1 :(得分:-2)
如果我们减少圆直径以激发像circleDiameter = 68
这样的值,它将正常工作并进入圆圈
int circleDiameter;
public form()
{
InitializeComponent();
circleDiameter = 68;
}