- Home /
How to calculate angle in the triangle formed by screen touches
I am trying to calculate the angles of a triangle that is formed by three touches on the screen (1,2,3) - please look at the attached picture and the angles marked in red. I am using Vector2.Angle for this but I am not sure if I am doing it correctly. Can you suggest how to do it so that every new touch on the screen when such a triangle is formed, I check every angle ?
public class TouchPosition : MonoBehaviour
{
public Text distanceText, angleText;
public float angle, distance;
void Update()
{
CheckDistanceAndAngle();
}
public void CheckDistanceAndAngle()
{
for (int i = 2; i < Input.touchCount; i++)
{
for (int j = 0; j < i; j++)
{
distance = Vector2.Distance(Input.touches[i].position, Input.touches[j].position);
angle = Vector2.Angle(Input.touches[j].position, Input.touches[j].position -
Input.touches[i].position);
Debug.Log($"i: {i}");
Debug.Log($"j: {j}");
Debug.Log($"Touch {i} position: {Input.touches[i].position}");
Debug.Log($"Touch {j} position: {Input.touches[j].position}");
distanceText.text = " Distance : " + distance;
angleText.text = "Angle : " + angle;
}
}
}
}
Answer by Hellium · Feb 16 at 08:18 PM
Code not tested
float[] angles = new float[3];
public void CheckDistanceAndAngle()
{
int touchCount = Input.touchCount;
int i = 0;
for (; touchCount > 2 && i < touchCount && i < angles.Length; i++)
{
Vector2 vertex = Input.GetTouch(i).position;
Vector2 previous = Input.GetTouch((i - 1 + touchCount) % touchCount).position;
Vector2 next = Input.GetTouch((i + 1) % touchCount).position;
angles[i] = Vector2.Angle(previous - vertex, next - vertex);
}
for (; i < angles.Length; i++)
{
angles[i] = 0;
}
angleText.text = $"Angles: [{string.Join(",", angles)}]";
}
For all possible combinations of triangles with the touches
(Code tested)
public void CheckDistanceAndAngle()
{
int touchCount = Input.touchCount;
if(touchCount <= 2)
return;
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder("Angles:");
for (int firstTouchIndex = 0; firstTouchIndex < touchCount; firstTouchIndex++)
{
for (int secondTouchIndex = firstTouchIndex + 1; secondTouchIndex < firstTouchIndex + touchCount - 1; secondTouchIndex++)
{
for (int thirdTouchIndex = secondTouchIndex + 1; thirdTouchIndex <= firstTouchIndex + touchCount - 1; thirdTouchIndex++)
{
Vector2 first = Input.GetTouch(firstTouchIndex).position;
Vector2 second = Input.GetTouch(secondTouchIndex % touchCount).position;
Vector2 third = Input.GetTouch(thirdTouchIndex % touchCount).position;
float angle = Vector2.Angle(second - first, third - first);
stringBuilder.Append($"\n({firstTouchIndex + 1}, {(secondTouchIndex % touchCount) + 1}, {(thirdTouchIndex % touchCount) + 1}) = {angle}");
}
}
}
angleText.text = stringBuilder.ToString();
}
Thank you for that. Actually I have tested it but still on the screen I can see only value 0 for all indexes not the position value as shoud be. Do you have any idea why it is like this ?
For reference, it looks like there's an error in line 6 if you used this as-is:
for (; i > 2 && i < touchCount && i < angles.Length; i++)
... should be...
for (; touchCount > 2 && i < touchCount && i < angles.Length; i++)
Wow fantastic, it is working as expected! Thank you so much. Do you think that is it possible to detect every configuration of new triangles ? Lets say that we have 6 touches which are create couple of different patterns of triangles
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can't see the angle value on the screen 0 Answers