Trying to realize the target shot. This type of target: Target

It is registered in ImageView with the fitCenter property specified in scaleType. Those. will stretch under the width of the screen, being strictly in the center, with the boundaries of the target literally resting on the screen.

The user sets the touch and moves the hole on the target, then by pressing the "Save" button enters the data and evaluation into something-type-DB (SharedPreferences)

Now I have the following code on my assessment:

public void getTheMarkForShot(float absoluteX, float absoluteY){ ImageView aimTarget = findViewById(R.id.aimTarget); TextView textView = findViewById(R.id.textView2); ImageView red_dot = findViewById(R.id.red_dot); //Координаты центра - считаются правильно float centerX=aimTarget.getX() + aimTarget.getWidth() / 2; float centerY=aimTarget.getY() + aimTarget.getHeight() / 2; //Определим расстояние от центра пробоины до центра мишени double absoluteDistance = Math.sqrt(Math.pow(absoluteX - centerX, 2) + Math.pow(absoluteY - centerY, 2)); //Определяем координаты точки на пробоине, ближайшей к центру мишени double betterX = absoluteX * ((absoluteDistance - (red_dot.getWidth() / 2)) / absoluteDistance); double betterY = absoluteY * ((absoluteDistance - (red_dot.getHeight() / 2)) / absoluteDistance); //Определение оценки и вывод результата byte finalMark; //Если координаты ближайшей точки находятся вне круга (смотрим по его радиусу) if ((Math.pow(betterX - centerX, 2) + Math.pow(betterY - centerY, 2)) > Math.pow(aimTarget.getWidth() / 2 , 2)){ //Оценка равна 0. Выводим результаты на экран. finalMark = 0; String coordinatesMessage = String.format("Координаты выстрела: (%.2f, %.2f). Оценка: %s", absoluteX - centerX, centerY - absoluteY, finalMark); textView.setText(coordinatesMessage); } //Иначе - определяем в каком из сегментов круга находится пробоина else { //Разделим ширину круга на части for (int i = 10; i > -1; i--) { if ((Math.pow(betterX - centerX, 2) + Math.pow(betterY - centerY, 2)) <= Math.pow(aimTarget.getWidth() / 2 - (i * (aimTarget.getWidth() / 2/10)), 2)){ finalMark = (byte)i; String coordinatesMessage = String.format("Координаты выстрела: (%.2f, %.2f). Оценка: %s", absoluteX - centerX, centerY - absoluteY, finalMark); break; } } } } 

At the moment, the following is registered:

 @SuppressLint("ClickableViewAccessibility") public void getTouchCoordinates(){ final ImageView aimTarget = findViewById(R.id.aimTarget); final ImageView red_dot = findViewById(R.id.red_dot); aimTarget.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { float x; float y; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { break; } case MotionEvent.ACTION_MOVE: { x = event.getX(); y = event.getY(); red_dot(x, y); getTheMarkForShot(x,y); return true; } case MotionEvent.ACTION_UP: { x = event.getX(); y = event.getY(); red_dot(x, y); getTheMarkForShot(x,y); return false; } } return true; } }); } 

The result of the above is determined only by a score of 0. In other cases, only coordinates are determined, without a score (implemented in the red_dot function).

So, the question itself: how, in this case, is it possible to determine the shot estimate most correctly? Three pages of Google, three times a day for three days, gave the only similar task: Problem in C ++ about targets . Tried to implement, in the end, the score 0 is determined generally across the entire screen.

I beg you to help, time is not something that is pressing - there is none at all = (

    1 answer 1

    You have complicated everything. All you had to do was find the center of the circle, you have it, find the radius of the circle, you have half the width of the screen, the distance from the center to the point of depression, you also think. And you just need to check how close the point is from the center relative to the radius. If the radius is greater than 0, otherwise the distance / radius will be from 0 to 1. Invert again, subtract this number from 1 so that the center has a larger value. Multiply by 10 and rounded and there will be a number in which you hit