/// <summary> /// 获取一个向量的unit 法向量,该法向量位于向量右侧,向量不能为零向量 /// </summary> /// <param name="vtr"></param> public static OxyzPointF GetNormal(OxyzPointF vtr) { double iDX = vtr._X * vtr._X; double iDY = vtr._Y * vtr._Y; double dDistance = Math.Sqrt(iDX + iDY); double dx = 0d; double dy = 0d; OxyzPointF p1 = new OxyzPointF(0f, 0f, 0f); OxyzPointF p2 = new OxyzPointF(0f, 0f, 0f); if (vtr._Y != 0) { dx = vtr._Y / dDistance; dy = dx * vtr._X / vtr._Y; p1 = new OxyzPointF((float)dx, (float)dy); p2 = new OxyzPointF((float)-dx, (float)-dy); } else if (vtr._X != 0) { dy = vtr._X / dDistance; dx = dy * vtr._Y / vtr._X; p1 = new OxyzPointF((float)dx, (float)dy); p2 = new OxyzPointF((float)-dx, (float)-dy); } if (VectorTool.GetVectorPos(p1, new OxyzPointF(vtr._X, vtr._Y)) == -1) { return(new OxyzPointF(p1._X, -p1._Y)); } return(new OxyzPointF(p2._X, -p2._Y)); }
public static OxyzPointF mpBaseVector = new OxyzPointF(0, 1.0f);//使用x轴做基向量 /// <summary> /// 按照参数2指定的向量与基向量的夹角计算参数1点的绝对元胞坐标 /// </summary> /// <param name="vector"></param> /// <returns></returns> // public static Point GetRealXY(Point point, OxyzPointF newVector) // { // if (newVector == null) // { // throw new ArgumentException("输入的参数不能为零"); // } // //获取正弦和余弦值并且进行旋转变换 // SinCos sc = VectorTools.GetSinCos(mpBaseVector, newVector); // return Coordinates.Rotate(point, sc); // } /// <summary> /// 两个点的欧式距离 /// </summary> //public static int Distance(Point p1, Point p2) //{ // int iX = Math.Abs(p1.X - p2.X); // int iY = Math.Abs(p1.Y - p2.Y); // return (int)Math.Round(Math.Sqrt(iX * iX + iY * iY)); //} //------------------------20160202-------------------------计算两个点的欧式距离 public static double Distance(OxyzPointF p1, OxyzPointF p2) { double iX = Math.Abs(p1._X - p2._X); double iY = Math.Abs(p1._Y - p2._Y); double iZ = Math.Abs(p1._Z - p2._Z); return(Math.Sqrt(iX * iX + iY * iY + iZ * iZ)); }
public static Point GetNormal(Point start, Point end) { if (start != end) { var vector = new OxyzPointF(end.X - start.X, end.Y - start.Y); return(VectorTool.GetNormal(vector).ToPoint()); } //return new OxyzPointF(-opVector._X, -opVector._Y); throw new Exception(); }
/// <summary> /// to enlarge a point for drawing.use screen coordinates /// </summary> /// <param name="mp"></param> /// <param name="iWidth"></param> /// <returns>new points</returns> public static OxyzPointF Offset(OxyzPointF mOld, int iWidth) { double dWidth = -1 * iWidth; mOld._X += dWidth; mOld._Y += dWidth; mOld._Z += dWidth; return(mOld); }
/// <summary> /// to enlarge a point for drawing.use screen coordinates /// </summary> /// <param name="mp"></param> /// <param name="iWidth"></param> /// <returns>new points</returns> public static OxyzPointF Offset(OxyzPointF mOld, int iWidth, OxyzPointF vector) { double dWidth = iWidth; mOld._X += dWidth * vector._X; mOld._Y += dWidth * vector._Y; mOld._Z += dWidth * vector._Z; return(mOld); }
/// <summary> /// offset中x和y的值向左上偏移,其xy都为负值 /// </summary> /// <param name="scrnPoint">原坐标系</param> /// <param name="offset">偏移坐标</param> // /// <returns>返回值为新参数</returns> // public static PointF Offset(PointF scrnPoint, PointF offset) // { // //计算平移(偏移) //// scrnPoint.X += offset.X; //// scrnPoint.Y += offset.Y; //// return scrnPoint;//这个是个结构参数复制,然后返回新的结果 // return Coordinates.Offset(new OxyzPointF(scrnPoint.X,scrnPoint.Y,0f),new OxyzPointF(offset.X,offset.Y,0f)).ToPointF(); // } /// <summary> /// vector use math coordinates .while offset servers screen coordinates,Y is reversed .so .Y needs to be reverse /// </summary> /// <param name="scrnPoint"></param> /// <param name="offset"></param> /// <returns></returns> public static OxyzPointF Offset(OxyzPointF scrnPoint, OxyzPointF offset) { OxyzPointF mp = new OxyzPointF(scrnPoint._X, scrnPoint._Y); //计算平移(偏移) mp._X += offset._X; //mp._Y -= offset._Y; mp._Y += offset._Y; return(mp); }
/// <summary> /// 以点mpA和点mpB为终点建立的直线方程,然后判断点mpNew在直线的上方还是下方 /// </summary> /// <param name="mpA">直线起点坐标</param> /// <param name="mpB">直线终点坐标</param> /// <param name="mpNew">要检验的点坐标</param> /// <returns>返回0表示点mpNew位于直线上,返回1表示点mpNew位于向量上方,返回-1表示点mpNew位于直线下方</returns> public static int GetPointPosition(OxyzPointF mpA, OxyzPointF mpB, OxyzPointF mpNew) { if (mpB._X == mpA._X && mpB._Y == mpA._Y) { throw new ArgumentException("直线的两个端点不能相同"); } double fResult = (mpNew._Y - mpA._Y) * (mpB._X - mpA._X) - (mpNew._X - mpA._X) * (mpB._Y - mpA._Y); if (Math.Abs(fResult) < 0.9f) //绝对值在1之内,因为点坐标系有误差造成的 { return(0); //返回0 表示向量在直线上 } return(fResult >= 0.9f ? 1 : -1); }
///// <summary> ///// convert cartesian coordinates to screen coordinates and enlarge it 转换为屏幕坐标系; ///// </summary> ///// <param name="iScaleFactor"></param> // public static PointF Project(OxyzPointF p, int iScaleFactor) // { // return Coordinates.Project(p, iScaleFactor).ToPointF(); // } /// <summary> /// screen coordinates scalaor /// </summary> /// <param name="mp">cartesian coordinates</param> /// <param name="iScaleFactor">一个元胞长度对应的屏幕像素点数</param> /// <returns></returns> public static OxyzPointF Project(OxyzPointF mp, int iScaleFactor) { OxyzPointF scrnPoint = new OxyzPointF(); scrnPoint._X = (float)Math.Round(iScaleFactor * mp._X); scrnPoint._Y = (float)Math.Round(iScaleFactor * mp._Y); // //计算平移(偏移) // scrnPoint.X -= Coordinates.GraphicsOffset.X; // scrnPoint.Y -= Coordinates.GraphicsOffset.Y; return(Coordinates.Offset(scrnPoint, Coordinates.GraphicsOffset)); //return scrnPoint;//这个是个结构参数复制,然后返回新的结果 }
/// <summary> /// 这个是Cartesian笛卡尔坐标系下的结果 ///利用两点式,建立基础方程,并且利用这个方程计算输入点与基方程的联系,调用三参数的重载,第一个参数默认为零,返回-1表示位于基向量下方或者是右方,返回1表示位于基向量上方或者是左方 /// </summary> /// <param name="vBase">基向量的终点坐标,起点坐标为0</param> /// <param name="vNew">要检验的点的坐标如果是向量,应当输入向量的终点坐标</param> /// <returns>返回-1表示位于基向量下方或者是右方,返回1表示位于基向量上方或者是左方</returns> public static int GetVectorPos(OxyzPointF vBase, OxyzPointF vNew) { if (vBase._X == 0 && vBase._Y == 0 || vNew._X == 0 && vNew._Y == 0) { throw new ArgumentException("基向量或者要判定的向量不能为0向量"); } int iReturn = VectorTool.GetPointPosition(new OxyzPointF(0.0f, 0.0f), vBase, vNew); if (iReturn == 0 && vNew._X < vBase._X) { iReturn = 2; } return(iReturn); }
/// <summary> /// 获取两个向量的夹角的余弦值,该值的区间是-1到1闭区间,两个参数向量都不能是0向量 /// </summary> public static double GetCosine(OxyzPointF vBase, OxyzPointF vNew) { //向量的数量积 double fNumerator = vBase._X * vNew._X + vBase._Y * vBase._Y; //第一个向量(基向量)的摸 double dBaseM = vBase._X * vBase._X + vBase._Y * vBase._Y; double dBase = Math.Sqrt(dBaseM); //第二个向量的模 double dNewM = vNew._X * vNew._X + vNew._Y * vNew._Y; double dNew = Math.Sqrt(dNewM); //两个向量的模的乘积 double dDenominator = dBase * dNew; if (dDenominator == 0.0) { throw new DivideByZeroException("向量的模为0是不允许的,无法计算0向量的角度"); } ///返回余弦值 return(fNumerator / dDenominator); }
/// <summary> /// 判定角度并且输出角度的正弦和余弦值,315-45 为0度 45-135度变为90度,135-225 为180度 225-315度变为270度, /// </summary> public static SinCos GetSinCos(OxyzPointF mpBaseVector, OxyzPointF mpVector) { //由于0.707小于根号2的一半所以45度变为90度 double dCosineValue = VectorTool.GetCosine(mpBaseVector, mpVector); //-45 180 +45度的左开右闭闭区间 if (-1.001 <= dCosineValue && dCosineValue < -0.708) //-根号2的是是1.414 其一半 是0.707 { return(new SinCos(0, -1)); //cosine 180 是-1; } int irtlPos = VectorTool.GetVectorPos(mpBaseVector, mpVector); if (-0.708 <= dCosineValue && dCosineValue < 0.708) { //判断y位于基向量的上方还是下方 if (irtlPos < -0.1) //-45 270 +45度 { return(new SinCos(-1, 0)); //270 } if (irtlPos > 0.1) // -45 90 +45度 { return(new SinCos(1, 0)); //90 } if (-0.1 < irtlPos && irtlPos < 0.1) { return(new SinCos(1, 0)); } else//iPos==0 当前角度下 这是不可能出现的 { throw new Exception("不可能出现的值"); } } //这之间的有可能是零度,有可能是360度 if (0.708 <= dCosineValue && dCosineValue <= 1.001) { //315度到44度之间全部当作0度处理 return(new SinCos(0, 1)); } throw new Exception("不可能没有捕获所有的角度值"); }
/// <summary> /// 获取向量的逆向量 /// </summary> /// <param name="opVector"></param> /// <returns></returns> public static OxyzPointF GetInverse(OxyzPointF opVector) { return(new OxyzPointF(-opVector._X, -opVector._Y)); }
/// <summary> /// 获取一个单位向量的倍数向量 /// </summary> /// <param name="uniVector"></param> /// <param name="iMuti"></param> /// <returns></returns> public static OxyzPointF GetMultiNormal(OxyzPointF uniVector, int iMuti) { return(new OxyzPointF(uniVector._X * iMuti, uniVector._Y * iMuti, uniVector._Z * iMuti)); }
public static OxyzPointF Offset(OxyzPoint scrnPoint, OxyzPointF offset) { return(Coordinates.Offset(scrnPoint.ToOxyzPointF(), offset)); }
public static OxyzPointF Muilt(OxyzPointF a, Int32 iScaler) { return(new OxyzPointF(a._X * iScaler, a._Y * iScaler, a._Z * iScaler)); }
public OxyzPoint(OxyzPointF opF) { this._X = Convert.ToInt32(opF._X); this._Y = Convert.ToInt32(opF._Y); this._Z = Convert.ToInt32(opF._Z); }