/// <summary> /// 获取一个向量的法向量,该法向量位于向量右侧,向量不能为零向量 /// </summary> /// <param name="vtr"></param> public static MyPoint getNormalVector(MyPoint 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; MyPoint p1 = null; MyPoint p2 = null; if (vtr.Y != 0) { dx = vtr.Y / dDistance; dy = dx * vtr.X / vtr.Y; p1 = new MyPoint((float)dx, (float)dy); p2 = new MyPoint((float)-dx, (float)-dy); } else if (vtr.X != 0) { dy = vtr.X / dDistance; dx = dy * vtr.Y / vtr.X; p1 = new MyPoint((float)dx, (float)dy); p2 = new MyPoint((float)-dx, (float)-dy); } if (VectorTools.getVectorPos(p1, new MyPoint(vtr.X, vtr.Y)) == -1) { return(new MyPoint(p1.X, -p1.Y)); } return(new MyPoint(p2.X, -p2.Y)); }
/// <summary> /// 判定角度并且输出角度的正弦和余弦值,315-45 为0度 45-135度变为90度 /// 135-225 为180度 225-315度变为270度, /// </summary> internal static SinCos getSinCos(MyPoint mpBaseVector, MyPoint mpVector) { //由于0.707小于根号2的一半所以45度变为90度 double dCosineValue = VectorTools.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 = VectorTools.getVectorPos(mpBaseVector, mpVector); if (-0.708 <= dCosineValue && dCosineValue < 0.708) { ///判断y位于基向量的上方还是下方 if (irtlPos < 0) //-45 270 +45度 { return(new SinCos(-1, 0)); //270 } if (irtlPos > 0) // -45 90 +45度 { return(new SinCos(1, 0)); //90 } 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("不可能没有捕获所有的角度值"); }