namespace Yw.WinFrmUI.HydroL3d
{
///
///
///
[TypeConverter(typeof(PropertySorter))]
public class Point3d
{
///
///
///
public Point3d() { }
///
///
///
public Point3d(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
///
///
///
public Point3d(double x, double y, double z)
{
this.X = (float)x;
this.Y = (float)y;
this.Z = (float)z;
}
///
///
///
public Point3d(Point3d rhs)
{
this.X = rhs.X;
this.Y = rhs.Y;
this.Z = rhs.Z;
}
///
/// x
///
[DisplayName("X")]
[PropertyOrder(1)]
public float X { get; set; }
///
/// y
///
[DisplayName("Y")]
[PropertyOrder(2)]
public float Y { get; set; }
///
/// z
///
[DisplayName("Z")]
[PropertyOrder(3)]
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(Point3d 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 LengthSquared()
{
return X * X + Y * Y + Z * Z;
}
///
///
///
public float Length()
{
return (float)Math.Sqrt(LengthSquared());
}
///
///
///
public void Normalize()
{
float length = Length();
if (length > 0)
{
X /= length;
Y /= length;
Z /= length;
}
}
///
/// - 运算
///
public static Point3d operator -(Point3d a, Point3d b)
{
return new Point3d(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
}
///
///
///
public static float DotProduct(Point3d a, Point3d b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
}
///
///
///
public static Point3d CrossProduct(Point3d a, Point3d b)
{
return new Point3d(
a.Y * b.Z - a.Z * b.Y,
a.Z * b.X - a.X * b.Z,
a.X * b.Y - a.Y * b.X);
}
}
}