首页 > Sktech > 习作CB_简单求对角线交点应用直角三角形范围判定
2017
12-14

习作CB_简单求对角线交点应用直角三角形范围判定

习作CB_简单求对角线交点应用直角三角形范围判定 - 第1张  | Processing编程艺术


通过简单地比较目标点和对角线焦点的纵轴坐标值判断目标点是否在直角三角形的描画范围内.


[Q]:退出程序


在2.0内制作, 用3.0以上运行可能会报错:


//- --- --- ---
//- public
//- --- --- ---
//-
EcTrangleObstacle pbMyObstacleAA=new EcTrangleObstacle(80, 10, 40, 30, 'a');
EcTrangleObstacle pbMyObstacleAB=new EcTrangleObstacle(60, 60, 40, 30, 'b');
EcTrangleObstacle pbMyObstacleAC=new EcTrangleObstacle(120, 120, 40, 30, 'c');
EcTrangleObstacle pbMyObstacleAD=new EcTrangleObstacle(180, 180, 40, 30, 'd');
//-

public void setup() {size(320, 240);noStroke();frameRate(32);textAlign(LEFT, TOP);ellipseMode(CENTER);
  frame.setTitle("CharaMap");
  //--
}//+++

public void draw() {background(0);
  //--
  pbMyObstacleAA.cmAct=pbMyObstacleAA.ccIsContaining(mouseX, mouseY);pbMyObstacleAA.ccUpdate();
  pbMyObstacleAB.cmAct=pbMyObstacleAB.ccIsContaining(mouseX, mouseY);pbMyObstacleAB.ccUpdate();
  pbMyObstacleAC.cmAct=pbMyObstacleAC.ccIsContaining(mouseX, mouseY);pbMyObstacleAC.ccUpdate();
  pbMyObstacleAD.cmAct=pbMyObstacleAD.ccIsContaining(mouseX, mouseY);pbMyObstacleAD.ccUpdate();
  fill(0xFF,0x77,0x77);ellipse(mouseX,mouseY,4,4);
  //--
}//+++

public void keyPressed() {switch(key){
  //--
  case 'q':fsPover();
  default:break;
}}//+++

void fsPover(){
  exit();
}//+++

//-- --- --- ---
//-- CLASS
//-- --- --- ---

abstract class EcObstacle{
  int cmX,cmY,cmW,cmH;
  int cmOnColor;
  boolean cmAct;
  EcObstacle(){
    cmX=cmY=cmW=cmH=9;
    cmOnColor=color(0xEE,0xEE,0x33);
    cmAct=false;
  }
  //--
  abstract void ccUpdate();
  abstract boolean ccIsContaining(int pxX, int pxY);
}//+++

class EcTrangleObstacle extends EcObstacle{
  //--
  char cmMode;
  /** *
   * @param pxMode_abcd [a]north west point..[b]north east point..
   * [c]south east point..[d]south west point
   */
  EcTrangleObstacle(int pxX, int pxY, int pxW, int pxH, char pxMode_abcd){
    super();
    cmX=pxX;
    cmY=pxY;
    cmW=pxW;
    cmH=pxH;
    cmMode=pxMode_abcd;
  }
  //--
  @Override void ccUpdate(){
    fill(cmAct?cmOnColor:0x55);
    switch(cmMode){
      case 'a':triangle(cmX, cmY, cmX+cmW, cmY, cmX, cmY+cmH);break;
      case 'b':triangle(cmX, cmY, cmX+cmW, cmY, cmX+cmW, cmY+cmH);break;
      case 'c':triangle(cmX+cmW, cmY, cmX+cmW, cmY+cmH, cmX, cmY+cmH);break;
      case 'd':triangle(cmX, cmY, cmX+cmW, cmY+cmH, cmX, cmY+cmH);break;
      default:break;
    }
  }
  //--
  @Override boolean ccIsContaining(int pxX, int pxY){
    boolean lpRes=false;
    lpRes=(pxX>cmX)&&(pxX<cmX+cmW)&&(pxY>cmY)&&(pxY<cmY+cmH);
    if(!lpRes){return lpRes;}else{
      int lpDPointL=(pxX-cmX)*cmH/cmW+cmY;
      int lpDPointR=(cmX+cmW-pxX)*cmH/cmW+cmY;
      switch(cmMode){
        case 'a':return pxY<lpDPointR;
        case 'b':return pxY<lpDPointL;
        case 'c':return pxY>lpDPointR;
        case 'd':return pxY>lpDPointL;
        default:return false;
      }
    }
  }
  //--
}//+++

//EOF

 



最后编辑:
作者:constrain
constrain
nullpointerexception

留下一个回复

你的email不会被公开。