111,076
社区成员




public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<PointF> dat = new List<PointF>();
List<PointF> datA = new List<PointF>();
List<PointF> datB = new List<PointF>();
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
if (dat.Count > 2)
{
g.DrawPolygon(Pens.Black, dat.ToArray());
if (datA.Count > 2) g.DrawPolygon(Pens.Red, datA.ToArray());
if (datB.Count > 2) g.DrawPolygon(Pens.Green, datB.ToArray());
}
else
{
foreach (var p in dat)
{
g.DrawArc(Pens.Black, new RectangleF(p.X - 2, p.Y - 2, 5, 5), 0, 360);
}
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
dat.Add(new Point(e.X, e.Y));
pictureBox1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
var A = int.Parse("0" + textBox1.Text);
var B = int.Parse("0" + textBox2.Text);
if (A > 0) datA = Polygon(dat, A);
if (B > 0) datB = Polygon(dat, -B);
pictureBox1.Invalidate();
}
List<PointF> Polygon(List<PointF> dat, double h)
{
var t = new List<Line>();
for (var i = 0; i < dat.Count - 1; i++)
{
if (i == 0) t.Add(new Line(dat[dat.Count - 1], dat[i]).ParallelLines(h));
t.Add(new Line(dat[i], dat[i + 1]).ParallelLines(h));
}
var res = new List<PointF>();
for (var i = 0; i < t.Count - 1; i++)
{
if (i == 0) res.Add(t[t.Count - 1].Corss(t[i]));
res.Add(t[i].Corss(t[i + 1]));
}
return res;
}
private void button2_Click(object sender, EventArgs e)
{
dat.Clear();
datA.Clear();
datB.Clear();
pictureBox1.Invalidate();
}
}
public class Line
{
public PointF Start;
public PointF End;
public PointF Increment;
public float Slope;
public Line(PointF s, PointF e)
{
Start = s;
End = e;
Increment = new PointF(End.X - Start.X, End.Y - Start.Y);
Slope = Increment.Y / Increment.X;
}
public Line ParallelLines(double h)
{
var r = (float)(h / Math.Sin(Math.Atan2(Increment.Y, Increment.X)));
return new Line(
new PointF(Start.X + r - Increment.X, Start.Y - Increment.Y),
new PointF(End.X + r + Increment.X, End.Y + Increment.Y));
}
public PointF Corss(Line t)
{
if (Slope == t.Slope) return PointF.Empty;
var x = (Slope * Start.X - t.Slope * t.Start.X - Start.Y + t.Start.Y) / (Slope - t.Slope);
var y = Slope * x - Slope * Start.X + Start.Y;
return new PointF(x, y);
}
}
private Point startPoint;
private Graphics g;
Queue<Point> PointArray=new Queue<Point>();
写了三个事件
MouseDown
private void Form1_MouseDown(object sender,MouseEventArgs e)
{
startPoint=new Point(e.X,e.Y);
if(e.Button==MouseButtons.Left)
{
PointArray.Enqueue(new Point(e.X,e.Y));
}
}
MouseMove
private void Form1_MouseMove(object sender,MouseEventArgs e)
{
this.Text=string.Format("鼠标的位置({0},{1})",e.X,e.Y);
}
MouseUp
private void Form1_MouseUp(object sender,MouseEventArgs e)
{
g=this.CreateGraphics();
Pen p=new Pen(Color.Black,2);
g.DrawLine(p,startPoint.X,startPoint.Y,e.X,e.Y);
}
效果:就只是画图,不能实现我要的效果