2D下的几何变换(C#实现,持续更新)
(1)已知2D下,新坐标系的原点、X轴方向向量、Y轴方向向量在原始坐标系下的表示,求原始坐标系中直线,在新坐标系下的直线方程;
(2)求直线与2D包围盒的交点,可能有0、1或2个交点;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
//using System.Windows.Forms;namespace DTSAlgorithm
{class CoordinateTransform{// 定义一个方法来计算变换矩阵public static double[,] CalculateTransformationMatrix(double Ox, double Oy, double ux, double uy, double vx, double vy){// 创建一个3x3的变换矩阵double[,] transformationMatrix = new double[3, 3];// 设置矩阵的元素transformationMatrix[0, 0] = ux; // 新坐标系x轴方向向量的x分量transformationMatrix[0, 1] = uy; // 新坐标系y轴方向向量的x分量transformationMatrix[0, 2] = -ux * Ox - uy * Oy; // 新坐标系原点的x坐标transformationMatrix[1, 0] = vx; // 新坐标系x轴方向向量的y分量transformationMatrix[1, 1] = vy; // 新坐标系y轴方向向量的y分量transformationMatrix[1, 2] = -vx * Ox - vy * Oy; // 新坐标系原点的y坐标transformationMatrix[2, 0] = 0; // 齐次坐标部分transformationMatrix[2, 1] = 0;transformationMatrix[2, 2] = 1;return transformationMatrix;}// 打印矩阵的方法public static void PrintMatrix(double[,] matrix){for (int i = 0; i < matrix.GetLength(0); i++){for (int j = 0; j < matrix.GetLength(1); j++){Console.Write(matrix[i, j] + "\t");}Console.WriteLine();}}}public struct BoundingBox{public double XMin { get; }public double XMax { get; }public double YMin { get; }public double YMax { get; }public BoundingBox(double xmin, double xmax, double ymin, double ymax){XMin = xmin;XMax = xmax;YMin = ymin;YMax = ymax;}public override string ToString() =>$"X:[{XMin}, {XMax}], Y:[{YMin}, {YMax}]";}public class BoundingBoxCalculator{public static BoundingBox GetTransformedBoundingBox(List<PointF> originalPoints,double[,] transformMatrix){if (originalPoints == null || originalPoints.Count == 0)throw new ArgumentException("点集不能为空");if (transformMatrix.GetLength(0) != 3 || transformMatrix.GetLength(1) != 3)throw new ArgumentException("变换矩阵必须是3x3矩阵");double xmin = double.MaxValue, xmax = double.MinValue;double ymin = double.MaxValue, ymax = double.MinValue;foreach (var point in originalPoints){// 应用变换矩阵 (齐次坐标变换)double x = transformMatrix[0, 0] * point.X +transformMatrix[0, 1] * point.Y +transformMatrix[0, 2];double y = transformMatrix[1, 0] * point.X +transformMatrix[1, 1] * point.Y +transformMatrix[1, 2];// 透视除法(如果是仿射变换,则w=1)double w = transformMatrix[2, 0] * point.X +transformMatrix[2, 1] * point.Y +transformMatrix[2, 2];if (Math.Abs(w) > 1e-6) // 避免除以零{x /= w;y /= w;}// 更新边界xmin = Math.Min(xmin, x);xmax = Math.Max(xmax, x);ymin = Math.Min(ymin, y);ymax = Math.Max(ymax, y);}return new BoundingBox(xmin, xmax, ymin, ymax);}}public class LineTransformer{/// <summary>/// 计算变换后的直线方程系数/// </summary>/// <param name="a">原始直线方程ax+by+c=0的a系数</param>/// <param name="b">原始直线方程ax+by+c=0的b系数</param>/// <param name="c">原始直线方程ax+by+c=0的c系数</param>/// <param name="transformMatrix">3x3齐次变换矩阵(仿射变换)</param>/// <returns>新坐标系下的直线系数(a', b', c')</returns>public static (double a, double b, double c) TransformLineEquation(double a, double b, double c,double[,] transformMatrix){// 验证矩阵尺寸if (transformMatrix.GetLength(0) != 3 || transformMatrix.GetLength(1) != 3)throw new ArgumentException("变换矩阵必须是3x3矩阵");// 计算变换矩阵的逆矩阵(用于直线变换)if (!InvertMatrix(transformMatrix, out double[,] invMatrix))throw new ArgumentException("变换矩阵不可逆");// 直线变换公式:新系数 = 原始系数 * 逆变换矩阵double a_new = a * invMatrix[0, 0] + b * invMatrix[1, 0];double b_new = a * invMatrix[0, 1] + b * invMatrix[1, 1];double c_new = a * invMatrix[0, 2] + b * invMatrix[1, 2] + c;return (a_new, b_new, c_new);}/// <summary>/// 3x3矩阵求逆(简化版,适用于仿射变换)/// </summary>private static bool InvertMatrix(double[,] matrix, out double[,] inverse){inverse = new double[3, 3];double det = matrix[0, 0] * (matrix[1, 1] * matrix[2, 2] - matrix[1, 2] * matrix[2, 1]) -matrix[0, 1] * (matrix[1, 0] * matrix[2, 2] - matrix[1, 2] * matrix[2, 0]) +matrix[0, 2] * (matrix[1, 0] * matrix[2, 1] - matrix[1, 1] * matrix[2, 0]);if (Math.Abs(det) < 1e-6f) return false;double invDet = 1.0f / det;inverse[0, 0] = (matrix[1, 1] * matrix[2, 2] - matrix[1, 2] * matrix[2, 1]) * invDet;inverse[0, 1] = (matrix[0, 2] * matrix[2, 1] - matrix[0, 1] * matrix[2, 2]) * invDet;inverse[0, 2] = (matrix[0, 1] * matrix[1, 2] - matrix[0, 2] * matrix[1, 1]) * invDet;inverse[1, 0] = (matrix[1, 2] * matrix[2, 0] - matrix[1, 0] * matrix[2, 2]) * invDet;inverse[1, 1] = (matrix[0, 0] * matrix[2, 2] - matrix[0, 2] * matrix[2, 0]) * invDet;inverse[1, 2] = (matrix[0, 2] * matrix[1, 0] - matrix[0, 0] * matrix[1, 2]) * invDet;inverse[2, 0] = (matrix[1, 0] * matrix[2, 1] - matrix[1, 1] * matrix[2, 0]) * invDet;inverse[2, 1] = (matrix[0, 1] * matrix[2, 0] - matrix[0, 0] * matrix[2, 1]) * invDet;inverse[2, 2] = (matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]) * invDet;return true;}}public class LineSegmentCalculator{public static Tuple<PointF, PointF> GetLineSegmentInBoundingBox(double a, double b, double c, BoundingBox box){double xmin = box.XMin;double xmax = box.XMax;double ymin = box.YMin;double ymax = box.YMax;List<PointF> intersections = new List<PointF>();// 检查直线是否与矩形的四条边相交// 1. 左边界 (x = xmin)if (b != 0) // 避免垂直于y轴的直线{double y = (-a * xmin - c) / b;if (y >= ymin && y <= ymax)intersections.Add(new PointF(xmin, y));}// 2. 右边界 (x = xmax)if (b != 0){double y = (-a * xmax - c) / b;if (y >= ymin && y <= ymax)intersections.Add(new PointF(xmax, y));}// 3. 下边界 (y = ymin)if (a != 0) // 避免垂直于x轴的直线{double x = (-b * ymin - c) / a;if (x >= xmin && x <= xmax)intersections.Add(new PointF(x, ymin));}// 4. 上边界 (y = ymax)if (a != 0){double x = (-b * ymax - c) / a;if (x >= xmin && x <= xmax)intersections.Add(new PointF(x, ymax));}// 去重并验证交点数量intersections = RemoveDuplicates(intersections);if (intersections.Count == 2){return new Tuple<PointF, PointF>(intersections[0], intersections[1]);}else if (intersections.Count == 1){// 直线与矩形相切(仅一个交点)return new Tuple<PointF, PointF>(intersections[0], intersections[0]);}else{// 无交点(直线完全在矩形外或矩形退化为点)return null;}}private static List<PointF> RemoveDuplicates(List<PointF> points){List<PointF> uniquePoints = new List<PointF>();foreach (var point in points){bool isDuplicate = false;foreach (var uniquePoint in uniquePoints){if (Math.Abs(point.X - uniquePoint.X) < 1e-6 &&Math.Abs(point.Y - uniquePoint.Y) < 1e-6){isDuplicate = true;break;}}if (!isDuplicate)uniquePoints.Add(point);}return uniquePoints;}}internal class Program{[STAThread]private static void Main(){string filePath = "rotate.csv";var points = new List<PointF>();using (var reader = new StreamReader(filePath)){while (!reader.EndOfStream){string line = reader.ReadLine();string[] values = line.Split(';');if (values.Length == 3 &&double.TryParse(values[0], out double x) &&double.TryParse(values[1], out double y) &&double.TryParse(values[2], out double z)){points.Add(new PointF(x, y));}else{Console.WriteLine($"跳过无效行: {line}");}}}double ux = 0.935277;double uy = -0.353918;double vx = 0.353918;double vy = 0.935277;double Ox = -1.274;double Oy = 9.878;double[,] transformationMatrix = CoordinateTransform.CalculateTransformationMatrix(Ox, Oy, ux, uy, vx, vy);BoundingBox box = BoundingBoxCalculator.GetTransformedBoundingBox(points, transformationMatrix);double outerline_A = 0.354;double outerline_B = 0.935;double outerline_C = -8.788;(double a, double b, double c) = LineTransformer.TransformLineEquation(outerline_A, outerline_B, outerline_C, transformationMatrix);Tuple<PointF, PointF> result = LineSegmentCalculator.GetLineSegmentInBoundingBox(a, b, c, box);}}
}