Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static readonly XNamespace SvgNamespace = "http://www.w3.org/2000/svg";
- [Pure]
- private static string Svgify(double value)
- {
- return value.ToString("0.##", CultureInfo.InvariantCulture);
- }
- [Pure]
- private static XElement Tag(string name, params (string Name, string Value)[] attributes)
- {
- return new XElement(SvgNamespace + name, ToXmlAttributes(attributes));
- }
- [Pure]
- private static XElement Tag(string name, string content, params (string Name, string Value)[] attributes)
- {
- return new XElement(SvgNamespace + name, content, ToXmlAttributes(attributes));
- }
- [Pure]
- private static IEnumerable<XAttribute> ToXmlAttributes([NotNull] (string Name, string Value)[] attributes)
- {
- return from attribute in attributes select new XAttribute(attribute.Name, attribute.Value);
- }
- [UsedForDebug]
- internal static void SaveToSvg([NotNull] this Skeleton self, [NotNull] string path)
- {
- ThrowIf.Argument.IsNull(self, nameof(self));
- ThrowIf.Argument.IsNull(path, nameof(path));
- var svg = Tag("svg", ("viewBox", "0 0 3000 3000"));
- foreach (var bone in self.Bones)
- {
- var points = new StringBuilder();
- foreach (var vertex in bone.Vertices)
- {
- points.Append($"{Svgify(vertex.Position.X)},{Svgify(vertex.Position.Y)} ");
- }
- svg.Add
- (
- Tag("polyline", ("points", points.ToString()), ("fill", "none"), ("stroke", "black"))
- );
- }
- var sign = +1;
- var boneCounter = 0;
- foreach (var bone in self.Bones)
- {
- var vertexCounter = 0;
- foreach (var vertex in bone.Vertices)
- {
- var positionX = vertex.Position.X;
- var positionY = vertex.Position.Y;
- {
- var x = Svgify(positionX);
- var y = Svgify(positionY);
- svg.Add
- (
- Tag("circle", ("cx", x), ("cy", y), ("r", "5"), ("stroke", "red"))
- );
- }
- {
- var x = Svgify(positionX + sign * 10);
- var y = Svgify(positionY + sign * 10);
- svg.Add
- (
- Tag("text", vertexCounter.ToString(), ("x", x), ("y", y), ("style", "fill:green; font-size:10px;"))
- );
- vertexCounter++;
- }
- }
- var boneStartX = Svgify(bone.Vertices[0].Position.X - sign * 20);
- var boneStartY = Svgify(bone.Vertices[0].Position.Y - sign * 20);
- svg.Add
- (
- Tag("text", boneCounter.ToString(), ("x", boneStartX), ("y", boneStartY), ("style", "fill:blue; font-size:10px;"))
- );
- boneCounter++;
- sign *= -1;
- }
- using (var xmlWriter = XmlWriter.Create(path))
- {
- svg.Save(xmlWriter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement