[Processing × Java] How to use the function

This article is for understanding the structure of the program through Processing. This time I will write about functions.

ezgif.com-gif-maker (4).gif

table of contents 0. How to use the function

  1. Pre-prepared functions
  2. Functions that you define and create
  3. Example of function modularization → reuse

0. What is a function?

0-0. ** What is a function **

A function is like an output device. It takes something, converts it and outputs it.

0-1. ** Why use functions **

The main reason is ① ** Because the program becomes easier to read. ** ** ② ** Because it can be reused. ** ** (Details on reuse later)

0-2. ** Function type **

There are two types of functions: "** pre-prepared functions " and " self-defined functions **".

The functions provided in advance are, specifically, size (), background (), line (), void setup (), void draw () ... etc.

1. ** Pre-prepared functions **

In Processing, ** many functions are prepared in advance **. size (), background (), rect (), ellipse () ... etc. For example, in the size () function, size (600,400); will display a window with a width of 600px and a height of 400px.

The important thing here is that the pre-made functions must be used according to ** the rules **.

For example, size (600,400,300,200); will result in an error. You can find the ** set rules ** by looking at the Processing Reference.

(Example) Fixed rule of size ()

スクリーンショット 2020-07-06 11.23.34 1.png

2. ** Functions that you define and create **

Programming allows you to create your own original functions to simplify your program. First, we will simplify the program that displays the following circles.

maru.java



void setup(){
  size(200,200);
  background(255);
}

void draw(){
  strokeWeight(3);
  stroke(77,196,224);
  noFill();
  ellipse(100,100,30,30);
}

olympic.png

To simplify the program ① ** Modularize ** ② ** Make it reusable ** I will do that.

① Modularize

maru_module.java



void setup(){
  size(200,200);
  background(255);
}

//You can see at a glance what to repeat!
void draw(){
  //Execute the original function you created.
  maru();
}

//Summarized here! !!
void maru(){
  strokeWeight(3);
  stroke(77,196,224);
  noFill();
  ellipse(100,100,30,30);
}

Point : By combining ** into one function **, the draw () function will be cleaner and the program will be easier to understand.

② Make it reusable

Reusable means that ** multiple outputs are possible **.

maru_reuse.java


void setup(){
  size(200,200);
  background(255);
}

void draw(){
  //Use your own defined function
  //You can easily display multiple circles!
  //maru(x coordinate,y coordinate,The size of the circle);
  maru(100,80,100);
  maru(100,100,50);
}

//Function definition
//First decide what to take as an argument
void maru(float x,float y,float s){
  strokeWeight(3);
  stroke(77,196,224);
  noFill();
  //It says what to use the argument value for
  ellipse(x,y,s,s);
}

olympic01.png

Point : To make it reusable, allow the value of the argument (parameter) to be determined. That way, you can change the output value of the function.

for that purpose (1) Decide which value you want to change and express it as a variable.

  ellipse(x,y,s,s);

(2) Define the argument in the form of ** data type variable **.

void maru(float x,float y,float s){
}

③ Use the function according to the rules of the function you defined.

  maru(100,80,100);

□ ** What is void **

The data received as the result of executing the function is called ** return value **.

Functions basically return values such as int and float. Example of a function that returns a float type number ↓

float_function.java


void setup(){
  //Self-defined average()Use a function to assign the mean value to the variable f.
  float f = average(12.0,6.0);
  //Display the value of f.
  println(f);
}

//A function that can find the average of two values average()To define.
float average(float num1,float num2){
  //Create an expression so that the mean value of two values can be assigned to the variable av.
  float av = (num1 + num2) / 2.0;
  //Returns a float type number as the return value
  return av;
}
9.0

However, functions that draw shapes, such as ellipse (), do not return a value. Use void in this way when the function ** does not return a return value **.

Point : Functions that do not return a return value are usually used by themselves. (That is, void is not written) background (), fill (), rect () .... etc. Add void to the function ** that determines the processing content using ** {}. void setup () {}, void draw () {} ... etc.

3. Example of function modularization → reuse

◯ Program to draw cherry blossom petals

sakura.java


int x = 300;
int y = 300;
//Define a variable r that determines the speed of rotation.
float r = 30.0;
//Define the variable a that determines the transparency of the cherry blossoms.
int a = 150;

void setup(){
size(600,600);
}

void draw(){
  background(255);
  //The depictions up to popMatrix are output together.
  pushMatrix();
  //Coordinate reference point(origin)To(x,y)To move to
  translate(x,y);
  //Use the variable r to determine how much to rotate.
  rotate(frameCount/r);
  //Depiction of cherry blossoms
  for(int i = 0;i < 5;i++){
    rotate(TWO_PI * 1/5);
    noStroke();
    fill(255,50,80,a);
    stroke(225,50,80,100);
    bezier(0,0,35,-20,35,-52,8,-80);
    bezier(0,0,-35,-20,-35,-52,-8,-80);
    stroke(225,50,80,30);
    triangle(0,0,8,-80,0,-75);
    triangle(0,0,-8,-80,0,-75);
  }
  fill(200,50,100,200);
  ellipse(0,0,15,15);
  //The depiction of cherry blossoms is output together.
  popMatrix();
}

ezgif.com-gif-maker (3).gif

◯ Program to draw cherry blossom petals (modularize)

sakura_module.java


int x = 300;
int y = 300;
float r = 30.0;
int a = 150;

void setup(){
size(600,600);
}

//draw()The inside of the function is cleaner and easier to see.
void draw(){
  background(255);
  //Use your own defined function!
  sakura();
}

//Define a function for cherry blossom petals.
void sakura(){
  pushMatrix();
  translate(x,y);
  rotate(frameCount/r);
  for(int i = 0;i < 5;i++){
    rotate(TWO_PI * 1/5);
    noStroke();
    fill(255,50,80,a);
    stroke(225,50,80,100);
    bezier(0,0,35,-20,35,-52,8,-80);
    bezier(0,0,-35,-20,-35,-52,-8,-80);
    stroke(225,50,80,30);
    triangle(0,0,8,-80,0,-75);
    triangle(0,0,-8,-80,0,-75);
  }
  fill(200,50,100,200);
  ellipse(0,0,15,15);
  popMatrix();
}

◯ Program to draw cherry blossom petals (make it reusable)

sakura_reuse.java


void setup(){
size(600,600);
}

void draw(){
  background(255);
  //Output 3 functions with different parameters
  sakura(400,200,30,150);
  sakura(100,300,-40,100);
  sakura(380,400,60,50);
}
//Define the original function
//Arguments are defined in the form of data types and variables.
void sakura(float x,float y,float r,float a){
  
  pushMatrix();
  translate(x,y);
  rotate(frameCount/r);
  for(int i = 0;i < 5;i++){
    rotate(TWO_PI * 1/5);
    noStroke();
    fill(255,50,80,a);
    stroke(225,50,80,100);
    bezier(0,0,35,-20,35,-52,8,-80);
    bezier(0,0,-35,-20,-35,-52,-8,-80);
    stroke(225,50,80,30);
    triangle(0,0,8,-80,0,-75);
    triangle(0,0,-8,-80,0,-75);
  }
  fill(200,50,100,200);
  ellipse(0,0,15,15);
  popMatrix();
}

ezgif.com-gif-maker (4).gif

Finally

Thank you for reading. We appreciate your opinions and suggestions in order to make the article even better.

Recommended Posts

[Processing × Java] How to use the function
[Java] How to use the hasNext function
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use variables
[Processing × Java] How to use arrays
[Java] How to use the File class
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Java] How to use the Calendar class
[Java] How to use Map
[Java] How to use Map
[Java] How to use Thread.sleep to pause the program
How to use java Optional
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use the replace () method (Java Silver)
[Java] How to use Optional ①
How to use the link_to method
How to use Java HttpClient (Get)
How to use the include? method
How to use the form_with method
How to use Java HttpClient (Post)
How to use the wrapper class
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to add the delete function
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
[Processing × Java] How to use loop 2 --- Nested structure, coordinate conversion
How to use submit method (Java Silver)
[Rails] How to use the map method
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to get the current directory
[Swift] How to implement the countdown function
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to install the legacy version [Java]
How to use Java Scanner class (Note)
How to get the date in java
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
To use the "java" command line tool ... How to avoid popping up
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
How to use MinIO with the same function as S3 Use docker-compose
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging