public static double SlopeAngle(ZZCoordinate firstPoint, ZZCoordinate nextPoint, bool degres = true) { double dx = nextPoint.X - firstPoint.X, dy = nextPoint.Y - firstPoint.Y; double slopeAngle; if (dx == 0) // Vertical slope { slopeAngle = (dy > 0) ? 90 : -90; // Upward / Downward } else if (dy == 0) // Horizontal slope { slopeAngle = (dx > 0) ? 0 : 180; // Right / Left } else { slopeAngle = ZZMath.ToDegrees(Math.Atan(dy / dx)); if (dx < 0) { slopeAngle += 180; } else if (dy < 0) { slopeAngle += 360; // Because ATan return a negative angle in the 2th and 4th quarter, but the 2th is made positive thanks to the +180 } } return((degres) ? slopeAngle : ZZMath.ToRadians(slopeAngle)); }
// http://villemin.gerard.free.fr/GeomLAV/Triangle/Calcul/RelQuel_fichiers/image089.jpg // C = point to check, A = first point of line, B = second point of line public static double GetDistancePointToLine(ZZCoordinate firstPoint, ZZCoordinate secondPoint, ZZCoordinate toCheck) { // Calculation of the triangle' sides' length double b = ZZMath.GetDistance(firstPoint, toCheck); double c = ZZMath.GetDistance(firstPoint, secondPoint); // Calculation of one of the angle attached to the straight line double angleFirstPoint = ZZMath.AngleFrom3Points(secondPoint, firstPoint, toCheck, true); // double angleA = angleARadian / (Math.PI / 180.0); // Falcultative: to know the angle in degrees double h = b * Math.Sin(angleFirstPoint); // Perpendicular distance double c1 = b * Math.Cos(angleFirstPoint); // Distance between firstPoint base of h (can be negative) double c2 = c - c1; // Distance between secondPoint base of h (can be negative) double clickCoordToFirstPoint = GetDistance(c1, h); // Distance between toCheck and firstPoint double clickCoordToSecondPoint = GetDistance(c2, h); // Distance between toCheck and secondPoint if (c1 >= 0 && c2 >= 0) // toCheck is between firstPoint and secondPoint perpendicularly { return(h); } else // toCheck isn't betwwen firstPoint and secondPoint { return((clickCoordToFirstPoint < clickCoordToSecondPoint) ? clickCoordToFirstPoint : clickCoordToSecondPoint); } }