///<summary> /// Computes the incentre of a triangle. ///</summary> /// <remarks> /// The <c>InCentre</c> of a triangle is the point which is equidistant /// from the sides of the triangle. /// It is also the point at which the bisectors of the triangle's angles meet. /// It is the centre of the triangle's <c>InCircle</c>, which is the unique circle /// that is tangent to each of the triangle's three sides. /// </remarks> /// <param name="a">A vertex of the triangle</param> /// <param name="b">A vertex of the triangle</param> /// <param name="c">A vertex of the triangle</param> /// <returns>The point which is the incentre of the triangle</returns> public static Coordinate InCentre(Coordinate a, Coordinate b, Coordinate c) { // the lengths of the sides, labelled by their opposite vertex double len0 = b.Distance(c); double len1 = a.Distance(c); double len2 = a.Distance(b); double circum = len0 + len1 + len2; double inCentreX = (len0 * a.X + len1 * b.X + len2 * c.X) / circum; double inCentreY = (len0 * a.Y + len1 * b.Y + len2 * c.Y) / circum; return(new Coordinate(inCentreX, inCentreY)); }
///<summary>Computes the point at which the bisector of the angle ABC cuts the segment AC.</summary> /// <param name="a">A vertex of the triangle</param> /// <param name="b">A vertex of the triangle</param> /// <param name="c">A vertex of the triangle</param> /// <returns>The angle bisector cut point</returns> public static Coordinate AngleBisector(Coordinate a, Coordinate b, Coordinate c) { /* * Uses the fact that the lengths of the parts of the split segment * are proportional to the lengths of the adjacent triangle sides */ double len0 = b.Distance(a); double len2 = b.Distance(c); double frac = len0 / (len0 + len2); double dx = c.X - a.X; double dy = c.Y - a.Y; Coordinate splitPt = new Coordinate(a.X + frac * dx, a.Y + frac * dy); return(splitPt); }
/// <summary> /// Computes the closest point on this line segment to another point. /// </summary> /// <param name="p">The point to find the closest point to.</param> /// <returns> /// A Coordinate which is the closest point on the line segment to the point p. /// </returns> public Coordinate ClosestPoint(Coordinate p) { var factor = ProjectionFactor(p); if (factor > 0 && factor < 1) { return(Project(p)); } var dist0 = _p0.Distance(p); var dist1 = _p1.Distance(p); return(dist0 < dist1 ? _p0 : _p1); }
///<summary>Computes the length of the longest side of a triangle</summary> /// <param name="a">A vertex of the triangle</param> /// <param name="b">A vertex of the triangle</param> /// <param name="c">A vertex of the triangle</param> /// <returns>The length of the longest side of the triangle</returns> public static double LongestSideLength(Coordinate a, Coordinate b, Coordinate c) { // ReSharper disable InconsistentNaming double lenAB = a.Distance(b); double lenBC = b.Distance(c); double lenCA = c.Distance(a); // ReSharper restore InconsistentNaming double maxLen = lenAB; if (lenBC > maxLen) { maxLen = lenBC; } if (lenCA > maxLen) { maxLen = lenCA; } return(maxLen); }