Skip to content

U0200 Die Grundfläche

Aufgabenstellung

Erstellen Sie sich in Processing eine „Arbeitsfläche“, die mehrere Grundflächen wie die im Beispiel rechts in einem Raster anordnen.Versuchen Sie diese so flexibel wie möglich zu programmieren, um verschiedene Raster später realisieren zu können.

Ergebnis

Dieses Tool kann mit Mausrad eine beliebige Grid-Kombination erstellen und diese durch das Drücken einer Zifferntaste noch einmal unterteilen. Die X-Achse erreicht man jeweils durch die Kombination mit Ctrl. Mit Linksklick können Punkte erstellt werden, deren Größe mit Shift + Mausrad geändert werden kann und die mit Rechtsklick rückgängig gemacht, oder Ctrl + Rechtsklick komplett gelöscht werden können. Die mittlere Maustaste legt einen Screenshot in einen gewünschten Ordner ab, während die Rücktaste die Dicke des äußeren Rahmens toggled.

int gridSizeX = 1;
int gridSizeY = 1;
int gridChunkX = 9999;
int gridChunkY = 9999;
int pointWeight = 8;
// 0 = None, 1 = Thin, 2 = Thick
int edgeMode = 0;
// [ x, y, weight ]
ArrayList<int[]> pointStore = new ArrayList<>();
boolean setup = false;
boolean ctrlPressed = false;

final int BG_COLOR = 0;
final int BOX_COLOR = 255;
final int THIN_MARGIN = 4;
final int THICK_MARGIN = 20;

void setup() {
  fullScreen();
  background(BG_COLOR);
  // Ungefährer Offset meines Betriebssystems
  /* height -= 32; */
}

void draw() {
  stroke(BOX_COLOR);
  fill(BOX_COLOR);
  if (!setup) {
    drawGrid();
    setup = true;
  }
}

// ---- Events ----
void mouseClicked() {
  if (mouseButton == RIGHT) {
    if (ctrlPressed) {
      pointStore.clear();
    } else if (pointStore.size() > 0) {
      pointStore.remove(pointStore.size() - 1);
    }
    drawGrid();
  } else if (mouseButton == CENTER) {
    saveFrame("<Screenshot-Pfad>");
  } else {
    int[] points = {mouseX, mouseY, pointWeight};
    pointStore.add(points);
    drawGrid();
  }
}

void mouseWheel(MouseEvent event) {
  int direction = event.getCount() / Math.abs(event.getCount());
  if (event.isShiftDown()) {
    pointWeight -= 2 * direction;
    if (pointWeight <= 0) pointWeight = 1;
  } else {
    if (event.isControlDown()) {
      gridSizeX -= direction;
      if (gridSizeX <= 0) gridSizeX = 1;
    } else {
      gridSizeY -= direction;
      if (gridSizeY <= 0) gridSizeY = 1;
    }
    drawGrid();
  }
}

void keyPressed() {
  if (key == BACKSPACE) {
    edgeMode = (edgeMode + 1) % 3;
    drawGrid();
  } else if (keyCode == CONTROL) {
    ctrlPressed = true;
  } else if (Character.isDigit(key)) {
    int number = key - '0';
    if (ctrlPressed) {
      if (number == 0) {
        gridChunkX = 9999;
      } else {
        gridChunkX = number;
      }
    } else if (number == 0) {
      gridChunkY = 9999;
    } else {
      gridChunkY = number;
    }
    drawGrid();
  }
}
void keyReleased() {
  if (keyCode == CONTROL) {
    ctrlPressed = false;
  }
}

// ---- logic ----
void drawGrid() {
  background(BG_COLOR);

  int chunkAmountX = (gridSizeX - 1) / gridChunkX;
  int chunkAmountY = (gridSizeY - 1) / gridChunkY;
  int edge = edgeMode == 0 ? 0 : (edgeMode == 1 ? THIN_MARGIN : THICK_MARGIN);

  float currentY = edge;
  float fractionX = (width
     - THIN_MARGIN * (gridSizeX - chunkAmountX - 1)
     - THICK_MARGIN * chunkAmountX
     - edge * 2) / (float)gridSizeX;
  float fractionY = (height
     - THIN_MARGIN * (gridSizeY - chunkAmountY - 1)
     - THICK_MARGIN * chunkAmountY
     - edge * 2) / (float)gridSizeY;

  for (int i = 0; i < gridSizeY; i++) {
    float currentX = edge;
    for (int j = 0; j < gridSizeX; j++) {
      rect(currentX, currentY, fractionX, fractionY);
      currentX += fractionX;
      if ((j + 1) % gridChunkX == 0) {
        currentX += THICK_MARGIN;
      } else {
        currentX += THIN_MARGIN;
      }
    }
    currentY += fractionY;
    if ((i + 1) % gridChunkY == 0) {
      currentY += THICK_MARGIN;
    } else {
      currentY += THIN_MARGIN;
    }
  }

  drawPoints();
}

void drawPoints() {
  stroke(BG_COLOR);
  for (int[] points : pointStore) {
    strokeWeight(points[2]);
    point(points[0], points[1]);
  }
  stroke(BOX_COLOR);
  strokeWeight(0);
}