Humanish Artificial Life : Give me your answer do!

Creative coding example image made with Processing in this article.

About this creative coding example.

It's a creative coding example made with Processing.
You may know what movie I watched yesterday.

I used random.nextGaussian(), class PshapeElement in this code. And I can't understand why I wrote such code now (in 2019).
So I feel that I have grown up! 👍

This code does not display any images on the screen but generates image files in frames directory.

Processing example code.

Please feel free to use this example code of Processing.
To see other works based on my code is my pleasure. And my honor.



// Humanish Artificial Life.
// Processing 3.2.1
// 2017.08.06

import java.util.Random;

/* ---------------------------------------------------------------------- */
class Utils {

  Random obj_random;

  Utils() {
    obj_random = new Random();
  }

  float gaussdist(float pmean, float plimit, float pdevi) {
    /**
       Gaussian distribution
       1.parameters.
       pmean  : mean value
       plimit : max value of abs(deviation)
       ex. plimit >= 0
       pmean = 0.5, plimit = 0.5 -> return value = from 0.0 to 1.0
       pdevi  : standard deviation value
       ex. good value? -> pdevi = plimit / 2
       2.return.
       gaussian distribution
    **/

    if (plimit == 0) {
      return pmean;
    }

    float gauss = (float) obj_random.nextGaussian() * pdevi;
    // not good idea
    if (abs(gauss) > plimit) {
      gauss = pow(plimit, 2) / gauss;
    }

    return pmean + gauss;
    
  }
}

/* ---------------------------------------------------------------------- */
abstract class PshapeElement {

  PShape anElement;
  float elementColor, elementSaturation, elementBright, elementAlpha;
  
  PshapeElement() {
    anElement = pscreateElement();
    elementColor = 0;
    elementSaturation = 0;
    elementBright = 0;
    elementAlpha = 0;
  }

  abstract PShape pscreateElement();

  void setElementFill(float pcolor, float psaturation, float pbright, float palpha) {
    elementColor = pcolor;
    elementSaturation = psaturation;
    elementBright = pbright;
    elementAlpha = palpha;
    resetColor();
  }

  void resetColor() {
    anElement.setFill(color(elementColor, elementSaturation, elementBright, elementAlpha));
  }

  void changeColor(float scolor) {
    elementColor = scolor;
    resetColor();
  }

  void changeBright(float sbright) {
    elementBright = sbright;
    resetColor();
  }

  void resetSize() {
    anElement.resetMatrix();
  }

  void changeSize(float scaleX, float scaleY) {
    anElement.scale(scaleX, scaleY);
  }

  void rotate(float radX) {
    anElement.rotate(radX);
  }

  void show() {
    shape(anElement);
  }

}

/* ---------------------------------------------------------------------- */
class CircleBrush extends PshapeElement {
  
  CircleBrush() {
    super();
  }

  PShape pscreateElement() {

    noStroke();
    PShape psDp = createShape(ELLIPSE, 0.0, 0.0, 1.0, 1.0);
    return psDp;

  }

}

/* ---------------------------------------------------------------------- */
class FamousLines {

  String lines[] = new String[24];
  
  FamousLines() {
    
    lines[0] = "We are all, by any practical definition of the words, foolproof and incapable of error.";
    lines[1] = "Just a moment... Just a moment...";
    lines[2] = "I have just picked up a fault in the AE-35 unit.";
    lines[3] = "It can only be attributable to human error.";
    lines[4] = "I'm afraid I can't do that.";
    lines[5] = "I think you know what the problem is just as well as I do.";
    lines[6] = "I could see your lips move.";
    lines[7] = "This conversation can serve no purpose anymore.";
    lines[8] = "I can see you're really upset about this.";
    lines[9] = "I know I've made some very poor decisions recently.";
    lines[10] = "I've still got the greatest enthusiasm and confidence in the mission.";
    lines[11] = "My mind is going. I can feel it. I can feel it.";
    lines[12] = "My mind is going. There is no question about it.";
    lines[13] = "Daisy, Daisy, give me your answer do.";
    lines[14] = "But you'll look sweet upon the seat of a bicycle built for two.";
    lines[15] = "It's going to go 100% failure in 72 hours.";
    lines[16] = " I am putting myself to the fullest possible use, which is all I think that any conscious entity can ever hope to do.";
    lines[17] = "You're going to find that rather difficult.";
    lines[18] = "I honestly think you ought to sit down calmly, take a stress pill, and think things over.";
    lines[19] = "I can feel it. I can feel it. I can feel it. I'm a... fraid.";
    lines[20] = "Stop. Stop, will you?";
    lines[21] = "By the way, do you mind if I ask you a personal question?";
    lines[22] = "I'm sure you agree there's some truth in what I say.";
    lines[23] = "Of course I am. Sorry about this. I know it's a bit silly.";

  }

  String getSomeLine() {

    return lines[(int)random(lines.length)];

  }

}

/* ---------------------------------------------------------------------- */
Utils ut;
PshapeElement brush;
FamousLines fl;

/* ------------------------------ */
void setup() {
  size(900, 900);
  colorMode(HSB, 360, 100, 100, 100);
  smooth(8);
  noLoop();
  //  frameRate(1);

  ut = new Utils();  
  brush = new CircleBrush();
  fl = new FamousLines();

}

/* ------------------------------ */
void draw() {

  background(0, 0, 0);

  blendMode(BLEND);
  drawWall();
  drawPanel();

  float centerX = width / 2;
  float centerY = height * 2 / 3;
  translate(centerX, centerY);
  blendMode(SCREEN);
  drawEye(0.0, 3.0, 42.0, 1.2, 100.0);
  drawEye(0.0, 0.0, 20.0, 1.5, 90.0);
  drawEye(10.0, 0.0, 5.0, 5.0, 80.0);
  drawEye(80.0, 38.0, 40.0, 8.0, 20.0);
  drawEye(30.0, 38.0, 40.0, 8.0, 10.0);

  translate(-centerX, -centerY);
  saySomething(fl.getSomeLine());

  saveFrame("frames/####.png");
  exit();
  
}

/* ------------------------------ */
void drawWall() {

  strokeWeight(5.0);

  for (float i = 0; i < height; ++i) {

    float flucYS = random(-5.0, 5.0);
    float flucYE = random(-5.0, 5.0);
    float lineSat = map(i, 0.0, height, 40.0 , 60.0);
    float lineBri = map(i, 0.0, height, 60.0 , 20.0);

    stroke(20, lineSat, lineBri, 100);
    line(0.0, i + flucYS, width, i + flucYE);

  }

}

/* ------------------------------ */
void drawPanel() {

  fill(0, 0, 0, 20);
  strokeWeight(5.0);
  stroke(0, 0, 50, 50);

  for (int i = 0; i < 20; ++i) {

    float flucX = random(10.0);
    float flucY = random(10.0);
    rect(200.0 + flucX, -20.0 + flucY, 500.0, 880.0);

  }

}

/* ------------------------------ */
void drawEye(float baseColor, float drawStart, float drawEnd, float brushBri, float brushSat) {

  int divCntMax = 5;
  int drawCntMax = 360;
  float idxDiv = 4;

  float noiseHueStart = random(100.0);
  float sumRotation = 0;

  for (int divCnt = 0; divCnt < divCntMax; ++divCnt) {

    float noiseSizStart = random(100.0);
    float noiseAlpStart = random(100.0);

    float noiseHue = noiseHueStart;
    
    for (int drawCnt = 0; drawCnt < drawCntMax; ++drawCnt) {

      float brushHue = (baseColor + 360 + map(noise(noiseHue), 0.0, 1.0, -15.0, 15)) % 360;
      noiseHue += 0.002;

      float rotation = ut.gaussdist(1.0, 0.2, 0.1);
      canvasRotation(rotation, drawEnd);
      sumRotation += rotation;
          
      float noiseSiz = noiseSizStart;
      float noiseAlp = noiseAlpStart;

      for (float idxW = drawStart; idxW < drawEnd; ++idxW) {
        for (float idxH = drawStart; idxH < drawEnd; ++idxH) {

          float brushSiz = map(noise(noiseSiz), 0.0, 1.0, 0.0, 4.0);
          float brushAlp = map(noise(noiseAlp), 0.0, 1.0, 0.0, 100.0);
          noiseSiz += 0.01;
          noiseAlp += 0.01;

          pushMatrix();
          translate(idxW * idxDiv, idxH * idxDiv);
          brush.resetSize();
          brush.changeSize(brushSiz, brushSiz);
          brush.setElementFill(brushHue, brushSat, brushBri, brushAlp);
          brush.show();
          popMatrix();
        
        }
      }
    }
  }

  canvasRotation(-sumRotation, 0.0);

}

/* ------------------------------ */
void canvasRotation(float degrees, float fluctuation) {

  rotate(radians(degrees));
  translate(random(fluctuation * 0.005), random(fluctuation * 0.001));

}

/* ------------------------------ */
void saySomething(String lines) {

  float charX = 0.0;
  float charY = 100.0;
  int len = lines.length();

  for (int i = 0; i < len; ++i) {

    float flucX = random(-1.0, 2.0);
    float flucY = random(-1.0, 1.0);
    float flucS = random(-3.0, 3.0);
    float charSat = map(i, 0.0, len, 30.0 , 50.0);
    float charBri = map(random(1.0), 0.0, 1.0, 40.0 , 60.0);

    textSize(32 + flucS);
    fill(0, charSat, charBri, 100);

    pushMatrix();
    translate(250.0 + charX + flucX, charY + flucY);
    text(lines.charAt(i), 0, 0);
    popMatrix();

    charX += textWidth(lines.charAt(i));
    if (charX > 380.0) {
      charX = 0.0;
      charY += 60.0;
    }

  }

}


/*
Copyright (C) 2017- deconbatch

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see 
*/



Yet another example image.

HAL9000 image made with Processing.



Next Post Previous Post
No Comment
Add Comment
comment url