You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PflanzeImpl.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package garten.model;
  7. /**
  8. *
  9. * @author Jan
  10. */
  11. import java.text.CharacterIterator;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import lsystem.Grammar;
  15. import lsystem.collection.TurtleStack;
  16. import lsystem.turtle.Turtle;
  17. public class PflanzeImpl implements Pflanze
  18. {
  19. ArrayList<float[]> pts;
  20. protected float DELTA; // 18 degrees
  21. protected Grammar grammar;
  22. final protected String axiom = "F";
  23. protected int generations;
  24. //protected String rule;
  25. protected float startLength;
  26. protected float drawLength;
  27. protected float theta;
  28. protected float xpos;
  29. protected float ypos;
  30. protected int positionx;
  31. protected int positiony;
  32. protected int flaeche;
  33. protected float[] boundingBoxes;
  34. ArrayList<Float> tempx;
  35. ArrayList<Float> tempy;
  36. //Position();
  37. TurtleStack ts;
  38. public PflanzeImpl(int xstart, int ystart, int generation, int startLength)
  39. {
  40. this.positionx = xstart;
  41. this.positiony = ystart;
  42. this.generations = generation;
  43. this.startLength = startLength;
  44. pts = new ArrayList<float[]>();
  45. ts = new TurtleStack();
  46. boundingBoxes=new float[4]; //minx, miny, maxx, maxy
  47. tempx = new ArrayList<>();
  48. tempy = new ArrayList<>();
  49. }
  50. public void translateRules()
  51. {
  52. float x_temp, y_temp;
  53. Turtle turtle = new Turtle(positionx, positiony, (float) ((Math.PI) / 2));
  54. CharacterIterator it = grammar.getIterator();
  55. for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next())
  56. {
  57. switch (ch)
  58. {
  59. case 'F':
  60. x_temp = turtle.getX();
  61. y_temp = turtle.getY();
  62. turtle.setX((float) (x_temp + drawLength * Math.cos((turtle.getTheta()))));
  63. turtle.setY((float) (y_temp - drawLength * Math.sin((turtle.getTheta()))));
  64. float[] temp =
  65. {
  66. x_temp, y_temp, turtle.getX(), turtle.getY()
  67. };
  68. tempx.add(x_temp);
  69. tempx.add(turtle.getX());
  70. tempy.add(y_temp);
  71. tempy.add(turtle.getY());
  72. pts.add(temp);
  73. break;
  74. case '+':
  75. turtle.setTheta(turtle.getTheta() + DELTA);
  76. break;
  77. case '-':
  78. turtle.setTheta(turtle.getTheta() - DELTA);
  79. break;
  80. case '[':
  81. ts.push(new Turtle(turtle)); // Uncomment this (and comment previous line to avoid using clone)
  82. break;
  83. case ']':
  84. turtle = ts.pop();
  85. break;
  86. default:
  87. System.err.println("character " + ch + " not in grammar");
  88. }
  89. }
  90. setBoundingBoxes();
  91. }
  92. void setBoundingBoxes()
  93. {
  94. boundingBoxes[0] = Collections.min(tempx);
  95. boundingBoxes[1] = Collections.min(tempy);
  96. boundingBoxes[2] = Collections.max(tempx);
  97. boundingBoxes[3] = Collections.max(tempy);
  98. }
  99. public float [] getBoundingBoxes()
  100. {
  101. return boundingBoxes;
  102. }
  103. @Override
  104. public ArrayList<float[]> getPoints()
  105. {
  106. return pts;
  107. }
  108. @Override
  109. public int getPositionx()
  110. {
  111. return positionx;
  112. }
  113. @Override
  114. public int getPositiony()
  115. {
  116. return positiony;
  117. }
  118. @Override
  119. public void setPositionx(int x)
  120. {
  121. positionx = x;
  122. }
  123. @Override
  124. public void setPositiony(int y)
  125. {
  126. positiony = y;
  127. }
  128. @Override
  129. public int getflaeche()
  130. {
  131. return flaeche;
  132. }
  133. @Override
  134. public void setAlgorithm()
  135. {
  136. pts.removeAll(pts);
  137. translateRules();
  138. }
  139. public void setPosition()
  140. {
  141. }
  142. @Override
  143. public void setflaeche(int flaeche)
  144. {
  145. }
  146. }