CS1335 Java and Processing
  • CS 1335 Computer Science 1
  • Getting Started
    • Processing IDE
    • Java vs Javascript
    • Review: Processing, Functions
    • HSB Color Mode
      • HSB Color Wheel
        • Example Code
      • HSB Color Palette Tool
    • Recursion
      • Recursion Call-Stack
      • Example Code
        • Example Code Feb 5 S20
        • Feb 12 Code
  • Project 1
    • Subjective Modeling of Emotions
    • Emotions represented using color, form, space
      • Kandinsky Color - Emotion
      • Emotional Intelligence
    • Project 1: PShapes
      • Example Code
      • Inspiration
    • PShape with Cutout - Inner Contour
    • VertexShape - Recursion
    • Project 1: Recursive Drawing
    • Project 1: Programmatic Variations in Color
      • Recursion with rotate, scale
      • Plan Region Size, Color
    • Map Function
    • Transforms for Mirroring
    • Project1-Steps
  • Grid Based Designs
    • Computational Design
      • Generative Design
    • Artist: Victor Vasarely
    • Grid Pattern Design
    • 1D - Array of PShapes for Grid Layout
      • Truchet Tiling
      • Example Code
    • PShapes in Grid Regions
    • Grid Region Logic
    • Pattern Preview - Transforms: Translate & Scale
  • Project 2
    • Project 2 - 2D Arrays for Gradient Logic
      • 2D Array Grid with Labels
    • Grid Patterns using 2D Array Indexes: i, j
      • Example Class Code
    • lerpColor( ) and map( ) Functions
    • Demo Lerp Colors
    • 2D Arrays with lerpColor
    • Create PShape 2D Array
    • Function: Populate2DArray( )
    • Function: DisplayShapeMatrix()
    • Transforms for Position, Rotation, Scale of ShapeMatrix Elements
    • Project 2 - Steps
    • Animation for ShapeMatrix
      • Animation w/Noise
  • Object Oriented Programming
    • Introduction to Objects
    • OOP vs Data-Flow
    • Button States
    • Buttons as Objects
      • Button Class
    • Create Object Instances
    • Button Types
    • Modeling Buttons: States and Events
    • OOP - Inheritance
    • OOP - Polymorphism
    • Child-Class: PImageButton
    • PShape - SVG Objects
    • Menu of Buttons
    • ButtonGroup - Final Version
    • Slider Controller
    • UML Class Diagram
  • Project 3
    • Project 3 - Logic, Steps
    • Example Code S20
      • Code Wed Apr 1
      • Code Wed Apr 8 v1
      • Code Wed Apr 8 v2
      • Code Mon Apr 13
      • Code Wed Apr 15
      • Code Mon Apr 20
      • Code Wed Apr 22
      • Code Mon Apr 27
      • Code Wed Apr 29
    • Project 3 - Class Definitions
      • Button
      • PImageButton
      • ButtonGroup
      • Pattern
        • PShapes - SVG, Vertex Shapes
        • Setting Colors For Patterns
        • Pattern - With Child-PShapes
      • Slider
      • Particles
  • Java Syntax
    • Java Syntax
      • Typed-Variables
      • Float - Integer Conversion Errors
      • Modulus
      • Functions
      • Object Reference Data Types
      • Arrays
        • Class Example Code
      • Switch-Case Statement
      • Ternary Operator
      • Class
      • Learning Science
    • UML Class Diagram
    • Glossary
  • Resources and References
    • Resources
    • Random Inspiration
      • Ulm School
      • Heart-Mind, Mind, Body
      • Statistical Uncertainty
Powered by GitBook
On this page

Was this helpful?

  1. Project 1
  2. Project 1: Programmatic Variations in Color

Recursion with rotate, scale

PreviousProject 1: Programmatic Variations in ColorNextPlan Region Size, Color

Last updated 5 years ago

Was this helpful?

Using Rotate or Scale( ) within the Recursive Function allows for creating complex shapes using rotation and mirroring.

float maxLen, minLen;
color cNeg1, cNeg2, cNegPop1, cNegPop2;
color cPos1, cPos2, cPosPop1, cPosPop2;

void setup(){
size( 1000, 1000);
colorMode(HSB, 360, 100, 100, 100);
background(0);
maxLen = 50;
minLen=1;
cNeg1 = color( 285, 100,100); //purple
cNeg2 = color( 260, 100, 100); //blue
cPos1 = color( 185, 100,100); //aqua
cPos2 = color( 230, 100, 100);//blue
}

void draw(){
    if(mousePressed && frameCount %5 == 0){
    translate( mouseX, mouseY);
    float dist = dist( mouseX, mouseY, width/2, height/2);
    if( mouseX < width/2 ){
    //determine how color, size change across Negative region
        float curLen = map( mouseX, 0, width/2, maxLen, minLen);
        float lerpFraction = map( mouseX, 0, width/2, 0.0, 1.0);
        color curColor = lerpColor( cNeg1, cNeg2, lerpFraction);

        recursivePattern1( curLen, 5, curColor);

    }//end negative region
    else if( mouseX > width/2 ){ //positive region

    //create similar code for positive region

    } //end if positive region
    resetMatrix();
    } //end if mousePressed
} //end draw

void recursivePattern1(float len, float level, color c1){
if( level<1){
return;
}
color shapeColor = color( hue(c1), saturation(c1), brightness(c1)*.8 );
PShape s= createNegShape( len, c1);

shape( s, 0,0); //render shape to canvas

pushMatrix();
    scale( -1.0, 1.0); //mirror across y-axis
    shape( s,0, 0);//render shape to canvas
popMatrix();

pushMatrix();
    scale( 1.0, -1.0);//mirror across x-axis
    shape( s,0, 0);//render shape to canvas
popMatrix();

recursivePattern1( len * .8, level-1, shapeColor);

pushMatrix();
    scale( -1.0, -1.0); //mirror across x,y axis
    shape( s,0, 0); //rendered after recursive call, reversed stack order
popMatrix();
}

void recursivePattern2(float len, float level, color c1){
if( level<1){
return;
}
color shapeColor = color( hue(c1), saturation(c1), brightness(c1)*.8 );
PShape s= createPosShape( len, c1);
float degrees=0;
pushMatrix();
while( degrees< 360){
rotate( radians( degrees));
shape( s, 0,0);
degrees += 45;
}
popMatrix();
recursivePattern2( len * .8, level-1, shapeColor);
} //end recursivePattern2

PShape createNegShape(float len, color c1){
PShape s = createShape();
s.setFill( c1);
s.beginShape();
stroke( 30,40);
s.vertex(0,0 ); //1
s.vertex(.2*len,.1*len ); //2
s.vertex(.3*len,0 ); //3
s.vertex(.7*len,0 ); //4
s.vertex(.8*len,.1*len); //5
s.vertex(1.0*len,.1*len); //6
s.vertex(1.1*len,.2*len ); //7
s.vertex(1.0*len,.3*len ); //8
s.vertex(1.1*len,.4*len ); //9
s.vertex(1.1*len,.5*len); //10
s.vertex(1.2*len,.6*len); //11
s.vertex(1.2*len,.7*len); //12
s.vertex(1.1*len,.8*len); //13
s.vertex(.9*len,.7*len); //14
s.vertex(1.0*len,.9*len); //15
s.vertex(.9*len,1.0*len); //16
s.vertex(.8*len,.9*len); //17
s.vertex(.4*len,.9*len); //18
s.vertex(.3*len,1.0*len); //19
s.vertex(.1*len,.8*len); //20
s.vertex(.2*len,.7*len); //21
s.vertex(.2*len,.6*len); //22
s.vertex(.1*len,.5*len); //23
s.vertex(.1*len,.4*len); //24
s.vertex(0*len,.3*len); //25
s.vertex(.1*len,.2*len); //26
s.vertex(0*len,0*len); //back to first point
s.beginContour();
s.vertex(.3*len,.3*len ); //1
s.vertex(.3*len,.4*len); //15
s.vertex(.4*len,.5*len); //14
s.vertex(.4*len,.7*len); //13
s.vertex(.5*len,.8*len); //12
s.vertex(.7*len,.8*len); //11
s.vertex(.7*len,.7*len); //10
s.vertex(.8*len,.7*len ); //9
s.vertex(.9*len,.6*len ); //8
s.vertex(.8*len,.5*len ); //7
s.vertex(.9*len,.4*len); //6
s.vertex(.8*len,.3*len); //5
s.vertex(.7*len,.3*len ); //4
s.vertex(.6*len,.2*len ); //3
s.vertex(.4*len,.2*len ); //2
s.vertex(.3*len,.3*len ); //1
s.endContour();
s.endShape(CLOSE);
return s;
}

PShape createPosShape(float len, color c1){
PShape s = createShape();
s.setFill( c1);
s.beginShape();
//s.noStroke();
stroke( 30,20);
s.vertex(0,0 ); //1
s.vertex(.1*len,.1*len ); //2
s.vertex(.3*len,.2*len ); //3
s.vertex(.3*len,.3*len ); //4
s.vertex(.5*len,.4*len); //5
s.vertex(.5*len,.5*len); //6
s.vertex(.7*len,.6*len ); //7
s.vertex(.7*len,.7*len ); //8
s.vertex(.9*len,.8*len ); //9
s.vertex(1.0*len,1.0*len); //10
s.vertex(.8*len,.9*len); //11
s.vertex(.7*len,.8*len); //12
s.vertex(.6*len,.9*len); //13
s.vertex(.6*len,.7*len); //14
s.vertex(.5*len,.6*len); //15
s.vertex(.4*len,.6*len ); //16
s.vertex(.4*len,.7*len ); //17
s.vertex(.5*len,.8*len ); //18
s.vertex(.5*len,1.0*len ); //19
s.vertex(.4*len,.8*len); //20
s.vertex(.3*len,.7*len); //21
s.vertex(.3*len,.6*len ); //22
s.vertex(.4*len,.5*len ); //23
s.vertex(.3*len,.4*len ); //24
s.vertex(.2*len,.4*len); //25
s.vertex(.3*len,.5*len); //26
s.vertex(.2*len,.5*len); //27
s.vertex(.2*len,.7*len); //28
s.vertex(.1*len,.5*len); //29
s.vertex(.1*len,.4*len); //30
s.vertex(.2*len,.3*len); //31
s.vertex(.1*len,.4*len); //32
s.vertex(0,0 ); //1 //back to first point
s.endShape(CLOSE);
return s;
}