public static CompassPoint DirectionFromAngle(out float angle, Vector delta) { angle = (float)-(Math.Atan2(delta.Y, delta.X) / Math.PI * 180.0); var compassPoint = CompassPoint.East; if (Numeric.InRange(angle, 0, 45)) { compassPoint = CompassPoint.NorthWest; } else if (Numeric.InRange(angle, 45, 90)) { compassPoint = CompassPoint.SouthEast; } else if (Numeric.InRange(angle, 90, 135)) { compassPoint = CompassPoint.SouthWest; } else if (Numeric.InRange(angle, 135, 180)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, 0, -45)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, -45, -90)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, -90, -135)) { compassPoint = CompassPoint.NorthWest; } else if (Numeric.InRange(angle, -135, -180)) { compassPoint = CompassPoint.SouthEast; } return(compassPoint); }
private static void Annotate(XGraphics graphics, Palette palette, LineSegment lineSegment, TextBlock text, StringAlignment alignment) { Vector point; var delta = lineSegment.Delta; switch (alignment) { case StringAlignment.Near: default: point = lineSegment.Start; delta.Negate(); break; case StringAlignment.Center: point = lineSegment.Mid; break; case StringAlignment.Far: point = lineSegment.End; break; } var bounds = new Rect(point, Vector.Zero); bounds.Inflate(Settings.TextOffsetFromConnection); var angle = (float)-(Math.Atan2(delta.Y, delta.X) / Math.PI * 180.0); var compassPoint = CompassPoint.East; if (Numeric.InRange(angle, 0, 45)) { compassPoint = CompassPoint.NorthWest; } else if (Numeric.InRange(angle, 45, 90)) { compassPoint = CompassPoint.SouthEast; } else if (Numeric.InRange(angle, 90, 135)) { compassPoint = CompassPoint.SouthWest; } else if (Numeric.InRange(angle, 135, 180)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, 0, -45)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, -45, -90)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, -90, -135)) { compassPoint = CompassPoint.NorthWest; } else if (Numeric.InRange(angle, -135, -180)) { compassPoint = CompassPoint.SouthEast; } var pos = bounds.GetCorner(compassPoint); var format = new XStringFormat(); Drawing.SetAlignmentFromCardinalOrOrdinalDirection(format, compassPoint); if (alignment == StringAlignment.Center && Numeric.InRange(angle, -10, 10)) { // HACK: if the line segment is pretty horizontal and we're drawing mid-line text, // move text below the line to get it out of the way of any labels at the ends, // and center the text so it fits onto a line between two proximal rooms. pos = bounds.GetCorner(CompassPoint.South); format.Alignment = XStringAlignment.Center; format.LineAlignment = XLineAlignment.Near; } if (!Settings.DebugDisableTextRendering) { text.Draw(graphics, Settings.LineFont, palette.LineTextBrush, pos, Vector.Zero, format); } }