能力有限,只针对直线路径
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program01
{
static void Main(string[] args)
{
//string path = "M-14,205 H20 V-205 H-20 ";
string path1 = "M10,10 H90 V90 H10 L10,10 ";
string[] str = pathTransform(path1);
Console.WriteLine(str);
}
public static string[] pathTransform(string path)
{
path = path.Remove(path.Length - 1); // 去掉最后一位的空格
string[] result = path.Split(' ');
List<string> list = new List<string>(); // 保存点集合
float tempX = 0; // 上一步的X
float tempY = 0; // 上一步的Y
float startX = tempX;
float startY = tempY;
foreach (string s in result)
{
string pointStr = s.Substring(1, s.Length - 1); // 每一步的位置
if (s.Substring(0, 1) == "M" || s.Substring(0, 1) == "L")
{
tempX = Convert.ToSingle(pointStr.Split(',')[0]);
tempY = Convert.ToSingle(pointStr.Split(',')[1]);
} else if (s.Substring(0, 1) == "H")
{
tempX = Convert.ToSingle(pointStr.Split(',')[0]);
StringBuilder stringBuilder = new StringBuilder(tempX.ToString());
stringBuilder.Append("," + tempY);
pointStr = stringBuilder.ToString();
}
else if (s.Substring(0, 1) == "V")
{
tempY = Convert.ToSingle(pointStr.Split(',')[0]);
StringBuilder stringBuilder = new StringBuilder(tempX.ToString());
stringBuilder.Append("," + tempY);
pointStr = stringBuilder.ToString();
}else if (s.Substring(0, 1) == "Z")
{
StringBuilder stringBuilder = new StringBuilder(startX.ToString());
stringBuilder.Append("," + startX);
pointStr = stringBuilder.ToString();
}
list.Add(pointStr);
}
return list.ToArray();
}
}
}