hisence-yolo-detection/XKRS.UI/Helper/PointHelper.cs
2024-06-17 11:02:28 +08:00

57 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XKRS.UI.Helper
{
internal static class PointHelper
{
public static Point ToPoint(this PointF pf)
{
return new Point((int)pf.X, (int)pf.Y);
}
/// <summary>
/// 将相对于控件的坐标转换为相对于图像的坐标
/// </summary>
/// <param name="p">控件中指定点的点位坐标,坐标原点为控件左上角</param>
/// <returns>该点以图像坐标系为基准的坐标值,坐标原点为图像左上角</returns>
public static PointF ToImageCoordinate(this Point p, Matrix m)
{
PointF pf = new PointF(p.X, p.Y);
return ToImageCoordinate(pf, m);
}
/// <summary>
/// 将相对于控件的坐标转换为相对于图像的坐标
/// </summary>
/// <param name="p">控件中指定点的点位坐标,坐标原点为控件左上角</param>
/// <returns>该点以图像坐标系为基准的坐标值,坐标原点为图像左上角</returns>
public static PointF ToImageCoordinate(this PointF p, Matrix m)
{
PointF[] ps = new PointF[] { p };
Matrix invertMatrix = m.Clone();
//想要从旧空间到新空间的逆变换,所以我们需要对这个矩阵求逆
invertMatrix.Invert();
invertMatrix.TransformPoints(ps);
return ps[0];
}
/// <summary>
/// 将相对于图像的坐标转换为相对于控件坐标系
/// </summary>
/// <param name="p">图像中指定点的点位坐标,坐标原点为图像左上角</param>
/// <returns>该点以空间坐标系为基准的坐标值,坐标原点为空间坐左上角</returns>
public static PointF ToControlCoordinate(this PointF p, Matrix m)
{
PointF[] ps = new PointF[] { p };
m.TransformPoints(ps);
return ps[0];
}
}
}