Occurrence of one vector in another by coordinates



I have 2 pair of coords: (x1;x2) – first segment and (x1′; x2′) – second segment. How do I calculate the degree of occurrence of one segment in another?

Example:

vector1 – (10; 15); vector2 – (13; 20); vector2 is included in vector1 by 2/7.

Thanks!

Answer

If segments ends are ordered (x1 always less than x2), second segment is (x3,x4):

l = max(x1, x3)
r = min(x2, x4)
return l < r? (r-l)/(x4-x3): 0


Source: stackoverflow