namespace Yw.WinFrmUI.Hydro { /// /// /// public class PointL3d { /// /// /// public PointL3d() { } /// /// /// public PointL3d(float value) { this.X = value; this.Y = value; this.Z = value; } /// /// /// public PointL3d(float x, float y, float z) { this.X = x; this.Y = y; this.Z = z; } /// /// /// public PointL3d(double value) { var fValue = (float)value; this.X = fValue; this.Y = fValue; this.Z = fValue; } /// /// /// public PointL3d(double x, double y, double z) { this.X = (float)x; this.Y = (float)y; this.Z = (float)z; } /// /// /// public PointL3d(PointL3d rhs) { this.X = rhs.X; this.Y = rhs.Y; this.Z = rhs.Z; } /// /// x /// public float X { get; set; } /// /// y /// public float Y { get; set; } /// /// z /// public float Z { get; set; } /// /// 是否无效 /// public bool InValid() { if (this.X.Invalid()) { return true; } if (this.Y.Invalid()) { return true; } if (this.Z.Invalid()) { return true; } return false; } /// /// 是否有效 /// public bool Valid() { return !InValid(); } /// /// 距离 /// public float Distance(PointL3d other) { if (other == null) { return default; } return MathF.Sqrt(MathF.Pow(this.X - other.X, 2) + MathF.Pow(this.Y - other.Y, 2) + MathF.Pow(this.Z - other.Z, 2)); } /// /// 长度 /// public float Length => MathF.Sqrt(X * X + Y * Y + Z * Z); /// /// 缩放到单位长度 /// public void Normalize() { var length = Length; if (length > 0) { float num = 1f / Length; X *= num; Y *= num; Z *= num; } } /// /// 缩放到单位长度 /// public PointL3d Normalized() { var result = this; result.Normalize(); return result; } /// /// - 运算 /// public static PointL3d operator -(PointL3d a, PointL3d b) { return new PointL3d(a.X - b.X, a.Y - b.Y, a.Z - b.Z); } /// /// +运算 /// public static PointL3d operator +(PointL3d a, PointL3d b) { return new PointL3d(a.X + b.X, a.Y + b.Y, a.Z + b.Z); } /// /// /// public static float DotProduct(PointL3d a, PointL3d b) { return a.X * b.X + a.Y * b.Y + a.Z * b.Z; } /// /// /// public static PointL3d CrossProduct(PointL3d a, PointL3d b) { return new PointL3d( a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X); } /// /// 返回从相应分量中的最小值创建的向量 /// public static PointL3d ComponentMin(PointL3d a, PointL3d b) { a.X = ((a.X < b.X) ? a.X : b.X); a.Y = ((a.Y < b.Y) ? a.Y : b.Y); a.Z = ((a.Z < b.Z) ? a.Z : b.Z); return a; } /// /// 返回从相应分量中最大的一个创建的向量 /// public static PointL3d ComponentMax(PointL3d a, PointL3d b) { a.X = ((a.X > b.X) ? a.X : b.X); a.Y = ((a.Y > b.Y) ? a.Y : b.Y); a.Z = ((a.Z > b.Z) ? a.Z : b.Z); return a; } } }