On Wednesday, 21 October 2015 12:14:33 UTC+3, Ian wrote:
I had a quick peek at this but ran out of time, once I saw the documentation in details...here is how I would approach it, I may well be wrong in what the bits do, but it may give you some ideas...
If we take a look at a quick fiddle I did, eg
https://jsfiddle.net/cqrf411L/4/ (last few lines don't really work atm but may give an idea of where I was heading )
OK, I saw the fiddle... but that code (I think) cheats a bit because the intersection point "happens" to overlap with the starting point of the path.
In a real world scenario, we know where the starting point is, but we have no idea where the intersection point happens. So this is why I need to obtain the length of the path from the start point to the intersection points.
Raphael has a getPointAtLength() function, which returns the coordinated (x, y) of a point at a given length of the path.
But what is really needed is the inverse of that, namely the length of the path, from the beginning to a point of specific (x, y) coordinates.
I know that a point is inside a path, using Element.isPointInside(), but I don't know where (at which length) is that point inside a path.
I would have imagined that t1 and t2 are percentages of the total length of the path..., but either I don't understand how to use them, or they're not what we think they are.
For example, let's take these two shapes (some ellipses in coordinates using cubiec bezier curves), using your fiddle notation (where "r" is the paper):
var e1 = "M 293.4,210 C 290.7,288.9 320.1,357.5 359,363.4 398,369.3 431.7,310.2 434.4,231.3 437.1,152.4 407.7,83.77 368.8,77.87 329.8,71.96 296.1,131 293.4,210 z";
var e2 = "M 341.4,237.6 C 396.5,294 421,364.5 396.2,395.1 371.4,425.8 306.7,404.7 251.6,348.3 196.4,291.8 171.9,221.4 196.7,190.7 221.5,160.2 286.2,181.1 341.4,237.6 z";
var pe1 = r.path(e1);
var pe2 = r.path(e2);
var intersection = Raphael.pathIntersection( e1, e2 );
Now, the intersection points are indeed inside the path, but at which length...?
I would have imagined that t1 and t2 might be percentages of length, but that doesn't seem to be the case:
var pe1tl = pe1.getTotalLength(); // 691.3809814453125
console.log(pe1.getPointAtLength(pe1tl * intersection[0].t1)) // {x: 426.07913701276846, y: 289.4692102608795, alpha: 104.99917071647815}
console.log(pe1.getPointAtLength(pe1tl * intersection[0].t2)) // {x: 430.27349773764143, y: 172.0948398923059, alpha: 259.75147609911164}
console.log(pe1.getPointAtLength(pe1tl * intersection[1].t1)) // {x: 296.38104075711533, y: 177.4903843425423, alpha: 98.66809287261626}
console.log(pe1.getPointAtLength(pe1tl * intersection[1].t2)) // {x: 391.5010779152665, y: 89.10259705181718, alpha: 221.0237982989102}
As you can see, nothing resembles the intersection points.
So what t1 and t2 really are, is a mystery to me. Also, how could one use the coordinates from bez1 and bez2 components?
To my mind, the solution would be:
a. calculate the length of the path at the intersection point 1
b. calculate the length of the path at the intersection point 2
c. get the subpath using:
pe1.getSubpath(length1, length2)
But I'm stuck at a) and b)...
Adrian