using SharpDX; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media.Media3D; namespace SparkClient.Views.UserControl.ViewportData.Helper { internal class LineCalculationHelper { private readonly float Y = -0.01F; private Vector3 point1; private Vector3 point2; /// /// 常数a(y=ax+b) /// private float a; /// /// 常数b(y=ax+b) /// private float b; public LineCalculationHelper(Vector3 point1, Vector3 point2) { this.point1 = point1; this.point2 = point2; this.a = calA(point1, point2); this.b = calB(point1, a); } /// /// 常数a的计算 /// /// /// /// private float calA(Vector3 point1, Vector3 point2) { return (point1.Z - point2.Z) / (point1.X - point2.X); } /// /// 常数b的计算 /// /// /// /// private float calB(Vector3 point1, float a) { return -(a * point1.X)+point1.Z; } public Tuple calXline(float length) { var x1 = 5; var z1 = calZ(x1); var x2 = -5; var z2 = calZ(x2); return new Tuple(new Vector3(x1, Y, z1), new Vector3(x2, Y, z2)); } public Tuple calYline(float length) { var x1 = 5; var z1 = calZVertical(x1); var x2 = -5; var z2 = calZVertical(x2); return new Tuple(new Vector3(x1, Y, z1), new Vector3(x2, Y, z2)); } public Tuple calLineOfOffset(Tuple tuple, float d) { var point1 = tuple.Item1; var point2 = tuple.Item2; var x1 = point1.X - d * (point2.Z - point1.Z)/ (float)Math.Sqrt(Square(point2.X - point1.X) + Square(point2.Z - point1.Z)); var z1 = point1.Z + d * (point2.X - point1.X) / (float)Math.Sqrt(Square(point2.X - point1.X) + Square(point2.Z - point1.Z)); var x2 = point2.X - d * (point2.Z - point1.Z) / (float)Math.Sqrt(Square(point2.X - point1.X) + Square(point2.Z - point1.Z)); var z2 = point2.Z + d * (point2.X - point1.X) / (float)Math.Sqrt(Square(point2.X - point1.X) + Square(point2.Z - point1.Z)); return new Tuple(new Vector3(x1, Y, z1), new Vector3(x2, Y, z2)); } float Square(float number) { return number * number; } public float calZ(float x, float offset = 0) { return a * x + b + offset; } public float calZVertical(float x, float offset = 0) { return (-x / a) + b + offset; } } }