123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package garten.model;
-
- /**
- *
- * @author Jan
- */
- import java.text.CharacterIterator;
- import java.util.ArrayList;
- import java.util.Collections;
- import lsystem.Grammar;
- import lsystem.collection.TurtleStack;
- import lsystem.turtle.Turtle;
-
- public class PflanzeImpl implements Pflanze
- {
- ArrayList<float[]> pts;
- protected float DELTA; // 18 degrees
- protected Grammar grammar;
- final protected String axiom = "F";
- protected int generations;
- //protected String rule;
- protected float startLength;
- protected float drawLength;
- protected float theta;
- protected float xpos;
- protected float ypos;
-
- protected int positionx;
- protected int positiony;
-
- protected int flaeche;
-
- protected float[] boundingBoxes;
- ArrayList<Float> tempx;
- ArrayList<Float> tempy;
- //Position();
- TurtleStack ts;
-
- public PflanzeImpl(int xstart, int ystart, int generation, int startLength)
- {
- this.positionx = xstart;
- this.positiony = ystart;
- this.generations = generation;
- this.startLength = startLength;
- pts = new ArrayList<float[]>();
- ts = new TurtleStack();
- boundingBoxes=new float[4]; //minx, miny, maxx, maxy
- tempx = new ArrayList<>();
- tempy = new ArrayList<>();
- }
-
- public void translateRules()
- {
-
- float x_temp, y_temp;
- Turtle turtle = new Turtle(positionx, positiony, (float) ((Math.PI) / 2));
- CharacterIterator it = grammar.getIterator();
- for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next())
- {
- switch (ch)
- {
- case 'F':
- x_temp = turtle.getX();
- y_temp = turtle.getY();
- turtle.setX((float) (x_temp + drawLength * Math.cos((turtle.getTheta()))));
- turtle.setY((float) (y_temp - drawLength * Math.sin((turtle.getTheta()))));
- float[] temp =
- {
- x_temp, y_temp, turtle.getX(), turtle.getY()
- };
- tempx.add(x_temp);
- tempx.add(turtle.getX());
- tempy.add(y_temp);
- tempy.add(turtle.getY());
-
- pts.add(temp);
-
- break;
- case '+':
- turtle.setTheta(turtle.getTheta() + DELTA);
- break;
- case '-':
- turtle.setTheta(turtle.getTheta() - DELTA);
- break;
- case '[':
- ts.push(new Turtle(turtle)); // Uncomment this (and comment previous line to avoid using clone)
- break;
- case ']':
- turtle = ts.pop();
- break;
- default:
- System.err.println("character " + ch + " not in grammar");
- }
- }
- setBoundingBoxes();
- }
-
- void setBoundingBoxes()
- {
- boundingBoxes[0] = Collections.min(tempx);
- boundingBoxes[1] = Collections.min(tempy);
- boundingBoxes[2] = Collections.max(tempx);
- boundingBoxes[3] = Collections.max(tempy);
- }
- public float [] getBoundingBoxes()
- {
- return boundingBoxes;
- }
-
- @Override
- public ArrayList<float[]> getPoints()
- {
-
- return pts;
- }
-
- @Override
- public int getPositionx()
- {
- return positionx;
- }
-
- @Override
- public int getPositiony()
- {
- return positiony;
- }
-
- @Override
- public void setPositionx(int x)
- {
- positionx = x;
- }
-
- @Override
- public void setPositiony(int y)
- {
- positiony = y;
- }
-
- @Override
- public int getflaeche()
- {
- return flaeche;
- }
-
- @Override
- public void setAlgorithm()
- {
- pts.removeAll(pts);
- translateRules();
- }
-
- public void setPosition()
- {
- }
-
- @Override
- public void setflaeche(int flaeche)
- {
- }
-
- }
|