作者 主题: [ 作业讨论 ] DAY4 - OOP Dna & Falling Particles  (阅读 8175 次)

RavenKwok

  • Sr. Member
  • ****
  • 帖子: 277
  • Artist/ Animator/ Coder/ Cynical Asshole
[ 作业讨论 ] DAY4 - OOP Dna & Falling Particles
« 于: 十一月 08, 2011, 12:08:50 下午 »
两个作业的具体要求分别DAY4 pdf 第9页与第31页上。

第二个作业的难度较高,学有余力的童鞋可以研究着做一下。 8)

更新:第二个作业,俺做了一个Demo,给各位童鞋参考一下效果。 ;)

http://v.youku.com/v_show/id_XMzIwNjY1MjI4.html
« 最后编辑时间: 十一月 08, 2011, 11:52:07 下午 作者 RavenKwok »

钱 卓 韵

  • Newbie
  • *
  • 帖子: 7
  • 学习中 恩
Re: [ 作业讨论 ] DAY4 - OOP Dna & Falling Particles
« 回复 #1 于: 十一月 08, 2011, 09:52:17 下午 »
依葫芦画瓢,其实不甚明白 >:(

程序代码
Dna myDna;

void setup() {
  size(500, 500, P3D);
  myDna = new Dna();
}

void draw() {
  background(255);
  translate(width/2, height/2);
  rotateX(mouseY * 0.05);
  rotateY(mouseX * 0.05);
  myDna.display();
}

void DrawDna(int Y_) {
  pushMatrix();
  translate(-50, Y_, 0);
  sphereDetail(4);
  sphere(10);
  popMatrix();
  pushMatrix();
  translate(50, Y_, 0);
  sphereDetail(4);
  sphere(10);
  popMatrix();
  pushMatrix();
  rotateY(PI/2);
  translate(0, Y_, 0);
  box(3, 3, 100);
  popMatrix();
}

class Dna {
  int totalAmount;
  float theta;
  Dna() {
    totalAmount = 20;
    theta = 0.0;
  }
  void display() {
    theta += 0.01;
    rotateX(theta);
    rotateY(theta);
    for (int i=0;i<totalAmount;i++) {
      DrawDna(i*30-height/2);
      rotateY(10);
    }
  }
}



RavenKwok

  • Sr. Member
  • ****
  • 帖子: 277
  • Artist/ Animator/ Coder/ Cynical Asshole
Re: [ 作业讨论 ] DAY4 - OOP Dna & Falling Particles
« 回复 #2 于: 十一月 08, 2011, 10:59:24 下午 »
@ 钱 卓 韵

哎。。。一定是俺木有讲清楚,真是惭愧。 :-[

Anyway, DAY4 的课件帖下我会跟帖继续OOP 的浅易概念介绍。 ::)

calvinhuang

  • Newbie
  • *
  • 帖子: 8
Re: [ 作业讨论 ] DAY4 - OOP Dna & Falling Particles
« 回复 #3 于: 十一月 12, 2011, 10:39:42 上午 »
 :(
程序代码
DNA dna;

void setup() {
  size(600, 600, P3D);
  smooth();
  frameRate(10);
  dna= new DNA();
}
void draw() {
  background(255);
  lights();
  hint(ENABLE_OPENGL_4X_SMOOTH);
  translate(width/2, height/2);
  dna.update();
}
void drawhelix(int y) {
    pushMatrix();
    translate(-60, y);
    sphereDetail(5);
    sphere(9);
    popMatrix();
    pushMatrix();
    translate(60, y);
    sphereDetail(5);
    sphere(9);
    popMatrix();
    line(60, y, -60, y);
  }

class DNA {
  int totalAmount;
  int randiansY; 
  DNA() {
    //randiansY = 0;
    totalAmount = 20;
  }

  void update() {
    rotateY(mouseX/10);
    for (int i=0;i<totalAmount;i++) {
      drawhelix(i*30-height/2);
      rotateY(16);
      //randiansY ++;
    }
  }
}


RavenKwok

  • Sr. Member
  • ****
  • 帖子: 277
  • Artist/ Animator/ Coder/ Cynical Asshole
Re: [ 作业讨论 ] DAY4 - OOP Dna & Falling Particles
« 回复 #4 于: 十一月 12, 2011, 02:55:45 下午 »
@calvinhuang

效果差不多。但是代码的结构很混乱。几个需要修改的问题。

01. drawhelix() 依然是用来绘制dna 的函数,为何出现在DNA 类的外部。
02. update() 是用来更新位置数据的,为何里面会嵌有用来绘制的函数。
03. sphereDetail() 的程序的运行的过程中恒定为9,所以可以直接在setup() 中执行,且只需执行一次。
04. 目前dna 数目恒定为20,若需要修改个数必须更改DNA 类的内部,这样的话构造函数如同虚设,如何以从外部赋给他参数的形式来定义数目。

继续修改完善吧,少年。加勒个油。 ::)

Linyew

  • Newbie
  • *
  • 帖子: 7
Re: [ 作业讨论 ] DAY4 - OOP Dna & Falling Particles
« 回复 #5 于: 十一月 12, 2011, 08:34:59 下午 »
程序代码
DNA [] myDNA = new DNA[20];

void setup(){
  size(400, 600, P3D);
  smooth();
  for(int i=0;i<myDNA.length;i++){
    myDNA[i] = new DNA(13, 3);
  }
}

void draw(){
  background(0);
  lights();
  translate(width/2, height/2);
  rotateX(radians((height/2 - mouseY)*0.6));
  rotateY(radians((mouseX)));
  for (int i=0; i<myDNA.length;i++){
    myDNA[i].display(50, 30*i, 30*i);
    //myDNA[i].update();
  }
}

程序代码
class DNA {
  int sphereSize;
  int boxSize;
 
  DNA(int sSize, int bSize) {
    sphereSize = sSize;
    boxSize = bSize;
  }
 
  void display(float x_, float y_, float radian_) {
    pushMatrix();
    rotateY(radians(radian_));
    drawSphere(-x_, y_, color(255,0,0));//draw first sphere
    drawSphere(x_, y_, color(0,0,255));//draw second sphere
    drawBox(0, y_, x_*2, color(200,200,200));//draw box
    popMatrix();
  }
 
  void drawSphere(float transX, float a, color color_) {
    pushMatrix();
    translate(transX, -height/2+a);
    noStroke();
    fill(color_);
    sphereDetail(8);
    sphere(sphereSize);
    popMatrix();
  }
 
  void drawBox(float transX, float a, float length_, color color_) {
    pushMatrix();
    translate(transX, -height/2+a);
    noStroke();
    fill(color_);
    box(length_, boxSize, boxSize);
    popMatrix();
  }
 
}

RavenKwok

  • Sr. Member
  • ****
  • 帖子: 277
  • Artist/ Animator/ Coder/ Cynical Asshole
Re: [ 作业讨论 ] DAY4 - OOP Dna & Falling Particles
« 回复 #6 于: 十一月 13, 2011, 08:20:27 下午 »
@Linyew

Nice work. Try the falling particles.  8)

Tags: