Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. public class Spline {
  2.  
  3. private ArrayList<Node> nodes = new ArrayList<Node>();
  4. private int precisionSteps = 15;
  5. private float node_step;
  6. private double totalLenght= 0;
  7.  
  8. public Spline(Node node) {
  9. nodes.add(node);
  10. calculate();
  11. }
  12.  
  13. public void add(Node n) {
  14. nodes.add(n);
  15. calculate();
  16.  
  17. }
  18.  
  19. public void addNode(Node n ) {
  20. nodes.add(n);
  21. calculate();
  22. }
  23.  
  24. public void addNode(Node n , int i ) {
  25. nodes.add(i , n);
  26. calculate();
  27. }
  28.  
  29. public Node getNode(int i) {
  30. return nodes.get(i);
  31. }
  32.  
  33. public void removeNode(int i) {
  34. nodes.remove(i);
  35. calculate();
  36. }
  37.  
  38.  
  39. // na nowo oblicza calego spline
  40. // dlugosc , oraz skok
  41. public void calculate() {
  42. totalLenght = 0;
  43. for (Node n : nodes) {
  44. n.reCalculate();
  45. totalLenght += n.getLength();
  46. }
  47. // node_step = (float) 1/(nodes.size()-1);
  48. // double s;
  49. // totalLenght = 0;
  50. // Vector last ;
  51. // Vector v ;
  52. // for (int n = 0 ; n < nodes.size()-1 ; n++) {
  53. // s = 0;
  54. // last = nodes.get(n).position;
  55. // float jump = (float) 1/precisionSteps;
  56. // for (int j = 0 ; j <= precisionSteps ; j++) {
  57. //
  58. // v = calculate(n , jump*j );
  59. // System.out.println("zasieg : " + last.distance(v));
  60. //
  61. // s += last.distance(v);
  62. // last = v;
  63. //
  64. // }
  65. // nodes.get(n).lenght = s;
  66. // totalLenght += s;
  67. // }
  68. // System.out.println("nodestep:" + node_step);
  69. // System.out.println("totalLenght:" + totalLenght);
  70. // int i = 0;
  71. // for (Node an : nodes) {
  72. // System.out.println("node " +i +" totalLenght " + an.lenght);
  73. // i++;
  74. // }
  75. }
  76. // od zero do 1 wylicza przebieg po krzywej
  77. // 0 to punkt position 0 node z listy
  78. // 1 dla ostatniego node wektor end
  79.  
  80. public Vector calculate(float range) {
  81. if (range < 0 || range > 1)
  82. return null;
  83. int n = getNodeIndex(range);
  84. return calculate(n, restFromNode(n, range));
  85. }
  86.  
  87. //rnge od 0-1 to sao co powyrzej ale dla jednego wezla i tylko dla niego
  88. public Vector calculate(int nodeIndex , float range) {
  89. return nodes.get(nodeIndex).calculate(range);
  90. }
  91.  
  92. public float restFromNode(int node , float f) {
  93. float x = f-(node*node_step);
  94. return x / node_step;
  95. }
  96.  
  97. public int getNodeIndex(float f) {
  98. int i = ((int) (f/(node_step)));
  99. if (i == nodes.size()-1)
  100. return i-1;
  101. return i;
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement