private void getAngleAndCenter(ref double angle, ref MyPointer point, int start, int end) { double resultX = 0, tempX = 0; double resultY = 0, tempY = 0; int count = 0, tempCount = 0; //获取线段中心点 for (int i = start; i < end; ++i) { resultX += pointers[i].x; resultY += pointers[i].y; ++count; } point.x = resultX / count; point.y = resultY / count; //角度计算 count = 0; resultX = resultY = 0; for (int i = start; i < end; ++i) { if (point.y < pointers[i].y) { ++count; resultX += pointers[i].x - point.x; resultY += pointers[i].y - point.y; } else if (point.y > pointers[i].y) { ++tempCount; tempX += pointers[i].x - point.x; tempY += pointers[i].y - point.y; } } if ((count == 0 && tempCount > 0) || ((count != 0 && tempCount != 0) && (Math.Abs(tempX / tempCount) < Math.Abs(resultX / count)))) { count = tempCount; resultX = -tempX; resultY = -tempY; } //4-22 if (count == 0) { ++count; } if (Math.Abs(resultX) < 0.0000001) { resultX = 1; } //end 4-22 resultX /= count; resultY /= count; angle = Math.Atan(resultY / resultX); if (angle < 0) { angle += 3.14159; } }
private void getMid() { double tempLength; MyPointer tempPoint = new MyPointer(); if (pointLeft.y < pointRight.y) { tempLength = pointRight.y - pointLeft.y; tempPoint.x = pointRight.x - tempLength * Math.Tan(angleRight * 3.14159 / 180); tempPoint.y = pointLeft.y; pointMid.x = (tempPoint.x + pointLeft.x) / 2; pointMid.y = (tempPoint.y + pointLeft.y) / 2; } else { tempLength = pointLeft.y - pointRight.y; tempPoint.x = pointLeft.x - tempLength * Math.Tan(angleLeft * 3.14159 / 180); tempPoint.y = pointRight.y; pointMid.x = (tempPoint.x + pointRight.x) / 2; pointMid.y = (tempPoint.y + pointRight.y) / 2; } // original //angleMid = (angleLeft + angleRight) / 2; //angle = angleMid * 180 / 3.1415; if (Math.Abs(angleLeft - angleRight) < 0.15) // 两者相差小于9度 { angleMid = (angleLeft + angleRight) / 2; angle = angleMid * 180 / 3.1415; } else if (Math.Abs(angleLeft - 1.57) <= Math.Abs(angleRight - 1.57) && Math.Abs(angleLeft - 1.57) < 0.2) { angleMid = angleLeft; angle = angleMid * 180 / 3.1415; } else if (Math.Abs(angleLeft - 1.57) > Math.Abs(angleRight - 1.57) && Math.Abs(angleRight - 1.57) < 0.2) { angleMid = angleRight; angle = angleMid * 180 / 3.1415; } else { angle = 90; } }