45
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Processing学習】 3D空間の座標に点を打つ

Last updated at Posted at 2015-08-14

まずは、3D空間に立方体を描く。
この立方体はY軸に対して回転している。

void setup() {
  size(500, 500, P3D); //P3Dライブラリを使う
  background(0);
  noFill();
  stroke(255);  
}

void draw() {
  background(0);
  
  translate(width/2, height/2);
  rotateY(frameCount / 200.0);
  
  box(300);
}

sketch_150814b.png

このbox内の座標に点を打つ。全体のコードは以下。


final int AMOUNT = 100;

int[] x = new int[AMOUNT];
int[] y = new int[AMOUNT];
int[] z = new int[AMOUNT];

void setup() {
  size(500, 500, P3D);
  background(0);
  noFill();
  stroke(255);
  strokeWeight(2);
  
  for (int i = 0; i < AMOUNT; i++) {
    x[i] = int(random(-150, 150));
    y[i] = int(random(-150, 150));
    z[i] = int(random(-150, 150));
  }
  
}

void draw() {
  background(0);
  
  translate(width/2, height/2);
  rotateY(frameCount / 200.0);
  
  box(300);
  
  for (int i = 0; i < AMOUNT; i++) {
    point(x[i], y[i], z[i]);
  }
}

するとこうなる(画面キャプチャだとわかりにくい...orz)

sketch_150814a.png

ポイントはx軸、y軸、z軸それぞれの配列をつくり、ランダムな数値を格納する。

// setup
...

x[i] = int(random(-150, 150));
y[i] = int(random(-150, 150));
z[i] = int(random(-150, 150));

以下のように各配列から座標を取り出して、3D空間に点を描画する。

// draw
...

for (int i = 0; i < AMOUNT; i++) {
  point(x[i], y[i], z[i]);
}

-おまけ-

boxを消すとこんな感じ

sketch_150814a.png

回転しているので、点の描画の軌跡を描くとこんな感じ

sketch_150814a.png

おわり

45
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?