Android Canvas检查Point是否在线

时间:2013-08-26 22:27:30

标签: java android android-canvas

我有一个自定义视图,它绘制线条并将它们保存到ArrayList。我想要做的是检查指定点是否在Arraylist内的一行上并返回该行。当线条直线使用if (l.startX == l.stopX && l.startY < l.stopY时,我能够做到这一点,但如果线条处于某个角度,则不起作用。我也想用drawText和drawCircle来做这个。我错过了一些简单的方法吗?

DrawView.java

class Line {
  float startX, startY, stopX, stopY;
  public Line(float startX, float startY, float stopX, float stopY) {
    this.startX = startX;
    this.startY = startY;
    this.stopX = stopX;
    this.stopY = stopY;
  }
  public Line(float startX, float startY) { // for convenience
    this(startX, startY, startX, startY);
  }
}

public class DrawView extends View {
  Paint paint = new Paint();
  ArrayList<Line> lines = new ArrayList<Line>();

  public DrawView(Context context, AttributeSet attrs) {
    super(context, attrs);

    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    for (Line l : lines) {
      canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);
    }
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      lines.add(new Line(event.getX(), event.getY()));
      return true;
    }
    else if ((event.getAction() == MotionEvent.ACTION_MOVE ||
        event.getAction() == MotionEvent.ACTION_UP) &&
        lines.size() > 0) {
      Line current = lines.get(lines.size() - 1);
      current.stopX = event.getX();
      current.stopY = event.getY();
      Invalidate();
      return true;
    }
    else {
      return false;
    }
  }
}

2 个答案:

答案 0 :(得分:2)

我不知道是否有用于此的画布库,但您可以手动:

  1. 浏览您的行列表
  2. 形成每条线的方程式,例如斜率截距形式y = mx + b
  3. 将event.X()和event.Y()插入上面的等式中,每行为x和y
  4. 如果双方相等,则您的触摸事件就在该行

答案 1 :(得分:1)

经过几个小时的尝试后,我设法得到一个算法来检查一个点是否在给定的线上,使用斜率交叉公式的混合和if (x = startX && y > startY && y < stopY)下面是代码,如果是代码,则返回true点数在线,否则为假,希望这可以节省一些时间!

public boolean checkPosition(float x, float y, float sx, float sy, float ex, float ey) {
        float mX = ex - sx;
        float mY = ey - sy;
        float smX = sx - ex;
        float smY = sy - ey;
        float pmX = mX;
        float pmY = mY;
        float psmX = smX;
        float psmY = smY;
        float yY = ey - y;
        float xX = ex - x;
        float sX = sx - x;
        float sY = sy - y;
        float m = mY / mX;
        float b = sy - (m * sx);
        if (mX < 0) {
            pmX = mX * - 1;
        }
        if (mY < 0) {
            pmY = mY * - 1;
        }
        if (smX < 0) {
            psmX = smX * - 1;
        }
        if (smY < 0) {
            psmY = smY * - 1;
        }
        if (yY < 0) {
            yY = yY * - 1;
        }
        if (xX < 0) {
            xX = xX * - 1;
        }
        if (sX < 0) {
            sX = sX * - 1;
        }
        if (sY < 0) {
            sY = sY * - 1;
        }
        if (sy == ey && y == sy) {
            if (sx >= ex) {
                if (x <= sx && x >= ex) return true;
                else return false;
            }
            else if (ex >= sx) {
                if (x <= ex && x >= sx) return true;
                else return false;
            }
            else return false;
        }
        else if (sx == ex && x == sx) {
            if (sy >= ey) {
                if (y <= sy && y >= ey) return true;
                else return false;
            }
            else if (ey >= sy) {
                if (y <= ey && y >= sy) return true;
                else return false;
            }
            else return false;
        }
        else if (xX <= pmX && sX <= psmX && yY <= pmY && sY <= psmY) {
            if (y == (m * x) + b) return true;
            else return false;
        }
        else return false;
    }