본문 바로가기

프로세싱

프로세싱 함수 ch8~10

반응형

1.

random(3);

3이하의 float 정수

 

2.

(int) random() == int (random());

형변환과 관련있다. 형변환 하는 방법 두가지

 

3.

randomSeed(숫자);

원래 random() 메소드는 완벽한 랜덤이 아니다. 미리 정해져있는 테이블에서 순차적으로 숫자를 출력해주기 때문인데

그 테이블을 바꿔주는 메소드가 randomSeed메소드이다.

 

4.

float calculateMars(float w)

{

  float newWeight = w * 0.38;
  return newWeight;
}

메소드를 선언하는 방법이다.

returen 으로 반환값을 설정함을 알아두자.

 

5.

// Example 09-01 from "Getting Started with Processing"
// by Reas & Fry. O'Reilly / Make 2010

JitterBug bug; // Declare object

void setup() {
  size(480, 120);
  smooth();
  // Create object and pass in parameters
  bug = new JitterBug(width/2, height/2, 20);
}

void draw() {
  bug.move();
  bug.display();
}

class JitterBug {
  float x;
  float y;
  int diameter;
  float speed = 2.5;
  
  JitterBug(float tempX, float tempY, int tempDiameter) {
    x = tempX;
    y = tempY;
    diameter = tempDiameter;
  }
  
  void move() {
    x += random(-speed, speed);
    y += random(-speed, speed);
  }
  
  void display() {
    ellipse(x, y, diameter, diameter);
  }
}

 

 

클래스를 선언하는 방법이다.

클래스는 객체(object)의 설계도라고 보면 된다.

평소에는 클래스라는 설계도상으로만 존재하다가

JitterBug bug; 로 객체를 선언하고

setup()

{

bug = new JitterBug()

}

으로 객체에 클래스를 배정한다면 비로소 객체가 메모리상에 존재하게 되는 것이다.

클래스 안의 메소드에 접근하는방법은 bug.move();와 같은방법으로 c언어에서 구조체 내부의 변수에 접근하는방법과 유사하다.

 

6.

float[] x = new float[3000];

배열을 선언하는 방법이다.

int[] x = {12,2};

이런식으로도 가능

참고로

float[] x = new float[3000];

을 하고

x[0] = 3;

이라고 하면 x배열의 첫번째에 3을 넣어줌을 의미한다. 배열의 첫번째는 0이란것을 알아두면 좋다.

반응형