import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.media.opengl.GLAutoDrawable;
import Vertex;
import Facade;
import Quad;
public class Grid2DArea {
private List<Quad> quads;
public Grid2DArea() {
quads = new ArrayList<Quad>();
}
public void addQuad(Quad addQuad) {
if (addQuad != null) {
quads.add(addQuad);
}
}
public void difference(Quad diffQuad) {
if (diffQuad != null) {
List<Quad> containQuads = new ArrayList<Quad>();
for (Quad currentQuad : quads) {
if (currentQuad.contain(diffQuad)) {
containQuads.add(currentQuad);
}
}
recalculateArea(containQuads, diffQuad);
}
}
/**
* Berechnet die Rechtecke um eine Geometry darzustellen
*
* @param containQuads
* @param diffQuad
*/
public void recalculateArea(List<Quad> containQuads, Quad diffQuad) {
float[] dimension = Facade.getSceneDimension();
List<Float> xCoords = new ArrayList<Float>();
List<Float> yCoords = new ArrayList<Float>();
List<Quad> tempQuads = new ArrayList<Quad>();
tempQuads.addAll(containQuads);
tempQuads.add(diffQuad);
for (Quad currentQuad : tempQuads) {
Vertex leftTopCorner = currentQuad.getLeftTopCorner();
Vertex rightBottomCorner = currentQuad.getRightBottomCorner();
if (!xCoords.contains(leftTopCorner.getX())) {
xCoords.add(leftTopCorner.getX());
}
if (!xCoords.contains(rightBottomCorner.getX())) {
xCoords.add(rightBottomCorner.getX());
}
if (!yCoords.contains(leftTopCorner.getZ())) {
yCoords.add(leftTopCorner.getZ());
}
if (!yCoords.contains(rightBottomCorner.getZ())) {
yCoords.add(rightBottomCorner.getZ());
}
}
Collections.sort(xCoords);
Collections.sort(yCoords);
remove(containQuads);
// Neue Rechtecke hinzufuegen
for (int x = 1; x < xCoords.size(); x++) {
for (int y = 1; y < yCoords.size(); y++) {
Quad addQuad = new Quad(new Vertex(xCoords.get(x - 1), dimension[4] + 0.5f, yCoords.get(y - 1)),
new Vertex(xCoords.get(x), dimension[4] + 0.5f, yCoords.get(y)));
for (Quad currentQuad : containQuads) {
if (currentQuad.contain(addQuad) && !diffQuad.contain(addQuad)) {
quads.add(addQuad);
}
}
}
}
}
/**
* Entfernt alle Rechtecke von der Grundflaeche
*/
public void removeAll() {
quads.clear();
quads = new ArrayList<Quad>();
}
/**
* Entfernt alle uebergebenen Quads
*
* @param containQuad
* Zu entfernenden Rechtecke
*/
public void remove(List<Quad> containQuad) {
for (Quad currentQuad : containQuad) {
quads.remove(currentQuad);
}
}
/**
* Zeigt alle Rechtecke an
*
* @param drawable
*/
public void display(GLAutoDrawable drawable) {
for (Quad currentQuad : quads) {
currentQuad.display(drawable);
}
}
}