Existing answers do not take into account that the end points are arbitrary (rather than given).
Thus, when measuring the straightness of the curve, it does not make sense to use the end points (for example, to calculate expected length, angle, position).
A simple example would be a straight line with both ends kinked. If we measure using the distance from the curve and the straight line between the end points this will be quite large, as the straight line we have drawn is offset from the straight line between the end points.
How do we tell how straight the curve is? Assuming that the curve is smooth enough, we want to know how much, on average, the tangent to the curve is changing. For a line, this would be zero (as the tangent is constant).
If we let the position at time t be \$(x(t),y(t))\$, then the tangent is \$(D_x(t),D_y(t))\$, where \$D_x(t)\$ is the derivative of x at time t.
If the curve is not parameterized by arc-length, we normalize by dividing by \$||(D_x(t),D_y(t))||\$.
So we have a unit vector (or angle) of the tangent to the curve at time t.
So, the angle is \$a(t)=(D_x(t),D_y(t))/||(D_x(t),D_y(t))||\$
We are then interested in \$||D_a(t)||^2\$ integrated along the curve.
Given that we most likely have discrete data points rather than a curve, we must use finite differences to approximate the derivatives.
So, \$D_a(t)\$ becomes \$\frac{a(t+h)-a(t)}{h}\$.
And, \$a(t)\$ becomes $$\frac{(x(t+h)-x(t))/h,(y(t+h)-y(t))/h}{||((x(t+h)-x(t))/h,(y(t+h)-y(t))/h)||}$$
Then we then get S by summing up \$h||D_a(t)||^2\$ for all data points and possibly normalizing by the length of the curve.
Most likely, we use \$h=1\$, but it really is just an arbitrary scale factor.
To reiterate, S will be zero for a line and larger the more it deviates from a line.
To convert to the required format, use \$\frac{1}{1+S}\$.
Given that the scale is somewhat arbitrary, it is possible to multiply S by some positive number (or transform it in some other way, e.g. use \$bS^c\$ instead of S) to adjust how straight certain curves are.
j=1
so that you can comparetouchList[j]
withtouchList[j-1]
, but whentouch.phase == TouchPhase.Began
ortouch.phase == TouchPhase.Ended
the positions are not added totouchList
and subsequently not included insumLength
. This bug would be present in all cases but would be more apparent when the line has few segments. \$\endgroup\$