我正在绘制一个矩形,并在实时绘制矩形的同时,还填充每个矩形边缘,左边,右边,上边和下边,每边都有绿色的点。 现在我将它设置为36个点每个矩形边缘:如何将点坐标添加到矩形的一侧的PointF列表中?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace mws
{
public partial class PaddingPoints : Form
{
private List<PointF> points = new List<PointF>();
private Point RectStartPoint;
private Image img;
private Image imgClone;
private Pen myPen;
private int n = 36; //number of points
private Bitmap _bmpBU = null;
public PaddingPoints()
{
InitializeComponent();
this.DoubleBuffered = true;
_bmpBU = new Bitmap(@"D:\MyWeatherStation-Images-And-Icons\radar090.PNG");
myPen = new Pen(Brushes.Red, 2);
//Bitmap to hold the picturebox image
img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g;
using (g = Graphics.FromImage(img))
{
g.DrawImage(_bmpBU, 0, 0, pictureBox1.Width, pictureBox1.Height);
}
//image to hold the original picturebox. We need it to clear img to the original
//picturebox image
imgClone = (Bitmap)img.Clone();
//We draw always on img and then we invalidate
pictureBox1.Image = img;
}
private void PaddingPoints_Load(object sender, EventArgs e)
{
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
RectStartPoint = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Location != RectStartPoint)
{
DrawRectangle(e.Location);
}
}
private void DrawRectangle(Point pnt)
{
Graphics g = Graphics.FromImage(img);
int width, height, i, x, y;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//Clear img from the rectangle we drawn previously
g.DrawImage(imgClone, 0, 0);
if (pnt.X == RectStartPoint.X || pnt.Y == RectStartPoint.Y)
{
g.DrawLine(myPen, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y);
}
else
{
g.DrawRectangle(myPen, Math.Min(RectStartPoint.X, pnt.X), Math.Min(RectStartPoint.Y, pnt.Y),
Math.Abs(RectStartPoint.X - pnt.X), Math.Abs(RectStartPoint.Y - pnt.Y));
//width of spaces between points
width = (int)((Math.Abs(RectStartPoint.X - pnt.X))/(n - 1));
//height of spaces between points
height = (int)((Math.Abs(RectStartPoint.Y - pnt.Y))/(n - 1));
//we always want the upper left x, y coordinates as a reference drawing clockwise
x = Math.Min(RectStartPoint.X, pnt.X);
y = Math.Min(RectStartPoint.Y, pnt.Y);
//Drawing the points. change the 3, 6 values for larger ones
for (i = 0; i < n - 1; i++)
{
//Up side
g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6));
//Right side
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3, y - 3, 6, 6));
//Bottom side
g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
//Left side
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3, 6, 6));
PointF pointf = new PointF(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3);
points.Add(pointf);
x += width;
y += height;
}
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3,
Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6));
g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3,
Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6));
}
g.Dispose();
//draw img to picturebox
pictureBox1.Invalidate();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
int t = points.Count;
}
}
}
结果是:
现在我想做好两件事在pictureBox1鼠标向上事件:
具有其中一个矩形边的坐标的PointF列表。 例如,如果它是左边缘,则List应该包含每个点36个坐标X,Y。 如果它是顶边,那么对于底边和右边也是36点并且是相同的。 但是每个时间点只能在列表中获得一个边。
而且还有一个变量,将保存距离另一个边缘的距离: 例如,如果我添加到List点左边缘的36个点我想获得int变量的距离在左边缘和八边之间。 如果我在列表中添加了右边缘的36个点,那么int变量应该保持从右边到左边的距离。上下边缘相同。
取决于我从哪里得到点。
对于点i的添加到顶部的列表:
private List<PointF> points = new List<PointF>();
然后在DrawRectangle的方法I中加入:
PointF pointf = new PointF(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3);
points.Add(pointf);
然后在鼠标向上事件时我使用断点在那里,我看到列表点有更多的4000点坐标,其中许多都是相同的。
我只需要得到例如矩形绿色点坐标的左边缘,所以点应该只在最后的36个坐标以及从一个边到另一个边的距离。