Guest User

Untitled

a guest
Jan 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1.     struct Vector3
  2.     {
  3.         public float x;
  4.         public float y;
  5.         public float z;
  6.     }
  7.  
  8.     public static class Bezier
  9.     {
  10.         public static void LoadProfile()
  11.         {
  12.  
  13.             StreamWriter sw = new StreamWriter("C:\\test.xml");
  14.             XmlDocument xml = new XmlDocument();
  15.             xml.Load("1-5_dwarf_gnome.xml");
  16.             XmlNodeList nodes = xml.DocumentElement.SelectNodes("/graph/nodes/node");
  17.  
  18.             int nodeCount = 0;
  19.  
  20.             for (int i = 0; i < nodes.Count - 1; i += 2)
  21.             {
  22.                 if (i + 2 > nodes.Count - 1) break;
  23.                 Vector3 a;
  24.                 //MessageBox.Show(nodes[i].Attributes["x"].Value);
  25.                 a.x = float.Parse(nodes[i].Attributes["x"].Value.Replace('.', ','));
  26.                 a.y = float.Parse(nodes[i].Attributes["y"].Value.Replace('.', ','));
  27.                 a.z = float.Parse(nodes[i].Attributes["z"].Value.Replace('.', ','));
  28.                 Vector3 b;
  29.                 b.x = float.Parse(nodes[i + 1].Attributes["x"].Value.Replace('.', ','));
  30.                 b.y = float.Parse(nodes[i + 1].Attributes["y"].Value.Replace('.', ','));
  31.                 b.z = float.Parse(nodes[i + 1].Attributes["z"].Value.Replace('.', ','));
  32.                 Vector3 c;
  33.                 c.x = float.Parse(nodes[i + 2].Attributes["x"].Value.Replace('.', ','));
  34.                 c.y = float.Parse(nodes[i + 2].Attributes["y"].Value.Replace('.', ','));
  35.                 c.z = float.Parse(nodes[i + 2].Attributes["z"].Value.Replace('.', ','));
  36.                 Vector3 p;
  37.  
  38.                 for (float f = 0.0f; f < 1.0f; f += 0.2f)
  39.                 {
  40.                     p = Bezier.QuadraticBezier(a, b, c, f);
  41.                     sw.WriteLine("<node id=\"" + nodeCount + "\" x=\"" + p.x.ToString().Replace(',', '.') + "\" y=\"" + p.y.ToString().Replace(',', '.') + "\" z=\"" + p.z.ToString().Replace(',', '.') + "\" />");
  42.                     nodeCount++;
  43.                 }
  44.  
  45.             }
  46.  
  47.             sw.Close();
  48.  
  49.         }
  50.  
  51.         static Vector3 LinearInterpolation(Vector3 a, Vector3 b, float t)
  52.         {
  53.             Vector3 p;
  54.             p.x = a.x + (b.x - a.x) * t;
  55.             p.y = a.y + (b.y - a.y) * t;
  56.             p.z = a.z + (b.z - a.z) * t;
  57.             return p;
  58.         }
  59.  
  60.         // Create a quadratic bezier curve between a and c with b as control point
  61.         static Vector3 QuadraticBezier(Vector3 a, Vector3 b, Vector3 c, float t)
  62.         {
  63.             Vector3 p, ab, bc;
  64.             ab = LinearInterpolation(a, b, t);
  65.             bc = LinearInterpolation(b, c, t);
  66.             p = LinearInterpolation(ab, bc, t);
  67.             return p;
  68.         }
  69.     }
Add Comment
Please, Sign In to add comment