# 請教各位大大教導如何寫顏色變數

**URL:** <https://discourse.processing.org/t/topic/4519>\
**Category:** Coding Questions\
**Created:** [October 16, 2018, 4:43am UTC](https://discourse.processing.org/t/topic/4519 "2018-10-16T04:43:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alk5603](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@alk5603](https://discourse.processing.org/u/alk5603)\
**Post date:** [October 16, 2018, 4:43am UTC](https://discourse.processing.org/t/topic/4519/1 "2018-10-16T04:43:44Z")

</div>

![%E7%90%83%E9%A1%9E](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/062ed7f2aa935bf9d7f158ca97bb347c2c9ef239.jpeg)

各位大大好  
小弟想請教一下,關於球類的問題 只要球有連一條線就變色 二條線又變另一種顏色 三條線又變一種色  
那我的代碼要怎麼打比較好 請各位大大指教

## 1.一條線變藍 2.二條線變紅 3.三條線變黃

```auto
int num = 50, radius = 50;
int[] posX, posY, speedX, speedY;

void setup() {
  size(800, 600);
  stroke(255);
  strokeWeight(15);

  posX = new int[num];
  posY = new int[num];
  speedX = new int[num];
  speedY = new int[num];

  for (int i=0; i<num; i++) {
    posX[i] = int(random(width));
    posY[i] = int(random(height));
    speedX[i] = int(random(-5, 5));
    speedY[i] = int(random(-5, 5));
  }
}

void draw() {
  background(0);
  for (int i=0; i<num; i++) {
    ball(i);
  }
}

void ball(int i) {
  posX[i] = posX[i] + speedX[i];
  posY[i] = posY[i] + speedY[i];

  if (posX[i] > width) {
    speedX[i] = -1 * speedX[i];
  } else if (posX[i] < 0) {
    speedX[i] = -1 * speedX[i];
  }

  if (posY[i] > height) {
    speedY[i] = -1 * speedY[i];
  } else if (posY[i] < 0) {
    speedY[i] = -1 * speedY[i];
  }
  point(posX[i], posY[i]);

  for (int j = 0; j < num; j++) {
    if(dist(posX[i], posY[i], posX[j], posY[j]) < radius) {
      strokeWeight(1);
      line(posX[i], posY[i], posX[j], posY[j]);
      strokeWeight(15);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 16, 2018, 5:38am UTC](https://discourse.processing.org/t/topic/4519/2 "2018-10-16T05:38:40Z")

</div>

translate with google:

> Ask you how to write color variables
> 
> Great, everyone.  
> The younger brother wants to ask questions about the ball. As long as the ball has a line, it will change color. The two lines will change to another color. The three lines will change color.  
> Then how should my code be better? Please give me a big advice.
> 
> 1. One line turns blue
> 2. Two lines turn red
> 3. Three lines turn yellow

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 16, 2018, 6:57am UTC](https://discourse.processing.org/t/topic/4519/3 "2018-10-16T06:57:56Z")

</div>

```auto
int num = 50, radius = 50;

Ball[] balls = new Ball[num];

void setup() {
  size(800, 600);
  for (int i=0; i<num; balls[i++] = new Ball() );
  strokeWeight(2);
}

void draw() {
  background(0);
  stroke(255);
  for ( int i = 0; i < num; i++) { 
    for (int j = i+1; j < num; j++) {
      if (dist(balls[i].px, balls[i].py, balls[j].px, balls[j].py) < radius) {
        line(balls[i].px, balls[i].py, balls[j].px, balls[j].py);
        balls[i].count++;
        balls[j].count++;
      }
    }
  }
  for (int i=0; i<num; balls[i++].draw() );
}

class Ball {
  float px, py, vx, vy;
  int count = 0;
  Ball() {
    px = int(random(width));
    py = int(random(height));
    vx = int(random(-5, 5));
    vy = int(random(-5, 5));
  }
  void draw() {
    px+=vx;
    py+=vy;
    vx*=(px<0||px>width)?-1:1;
    vy*=(py<0||py>height)?-1:1;
    noStroke();
    fill(255);
    if( count == 1 ){
      fill(0,0,255);
    } else if( count == 2 ){
      fill(255,0,0);
    } else if( count == 3 ){
      fill(255,255,0);
    }
    ellipse(px, py, 15, 15);
    count = 0;
  }
}

```

---

<div class="post-metadata">

**Author:** ![alk5603](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@alk5603](https://discourse.processing.org/u/alk5603)\
**Post date:** [October 16, 2018, 7:12am UTC](https://discourse.processing.org/t/topic/4519/4 "2018-10-16T07:12:36Z")

</div>

感謝大大解答，萬分感激！！！！！！😁😁

> Google Translate: Thank you for your great answer, thank you very much.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 16, 2018, 7:14am UTC](https://discourse.processing.org/t/topic/4519/5 "2018-10-16T07:14:46Z")

</div>

I don’t think I need Google translate to tell me that that was the answer he wanted. 🕶
