If we want to use processing functions to draw complex objects like flowers, we'll want to get more comfortable using the processing transform functions: pushMatrix(), popMatrix(), translate( ), scale( ), and rotate( ).
When using the bezierVertex tool, it may be best to position your shape in the upper left corner so that the shape is defined in relation to the origin, the code below for the flower petals works best when the bezier shape is defined at the canvas origin point.
If we use the end points and control points to drag our shape to the origin, it'll be easier to use for creating our flower.
Thinking about drawing a flower
If we were to draw a flowering plant on paper, would we start with plant base or the flower petals? There's no correct approach, either way can work.
This is also true with programming, but we'll want to create a flexible program so we can draw all shapes and locations of the flower on the canvas.
Draw A Plant - Start with lines for a stem.
For code to draw a stem, we can use a line, we will want to reposition flowers, so the 2 line points must be function input parameters.
The function below draws 3 lines, with the middle line to hold the flower.
var drawFlower =function( baseX, baseY, stemX, stemY, stemAngle){//start by drawing lines from the base to the stem pointsstroke(41,133,57); //greenstrokeWeight(10);line(baseX,baseY, stemX-18, stemY+40);line(baseX,baseY, stemX+18, stemY+10);line(baseX,baseY, stemX, stemY);///move the origin to the stemX, stemY pointpushMatrix();translate( stemX, stemY);rotate(stemAngle); //placeholder - we might want to be able to rotate the flower before drawing itdrawPetal( ); //placeHolder until we get this code written//fill(255,0,0); //red to mark origin//ellipse(0,0,10,10); //mark the origin so we can see itpopMatrix();}
Draw a Flower - start with more stem lines
We can begin by drawing more stem lines - from the stem connection point to the flower's center point, then we'll translate the origin to the flower's center point.
var drawFlower =function( stemX, stemY, centerX, centerY, flowerAngle){//start by drawing a line from stemX,stemY to centerX, centerYstroke(41,133,57);strokeWeight(10);line(stemX,stemY,centerX,centerY); //draw stem section//move origin to flower centerpushMatrix();translate( centerX, centerY);rotate( flowerAngle);//place holder in case we want to rotate the flower///drawing petals code goes here - placeholdersdrawPetal( );drawPetal( );drawPetal( );//fill(255,0,0); //red to mark origin//ellipse(0,0,10,10); //mark the origin so we can see itpopMatrix();}
Draw Petal Logic
The code below takes a section of a bezierVertex shape and puts it inside a function so we can determine where we want to draw petals. If we add angle and scaleX, scaleY as input parameters to our function, then we can flip the flower petal.