วันจันทร์ที่ 1 กรกฎาคม พ.ศ. 2556

Bridge

void setup() {
  size(400, 300);
  smooth();
  background(255);
  drawSlings(100, 300, 100);
}

void drawSlings(int t1, int t2, int y) {
  int count = 0;
  int s = 10;
  int n = 20;
  int x1 = 0;
  int x2 = 200;
  while(count<n) {
    line(x1, 200, t1, y);
    x1 = x1 + s;
    line(x2, 200, t2, y);
    x2 = x2 + s;
    count = count + 1;
  }

  strokeWeight(10);
  line(0, height/1.5, width, height/1.5);
  line(100, 200, 100, 300);
  line(300, 200, 300, 300);
}


void setup() {
  size(400, 300);
  smooth();
  background(255);
  drawSlings(100, 300, 100);
}

void drawSlings(int t1, int t2, int y) {
  int count = 0;
  int s = 10;
  int n = 20;
  int x1 = 0;
  int x2 = 200;
  while(count<n) {
    line(x1, 200, t1, y);
    x1 = x1 + s;
    line(x2, 200, t2, y);
    x2 = x2 + s;
    count = count + 1;
  }

  strokeWeight(10);
  line(0, height/1.5, width, height/1.5);
  line(100, 200, 100, 300);
  line(300, 200, 300, 300);
}

    เป็นการใช้คำสั่ง while loop  วาดรูปซ้ำๆกัน โดยการกำหนดเงื่อนไขคือ เมื่อ count มีค่า น้อยกว่า ค่า n แล้ว ซึ่งตอนแรก เรากำหนดค่าของ count เป็น 0 ดังนั้นเป็นจริงตามเงื่อนไข จึงสั่งให้วาดเส้น 1 เส้น แล้วให้ x1 = x1 + s เป็นการเพิ่นค่าของ x1 ให้เท่ากับ x1 + s แล้ววาดเส้นต่ออีก 1 เส้น แล้วให้ x2 = x2 + s เป็นการเพิ่นค่าของ x2 ให้เท่ากับ x2 + s แล้วให้ count = count + 1 เป็นการเพิ่นค่าของ count เพิ่มขึ้นอีก 1 แล้วกลับไปเช็คเงื่อนไขต่ออีกรอบ แล้วถ้าเงื่อนไขยังเป็นจริงอยู่ ก็จะวนขึ้นมาทำแบบนี้ไปเรื่อย จนกว่าเงื่อนไขจะเป็นเท็จ หรือก็คือ count มีค่ามากกว่า หรือเท่ากับค่าของ n จึงออกจาก loop แล้วก็คำสั่งต่อไปจนจบ

ไม่มีความคิดเห็น:

แสดงความคิดเห็น