[SOLVED] How can this piece of code be improved?

Purpose: the user draws a line in any direction, then from the end of the line (without lifting the finger), the user draws a second line facing downwards towards the start point of the touch. My goal is to use re-create a “perfect version” of the shape.

if (touch.phase == TouchPhase.Ended) {
    touchPoints [touch.fingerId].Add (touch.position);
    int lastIndex = touchPoints [touch.fingerId].Count - 1;
   
    int midPoint = 0;
    for (int j = 1; j < lastIndex - 1; j++) {
        if (Vector2.Distance (touchPoints [touch.fingerId] [j], touchPoints [touch.fingerId] [0]) >= Vector2.Distance (touchPoints [touch.fingerId] [j - 1], touchPoints [touch.fingerId] [0])) {
            midPoint = j;
        } else {
            break;
        }
    }
   
    float touchLength = Vector2.Distance (touchPoints [touch.fingerId] [0], touchPoints [touch.fingerId] [midPoint]);                           
    float slope = HelperScript.CalculateSlopeM (touchPoints [touch.fingerId] [0], touchPoints [touch.fingerId] [midPoint]);
    float slope2 = HelperScript.CalculateSlopeM (touchPoints [touch.fingerId] [midPoint], touchPoints [touch.fingerId] [lastIndex]);
    float b = HelperScript.CalculateSlopeB (slope, touchPoints [touch.fingerId] [0].x, touchPoints [touch.fingerId] [0].y);
    float b2 = HelperScript.CalculateSlopeB (slope, touchPoints [touch.fingerId] [midPoint].x, touchPoints [touch.fingerId] [midPoint].y);
    bool hasDeviated = false;
    float touchLength2 = Vector2.Distance (touchPoints [touch.fingerId] [midPoint], touchPoints [touch.fingerId] [lastIndex]);   
   
    Debug.Log ("Finger " + touch.fingerId + " - Touch Length: " + touchLength);
   
    if (slope != 0) {
        for (int j = 1; j < midPoint - 1; j++) {
            if (Mathf.Abs (((slope * touchPoints [touch.fingerId] [j].x) + b) - touchPoints [touch.fingerId] [j].y) > touchMaxDeviation) {
                hasDeviated = true;
                break;
            }
        }
    } else if (touchPoints [touch.fingerId] [midPoint].x - touchPoints [touch.fingerId] [0].x == 0) {
        for (int j = 1; j < midPoint - 1; j++) {
            if (Mathf.Abs (touchPoints [touch.fingerId] [0].x - touchPoints [touch.fingerId] [j].x) > touchMaxDeviation) {
                hasDeviated = true;
                break;
            }
        }
    } else {
        for (int j = 1; j < midPoint - 1; j++) {
            if (Mathf.Abs (touchPoints [touch.fingerId] [0].y - touchPoints [touch.fingerId] [j].y) > touchMaxDeviation) {
                hasDeviated = true;
                break;
            }
        }     
    }
   
    if (hasDeviated == false) {
        if (slope2 != 0) {
            for (int j = midPoint + 1; j < lastIndex - 1; j++) {
                if (Mathf.Abs (((slope2 * touchPoints [touch.fingerId] [j].x) + b2) - touchPoints [touch.fingerId] [j].y) > touchMaxDeviation) {
                    hasDeviated = true;
                    break;
                }
            }
        } else if (touchPoints [touch.fingerId] [midPoint].x - touchPoints [touch.fingerId] [0].x == 0) {
            for (int j = midPoint + 1; j < lastIndex - 1; j++) {
                if (Mathf.Abs (touchPoints [touch.fingerId] [midPoint].x - touchPoints [touch.fingerId] [j].x) > touchMaxDeviation) {
                    hasDeviated = true;
                    break;
                }
            }
        } else {
            for (int j = midPoint + 1; j < lastIndex - 1; j++) {
                if (Mathf.Abs (touchPoints [touch.fingerId] [midPoint].y - touchPoints [touch.fingerId] [j].y) > touchMaxDeviation) {
                    hasDeviated = true;
                    break;
                }
            }     
        }
    }

Fix the indent!

1 Like

Fixed. Sorry about that.

Bump.

bump

I don’t understand this; can you elaborate?

For example, if the user draws an “L” shape, the points of that L shape won’t be aligned perfectly. I want to re-create a perfect shape where all the points are aligned correctly.

I see