如何:使用重载的 - 号运算符和 Subtract 方法减去三维点
更新:2007 年 11 月
此示例演示如何使用重载的 - 号运算符和 Point3D 静态 Subtract 方法来减去 Point3D 结构和 Vector3D 结构。
下面的代码演示如何使用 Point3D 减法方法。首先实例化 Point3D 结构和 Vector3D 结构。使用重载的 - 号运算符减去 Point3D 结构,然后使用静态 Subtract 方法减去这些结构。接下来,使用静态 Subtract 方法从第一个 Point3D 结构中减去 Vector3D 结构。最后,使用重载的 - 号运算符从 Vector3D 中减去 Point3D 结构。
示例
// instantiate variables
Point3D point1 = new Point3D();
Point3D point2 = new Point3D(15, 40, 60);
Vector3D vector1 = new Vector3D(20, 30, 40);
Point3D pointResult1 = new Point3D();
Point3D pointResult2 = new Point3D();
Vector3D vectorResult1 = new Vector3D();
Vector3D vectorResult2 = new Vector3D();
// defining x,y,z of point1
point1.X = 10;
point1.Y = 5;
point1.Z = 1;
vectorResult1 = Point3D.Subtract(point1, point2);
// vectorResult1 is equal to (-5, -35, -59)
vectorResult2 = point2 - point1;
// vectorResult2 is equal to (5, 35, 59)
pointResult1 = Point3D.Subtract(point1, vector1);
// pointResult1 is equal to (-10, -25, -39)
pointResult2 = vector1 - point1;
// pointResult2 is equal to (10, 25, 39)