Project 5

What could you create with systems of inequalities? TEKS Standards 1G, 3F, and 3G

// Declare that you want to use variables.
int LT, GT;
int LIMIT;
int VERTICAL, HORIZONTAL;
int numVertical, numHorizontal;
int leftX, rightX;
int bottomY, topY;
int[][] relations, borders;

// Set the size of your drawing canvas and
// initialize your variables.
void setup() {
  size(500, 500);
  resetSystem();
  noLoop();
}

// Draw rectangles based on constraints
// defined using a system of inequalities.
void draw() {
  background(255);
  // Add a red rectangle
  horizontal(GT, 100);
  vertical(LT, 250);
  vertical(GT, 200);
  drawSystem(255, 0, 0);
  // Add a yellow rectangle
  horizontal(LT, 100);
  vertical(LT, 200);
  drawSystem(255, 255, 0);
  // Add a blue rectangle
  vertical(GT, 200);
  horizontal(GT, 400);
  drawSystem(0, 0, 255);
}

/**
 * Describe systems of inequalities in your
 * own words. What do their solutions look
 * like? How do you find them?
 *
 *
 *
 *
 */


 /**
  * Reset variables to prepare for drawing
  * the next system.
  */
 void resetSystem() {
   LT = 0;
   GT = 1;
   VERTICAL = 0;
   HORIZONTAL = 1;
   leftX = 0;
   rightX = width;
   bottomY = height;
   topY = 0;
   LIMIT = 10;
   relations = new int[2][LIMIT];
   borders = new int[2][LIMIT];
   for (int i = 0; i < LIMIT; i++) {
     relations[VERTICAL][i] = -1;
     relations[HORIZONTAL][i] = -1;
     borders[VERTICAL][i] = -1;
     borders[HORIZONTAL][i] = -1;
   }
   numVertical = 0;
   numHorizontal = 0;
 }


/**
 * Add vertical relations to the system.
 */
void vertical(int relation, int border) {
  relations[VERTICAL][numVertical] = relation;
  borders[VERTICAL][numVertical] = border;
  numVertical++;

  if (relation == LT) {
    if (rightX != width) {
      rightX = min(rightX, border - 2);
    } else {
      rightX = border - 2;
    }

    leftX = max(0, leftX);
  } else if (relation == GT) {
    if (leftX != 0) {
      leftX = max(leftX, border + 2);
    } else {
      leftX = border + 2;
    }

    rightX = min(rightX, width);
  }

  stroke(0);
  strokeWeight(4);
  line(border, 0, border, height);
}


/**
 * Add horizontal relations to the system.
 */
void horizontal(int relation, int border) {
  relations[HORIZONTAL][numHorizontal] = relation;
  borders[HORIZONTAL][numHorizontal] = border;
  numHorizontal++;

  if (relation == LT) {
    if (bottomY != height) {
      bottomY = min(bottomY, border - 2);
    } else {
      bottomY = border - 2;
    }

    topY = max(0, topY);
  } else if (relation == GT) {
    if (topY != 0) {
      topY = max(topY, border + 2);
    } else {
      topY = border + 2;
    }

    bottomY = min(bottomY, height);
  }

  stroke(0);
  strokeWeight(4);
  line(0, border, width, border);
}


/**
 * Test relations to ensure the system has
 * a solution.
 */
boolean isValidSystem() {
  int minLT = height;
  int maxGT = 0;
  for (int i = 0; i < numHorizontal; i++) {
    if (relations[HORIZONTAL][i] == LT) {
      minLT = min(borders[HORIZONTAL][i], minLT);
    }

    if (relations[HORIZONTAL][i] == GT) {
      maxGT = max(borders[HORIZONTAL][i], maxGT);
    }
  }

  if (maxGT >= minLT) {
    return false;
  }

  minLT = width;
  maxGT = 0;
  for (int i = 0; i < numVertical; i++) {
    if (relations[VERTICAL][i] == LT) {
      minLT = min(borders[VERTICAL][i], minLT);
    }

    if (relations[VERTICAL][i] == GT) {
      maxGT = max(borders[VERTICAL][i], maxGT);
    }
  }

  if (maxGT >= minLT) {
    return false;
  }

  return true;
}


/**
 * Draw the system if it is valid.
 */
void drawSystem(int r, int g, int b) {
  if (isValidSystem()) {
    int rectWidth = rightX - leftX;
    int rectHeight = bottomY - topY;
    noStroke();
    fill(r, g, b);
    rect(leftX, topY, rectWidth, rectHeight);
  }

  resetSystem();
}

All source code is licensed under the MIT License. Text and images are free to remix under the terms of a Creative Commons Attribution 4.0 International License.