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

              
                <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

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

var rechteckWaldImport = [];
var rechteckWaldGroesse = [];
var rechteckX = 0;
var rechteckY = 0;
var anzahlVoegel = 0;
var flugverhaltenVoegelImport = [];
var flugverhaltenVoegel = [];
var vogelFlug = [];
var vogelFarbe = [];
var feldGroesse = 35;
var koordUrsprungY = 0;
var koordUrsprungX = 0;
var flugStart = false;
var flugFrame = 0;
var woIstFritzSicher = [];
var analyse = false;

var auswahl_wald;
var slider_frame;
var container_canvas;

function reset_values() {
  rechteckWaldImport = [];
  rechteckWaldGroesse = [];
  rechteckX = 0;
  rechteckY = 0;
  anzahlVoegel = 0;
  flugverhaltenVoegelImport = [];
  flugverhaltenVoegel = [];
  vogelFlug = [];
  vogelFarbe = [];
  feldGroesse = 35;
  koordUrsprungY = 0;
  koordUrsprungX = 0;
  flugStart = false;
  flugFrame = 0;
  woIstFritzSicher = [];
  analyse = false;
}

function analyseWald(result) {
  rechteckWaldImport = result;
  rechteckWaldGroesse = rechteckWaldImport[0].split(' ');
  rechteckX = parseInt(rechteckWaldGroesse[0],10);
  koordUrsprungX = 20;
  rechteckY = parseInt(rechteckWaldGroesse[1],10);
  koordUrsprungY = rechteckY * feldGroesse + 100;
  container_canvas.position(0, 0);
  
  var canvas_x_groesse = 0;
  var canvas_y_groesse = 0;
  if ((feldGroesse * rechteckX + 50) < 700) {
    canvas_x_groesse = 700;
  }
  else { canvas_x_groesse = feldGroesse * rechteckX + 50 }
  if ((feldGroesse * rechteckY + 150) < 300) {
    canvas_y_groesse = 300;
  }
  else { canvas_y_groesse = feldGroesse * rechteckY + 150 }
  container_canvas.size(canvas_x_groesse, canvas_y_groesse);
  
  anzahlVoegel = rechteckWaldImport[1];
  
  flugverhaltenVoegelImport = rechteckWaldImport.slice(2);
  for (var i = 0; i < flugverhaltenVoegelImport.length; i++){
    flugverhaltenVoegel.push(flugverhaltenVoegelImport[i].split(' '));
  } 
  for (var j = 0; j < flugverhaltenVoegel.length; j++){
    voegelFliegen(parseInt(flugverhaltenVoegel[j][0],10),
      parseInt(flugverhaltenVoegel[j][1],10),
      parseInt(flugverhaltenVoegel[j][2],10),
      flugverhaltenVoegel[j][3]);
      
      var r, g, b;
      r = random(255);
      g = random(255);
      b = random(255);
      vogelFarbe.push([r,g,b]);
  } 
  for (var r = 0; r < rechteckY; r++){
    woIstFritzSicher.push([0]);
    for (var s = 0; s < rechteckX-1; s++){
      woIstFritzSicher[r].push(0);
    }
  }
  analysiereFritz();
  analyse = true;
}

function loadWintervorrat() {
  reset_values();
  var importWald = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1634054/bwinf2017_juna02_";
  importWald = importWald + auswahl_wald.value();
  loadStrings(importWald, analyseWald);
}

function setup() {
  frameRate(40);
  container_canvas = createCanvas(700, 200);
  background(240);
  auswahl_wald = createSelect();
  auswahl_wald.position(205, 52);
  auswahl_wald.option('keine Auswahl');
  auswahl_wald.option('wintervorrat1.txt');
  auswahl_wald.option('wintervorrat2.txt');
  auswahl_wald.option('wintervorrat3.txt');
  auswahl_wald.option('wintervorrat4.txt');
  auswahl_wald.option('wintervorrat5.txt');
  auswahl_wald.option('wintervorrat6.txt');
  auswahl_wald.changed(loadWintervorrat);
  
  slider_frame = createSlider(0, 60, 30, 2);
  slider_frame.position(272, 80);
  slider_frame.style('width', '150px');
}
 
function draw() {
  frameRate(slider_frame.value());
  background(240);
  
  textSize(20);
  var c = color(0);
  fill(c);    
  text('BWInf 2017, Junioraufgabe 02-1', 30, 30);
  textSize(16);
  text('Bitte Datei auswählen: ', 30, 60);  
  text('Geschwindigkeit der Animation: ', 30, 90);    
  
  if (analyse) {
    textSize(15);
    var c = color(100);
    fill(c); 
    text('Daten zum Wald: ',460,30); 
    text('Waldbreite = ' + rechteckX,460,50); 
    text('Waldhöhe = ' + rechteckY,570,50); 
    text('Anzahl Vögel = ' + anzahlVoegel,460,70);
    text('Zeitpunkt (< 720s) = ' + flugFrame + 's',460,90);
    
    if (flugFrame < 720) {
      zeichneVogelFlug(flugFrame);
      flugFrame = flugFrame + 1;
    }
    if (flugFrame >= 720) {
      zeichneWald();
    }
  }
}

function analysiereFritz() {
  for (var j = 0; j < 720; j++) { 
    for (var w = 0; w < vogelFlug.length; w++) {
      if (vogelFlug[w][j][0] != -1) {
        var tempX = vogelFlug[w][j][0];
        var tempY = vogelFlug[w][j][1];
        woIstFritzSicher[tempY-1][tempX-1] = woIstFritzSicher[tempY-1][tempX-1] + 1;
      }
    }
  }
}

function voegelFliegen(xKoord, yKoord, startMin, flugRichtung) {
  var vogelflugAnalyse = [];
  var zaehler = 0;
  var zaehlerMax = 0;  
  var newKoord = -1;
  var deltaXY = 1;

  switch(flugRichtung) {
      case 'N':
              newKoord = yKoord;
              zaehlerMax = rechteckY;
              for (var minutenZaehler = 1; minutenZaehler < 721; minutenZaehler++) {
                if (minutenZaehler < startMin) {
                  vogelflugAnalyse.push([-1,-1]);
                }
                else {
                  vogelflugAnalyse.push([xKoord,newKoord]);
                  if (zaehler == zaehlerMax-1) {
                    deltaXY = deltaXY * (-1);
                    zaehler = 0;
                  } 
                  zaehler = zaehler + 1;
                  newKoord = newKoord + deltaXY;
                }
              }
              break;
      case 'O':
              newKoord = xKoord;
              zaehlerMax = rechteckX;
              for (var minutenZaehler = 1; minutenZaehler < 721; minutenZaehler++) {
                if (minutenZaehler < startMin) {
                  vogelflugAnalyse.push([-1,-1]);
                }
                else {
                  vogelflugAnalyse.push([newKoord,yKoord]);
                  if (zaehler == zaehlerMax-1) {
                    deltaXY = deltaXY * (-1);
                    zaehler = 0;
                  } 
                  zaehler = zaehler + 1;
                  newKoord = newKoord + deltaXY;
                }
              }
              break;
      case 'S':
              newKoord = yKoord;
              zaehlerMax = rechteckY;
              for (var minutenZaehler = 1; minutenZaehler < 721; minutenZaehler++) {
                if (minutenZaehler < startMin) {
                  vogelflugAnalyse.push([-1,-1]);
                }
                else {
                  vogelflugAnalyse.push([xKoord,newKoord]);
                  if (zaehler == zaehlerMax-1) {
                    deltaXY = deltaXY * (-1);
                    zaehler = 0;
                  } 
                  zaehler = zaehler + 1;
                  newKoord = newKoord - deltaXY;
                }
              }
              break;
      case 'W':
              newKoord = xKoord;
              zaehlerMax = rechteckX;
              for (var minutenZaehler = 1; minutenZaehler < 721; minutenZaehler++) {
                if (minutenZaehler < startMin) {
                  vogelflugAnalyse.push([-1,-1]);
                }
                else {
                  vogelflugAnalyse.push([newKoord,yKoord]);
                  if (zaehler == zaehlerMax-1) {
                    deltaXY = deltaXY * (-1);
                    zaehler = 0;
                  } 
                  zaehler = zaehler + 1;
                  newKoord = newKoord - deltaXY;
                }
              }
              break;
      default:
      vogelflugAnalyse.push([xKoord,yKoord]);
  }
  vogelFlug.push(vogelflugAnalyse);
}

function zeichneVogelFlug(zeitPunkt) {
  var vogelX = 0;
  var vogelY = 0;
  for (var i = 0; i < rechteckX; i++){
    for (var j = 0; j < rechteckY; j++){
      c = color('white');
      fill(c);    
      rect(20+feldGroesse*i,120+feldGroesse*j,feldGroesse,feldGroesse); 
    }
  }
  for (var jj = 0; jj < vogelFlug.length; jj++) {
    if (vogelFlug[jj][zeitPunkt][0] != -1) {
      vogelX = koordUrsprungX + (feldGroesse*vogelFlug[jj][zeitPunkt][0] - (feldGroesse/2));
      vogelY = koordUrsprungY+20 - (feldGroesse*vogelFlug[jj][zeitPunkt][1] - (feldGroesse/2));
      fill(vogelFarbe[jj][0], vogelFarbe[jj][1], vogelFarbe[jj][2]);
      ellipse(vogelX,vogelY,10,10);
    }
  }
}

function zeichneWald() {
  for (var i = 0; i < rechteckX; i++){
    for (var j = 0; j < rechteckY; j++){
      if (woIstFritzSicher[j][i] == 0) {
        c = color('white');  
      }
      else {
        c = color('red');  
      }
      fill(c);    
      var startYFeld = (120-feldGroesse)+feldGroesse*rechteckY;
      rect(20+feldGroesse*i,startYFeld-feldGroesse*j,feldGroesse,feldGroesse); 
    }
  }
}
              
            
!
999px

Console