0

I have some situation here, I have one screen and when I touch on it drawing a dot on this coordinates, problem is that I want to draw second dot on the same line x or y it depend if we touch under the dot or next to it? Have you got any ideas about that?

Here is my drawing method at the moment:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            points.add(new MapPoint(event.getX(), event.getY()));
            if (surfaceHolder.getSurface().isValid()) {
                canvas = surfaceHolder.lockCanvas();
                canvas.drawColor(Color.WHITE);
                for (MapPoint point : points) {
                    canvas.drawCircle(point.getX(), point.getY(), 10, paint);
                }
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
        return false;
    }

This is what I am trying to do, but my problem now is that if I find any point with this coord it draws near the point not on the touched place:

public void addPoint(float x, float y){
        if(points.size() < 1) {
            points.add(new MapPoint(x, y));
        } else {

            float deltaX = Math.abs(x - points.get(points.size() - 1).getX());
            float deltaY = Math.abs(y - points.get(points.size() - 1).getY());

            for (int i = 0; i < points.size(); i++) {
                if(Math.max(deltaX , deltaY) == deltaX){

                    y = points.get(i).getY();
                }else{

                    x = points.get(i).getX();
                }

            }

            points.add(new MapPoint(x, y));
        }
    }
Tito
  • 478
  • 3
  • 19

2 Answers2

1

Calculate the maximum of the absolute difference of your positions.

float deltaX = Math.abs(event.getX()-otherPoint.getX());
float deltaY = Math.abs(event.getY()-otherPoint.getY());
if(Math.max(deltaX , deltaY) == deltaX){
// touched rather to left or to right of the otherPoint
}else{
// touched rather below or above the otherPoint
}
AlbAtNf
  • 3,859
  • 3
  • 25
  • 29
  • It helps, but I have some problem with i, I updated the code you can see what I mean. – Tito Feb 04 '16 at 13:53
  • @Tito please make it a bit more clear, what exactly you are trying to do. – AlbAtNf Feb 04 '16 at 14:04
  • for example when I touch the four points of the screen I want to connect them with a line but I cant touche the same width with the last point for example so I want to make something like autocorrect – Tito Feb 04 '16 at 14:06
  • sorry, I still don't get it. But you might be looking for circle collision detection (touch circle and already drawn circle). Have a look at [this](http://stackoverflow.com/a/1736741/4791726) – AlbAtNf Feb 04 '16 at 14:12
0

It seems that there are no direct converting methods.

You have to convert it manually by the help of maths.

You can process the coordinates according to the properties, like translation or scaling or even rotation for entire screen.

Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65