Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/addons/p5.dom.js"></script>
  </head>
  <body></body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                //Eintrag fuer Codepen, damit die for-Schleife nicht abbricht. Im Debug-View bitte deaktivieren
window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 6000;

// CC0 1.0 Universal (CC0 1.0)
// https://creativecommons.org/publicdomain/zero/1.0/deed.en
// Matthias Perenthaler, 2018

var buecherImport = [];
var buecherSortiert = [];
var buecherRegal = [];
var DekoFigur = 0;
var DekoFigurZaehler = 0;
var RegalMoeglich = true;
var sortiert = false;
var eingeraeumt = false;
var maxBuchhoehe = 0;

var x_offset = 20;
var y_offset = 60;
var text_offset = 30;

var auswahl;
var analyse = false;

function reset_values() {
  buecherImport = [];
  buecherSortiert = [];
  buecherRegal = [];
  DekoFigur = 0;
  DekoFigurZaehler = 0;
  RegalMoeglich = true;
  sortiert = false;
  eingeraeumt = false;
  maxBuchhoehe = 0;
  analyse = false;
}

function analyseBuecherregal(result) {
  buecherImport = result;
  buecherSortiert = buecherImport.slice(2);
  DekoFigur = parseInt(buecherImport[0], 10);
  maxBuchhoehe = findMax(buecherSortiert);
  analyse = true;
}

function loadBuecherregal() {
  reset_values();
  var importBuch = auswahl.value();
  importBuch = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1634054/bwinf2017_juna01_" + importBuch;
  loadStrings(importBuch, analyseBuecherregal);  
}

function setup() {
  createCanvas(900, 800);
  auswahl = createSelect();
  auswahl.position(225, 55);
  auswahl.option('keine Auswahl');
  auswahl.option('buecherregal1.txt');
  auswahl.option('buecherregal2.txt');
  auswahl.option('buecherregal3.txt');
  auswahl.option('buecherregal4.txt');
  auswahl.option('buecherregal5.txt');
  auswahl.option('buecherregal6.txt');
  auswahl.changed(loadBuecherregal);
}

function draw() {
  frameRate(20);
  background(240);
  
  textSize(20);
  var c = color(0);
  fill(c);    
  text("BWInf 2017, Junioraufgabe 01", x_offset, text_offset);
  textSize(16);
  text("Bitte Datei auswählen: ", x_offset, text_offset+30);
  
  if (analyse) {
      textSize(20);
      c = color(22, 148, 202);
      fill(c);    
      text(DekoFigur + ' Dekofiguren vorhanden.',x_offset,text_offset+60);
      for (var i = 0; i < buecherSortiert.length; i++){
        c = color(19, 131, 179);
        fill(c);    
        rect(x_offset+((width-x_offset*2)/buecherSortiert.length)*i,height/2,(width-x_offset*2)/buecherSortiert.length,-(buecherSortiert[i]/maxBuchhoehe)*(height/2-2*y_offset));    
      } 
      if (!sortiert) {
        bubbleSort(buecherSortiert);
      }
      if (sortiert == true && eingeraeumt == false) {
        RegalEinraeumen();
      }
      for (var j = 0; j < buecherRegal.length+1; j++){
        if (buecherRegal[j] == 0) {
          c = color('blue');
          fill(c);
          rect(x_offset+((width-x_offset*2)/buecherRegal.length)*j,height-y_offset,(width-x_offset*2)/buecherRegal.length,-(height/2-2*y_offset)); 
        }
        else {
          c = color('yellow');
          fill(c);
          rect(x_offset+((width-x_offset*2)/buecherRegal.length)*j,height-y_offset,(width-x_offset*2)/buecherRegal.length,-(buecherRegal[j]/maxBuchhoehe)*(height/2-2*y_offset));      
        }
      }
      if (eingeraeumt == true && RegalMoeglich == false) {
          c = color('red');
          fill(c);
          text('Notwendig wären ' + DekoFigurZaehler + ' Dekofiguren!',x_offset+250,text_offset+60);
      }
      if (eingeraeumt == true && RegalMoeglich == true) {
          c = color('green');
          fill(c);
          text('Die ' + DekoFigur + ' Dekofiguren reichen aus.',x_offset+250,text_offset+60);
      }
  }
}

function bubbleSort(a)
{
    var swapped;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a[i] > a[i+1]) {
                var temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                swapped = true;
            }
        }
        if (swapped == false) { sortiert = true } else { sortiert = false }
        return a;
    } while (swapped)
}

function findMax(a) {
  var max = 0;
  for (var i=0; i < a.length; i++) {
    if (a[i] >= max) {
      max = a[i];
    }
  }
  return max;
}

function RegalEinraeumen() {
  for (var i = 0; i < buecherSortiert.length-1; i++){
    var hoehendifferenz = buecherSortiert[i+1]-buecherSortiert[i];
    if (hoehendifferenz < 4) {
        buecherRegal.push(buecherSortiert[i]);
    }
    else {
        buecherRegal.push(buecherSortiert[i]);
        buecherRegal.push(0);
        DekoFigurZaehler = DekoFigurZaehler + 1;
    }
    if (i == buecherSortiert.length-2) {
      buecherRegal.push(buecherSortiert[i+1]);
    }
  }
  eingeraeumt = true;
  if (DekoFigurZaehler <= DekoFigur) { RegalMoeglich = true } else { RegalMoeglich = false }
}
              
            
!
999px

Console