大佬,可以提供一下用C#随机画点然后连成线的代码嘛
发布网友
发布时间:2023-08-03 14:45
我来回答
共2个回答
热心网友
时间:2024-11-15 20:35
这个很简单啊,看软糖帮你做一个!
鼠标画点连曲线,随机变换颜色。点击标签,也能随机画点。
代码
namespace 随机画点连成线 {
public partial class Form1 : Form {
Random rnd = new Random();
List<Point> 点;
Pen 画笔 = new Pen(Color.DarkBlue, 2);
Pen 圆画笔 = new Pen(Color.Black, 2);
Graphics 画板;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
画板 = this.CreateGraphics();
点 = new List<Point>();
}
private void Form1_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
点.Add(e.Location);
绘制圆(画板, 圆画笔, e.Location, 5.0f);
} else if (e.Button == MouseButtons.Right) {
绘制点数组(画板, 画笔, 点.ToArray());
点 = new List<Point>();
画笔 = new Pen(Color.FromArgb(255,rnd.Next(255), rnd.Next(255), rnd.Next(255)), 2);
} else if(e.Button == MouseButtons.Middle) {
画板.Clear(BackColor);
}
}
private void 绘制圆(Graphics g, Pen pen, PointF 中心点, float 宽度) {
g.DrawEllipse(pen, 中心点.X - 宽度/2, 中心点.Y - 宽度/2, 宽度,宽度);
}
private void 绘制点数组(Graphics g, Pen pen, Point[] 点数组) {
g.DrawCurve(pen, 点数组);
}
private void label1_Click(object sender, EventArgs e) {
//随机生成点并连成线
点 = new List<Point>();
for (int i = 1; i <= rnd.Next(7) + 3; i++) {
var rect = this.ClientRectangle;
int x = rnd.Next(rect.Width);
int y = rnd.Next(rect.Height);
Point P = new Point(x, y);
点.Add(P);
绘制圆(画板, 圆画笔, P, 5.0f);
}
绘制点数组(画板, 画笔, 点.ToArray());
画笔 = new Pen(Color.FromArgb(255, rnd.Next(255), rnd.Next(255), rnd.Next(255)), 2);
}
}
}
满意请及时采纳,谢谢!
热心网友
时间:2024-11-15 20:35
龙肉魁星潞令山神卷四凤阳士人耿十八珠儿小官人胡四姐祝翁侠女酒友