最短距離追いかけっこ Shortest distance tracking

赤と青のボールの追いかけっこを最短距離でするソフトです。 degの数字を変えることで、追いかけるルートの角度が変わります。38行目、 degの数字をいじって、遊んでみてください。今は、180÷4=45度になっています。

deg = 180/4のところを、deg = 90にすれば、90度(直角)になります。

It is soft which keeps chase of red and blue ball at the shortest distance.
By changing the number of degrees, the angle of the route chasing changes. Please line 38, deg with numbers and play around. Now it is 180 ÷ 4 = 45 degrees.

If deg = 180/4 is set to deg = 90, it becomes 90 degrees (right angle).

Download zip file here ↓

ProcessingのZipダウンロードは、こちら

ソースコードです。↓Source code is here

PFont font;
PGraphics bg;
void setup() {
  size(800, 600, P2D);
  surface.setLocation(int( (displayWidth  - width)/1.3), 
    (displayHeight - height)/2 );   
  background(220);
  font = loadFont("font.vlw");
  textFont(font, 32);
  noSmooth();
  noStroke();
  gx = width/2;
  gy = height/2;
  sx = width/2;
  sy = height/2;
  bg = createGraphics(width, height);
  x = sx;
  y = sy;
}

int[] tx = new int[50];
int[] ty = new int[50];

float x;
float y;
float s;
float ss;
float sx;
float sy;
float yx;
float yy;
float ys;
float gx;
float gy;
int j;
int jj;
<strong>float deg = 180/4;</strong>
void draw() {
  background(255); 
  yx = int(x/2)*2;
  yy = int(y/2)*2;
  for (int i = 0; i &lt; 1000; i++) {
    fill(128);
    rect(int(yx-1), int(yy-1), 2, 2);
    yx += sin(ys+PI)*4;
    yy += cos(ys+PI)*4;
    //if(red(get(int(sin(ys+PI+(PI/8))*20+yx),int(cos(ys+PI+(PI/8))*20+yy))) &lt; 10){
    ys = atan2(yx-gx, yy-gy);
    ys = radians(int(degrees(ys)/deg)*deg);
    //}
    //rect(int(sin(ys+PI+(PI/2))*20),int(cos(ys+PI+(PI/2))*20),1,1);
    if (int(gx) &lt; int(yx)+16 &amp;&amp; int(gx) &gt; int(yx)-16) {
      if (int(gy) &lt; int(yy)+16 &amp;&amp; int(gy) &gt; int(yy)-16) {
        i = 10000;
        //println("おわり");
      }
    }
  }
  fill(255, 0, 0);
  ellipse(x, y, 20, 20);
  x += sin(s+PI)*2;
  y += cos(s+PI)*2;
  fill(0, 0, 255);
  ellipse(gx, gy, 20, 20);
  s = atan2(x-gx, y-gy);
  s = radians(int(degrees(s)/deg)*deg);
  j = 0;
  if (int(gx) &lt; int(x)+7 &amp;&amp; int(gx) &gt; int(x)-7) {
    if (int(gy) &lt; int(y)+7 &amp;&amp; int(gy) &gt; int(y)-7) {
      x = sx;
      y = sy;
    }
  }

  if (!mousePressed) {
    gx = mouseX;
    gy = mouseY;
    if (mouseX != pmouseY || mouseY != pmouseY) {
      //x = sx;
      //y = sy;
    }
  }
  /* 
   gx += sin(ss+PI)*190;
   gy += cos(ss+PI)*190;
   ss = atan2(gx-x, gy-y)-PI;
   */
}

コメントを残す