# 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*.position, Input.touches[j].position);*
``````

angle = Vector2.Angle(Input.touches[j].position, Input.touches[j].position -
Input.touches*.position);*
Debug.Log("i: {i}");*_ _*Debug.Log(“j: {j}”);
Debug.Log("Touch {i} position: {Input.touches*.position}");*_</em> <em>_*Debug.Log(“Touch {j} position: {Input.touches[j].position}”);*

distanceText.text = " Distance : " + distance;
angleText.text = "Angle : " + angle;
}
}
}
}
[192708-2022-02-16-20h36-15.png|192708]

# 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 *= Vector2.Angle(previous - vertex, next - vertex);*
``````

}
for (; i < angles.Length; i++)
{
angles = 0;
}
angleText.text = "Angles: [{string.Join(",", angles)}]";*_ _*}*_ _*---*_ <em>_For **all** possible combinations of triangles with the touches_</em> <em>*_(Code tested)_*</em> _*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("
({firstTouchIndex + 1}, {(secondTouchIndex % touchCount) + 1}, {(thirdTouchIndex % touchCount) + 1}) = {angle}");

}
}
}

angleText.text = stringBuilder.ToString();
}