a-13 反応(5) カーソルの位置

Jul 5, 2018

a-13 反応(5) カーソルの位置

カーソル位置を知るために、if文を使ってmouseXとmouseYの値を調べる

float x;
int offset = 10;
void setup(){
  size(240,120);
  x = width/2;
}
void draw(){
  background(255,255,0);
  if(mouseX > x){
    x += 0.5;
    offset = -10;
  }
  if(mouseX < x){
    x -= 0.5;
    offset = 10;
  }
  line(x,0,x,height);
  line(mouseX,mouseY,mouseX+offset,mouseY-10);
  line(mouseX,mouseY,mouseX+offset,mouseY+10);
  line(mouseX,mouseY,mouseX+offset*3,mouseY);
}

カーソルの位置に合わせて→とバーが反応する。

dist()を使って円の中心からカーソルまでの距離を調べる。その値が円の半径よりも小さければ、カーソルは円の内側にあると判定する。

int x= 120;
int y = 60;
int radius=12;

void setup(){
  size(240,120);
  ellipseMode(RADIUS);
}
  
  void draw(){
    background(204);
    float d =dist(mouseX,mouseY,x,y);
    if (d < radius){
      radius++;
      fill(0);
    }else{
      fill(255);
    }
    ellipse(x,y,radius,radius);
  }
 

長方形の内側にカーソルがあることを知るのは円とは別の方法をとる。4辺すべてに対して真ならばカーソルは内側と判定する。

int x =100;
int y =40;
int w =100;
int h =80;

void setup(){
  size(300,160);
}

void draw(){
  background(204);
  if ((mouseX > x) && (mouseX < x+w) &&
  (mouseY > y) && (mouseY < y+h)) {
    fill(0);
  }else{
    fill(255);
  }
  rect(x,y,w,h);
}

&&は論理演算のANDを表す記号で全ての式が真であることを確認するために使われている

#animation

#いったところmemo

#読書きろく