c#怎么根据轴径画圆
时间: 2023-10-28 13:53:42 浏览: 46
在 C# 中,你可以使用 `Graphics` 类的 `DrawEllipse` 方法来根据轴径绘制圆。下面是一个示例代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
public class CircleForm : Form
{
public CircleForm()
{
this.Paint += new PaintEventHandler(DrawCircle);
}
private void DrawCircle(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 2);
int centerX = this.Width / 2;
int centerY = this.Height / 2;
int radiusX = 100; // 横向轴径
int radiusY = 100; // 纵向轴径
int x = centerX - radiusX;
int y = centerY - radiusY;
int width = radiusX * 2;
int height = radiusY * 2;
g.DrawEllipse(pen, x, y, width, height);
}
public static void Main()
{
Application.Run(new CircleForm());
}
}
```
这段代码创建了一个继承自 `Form` 的自定义窗体 `CircleForm`,并在窗体上绘制了一个椭圆(即轴径不相等的圆)。你可以根据需要修改 `radiusX` 和 `radiusY` 变量的值来调整椭圆的轴径。运行程序后,将显示一个带有绘制的圆的窗体。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)