How to get intersection points between polyline with another

The .getData().castToShape() construct is generally not a good idea since a data object is created (but not used) and a pointer to it’s shape is retrieved which is well used. The data object will get deleted by the garbage collector and the pointer will become invalid.

I’d refactor as follows:
var r1Shape = new RPolyline([n1, n2, n3, n4], false);
drawPolyline(r1Shape);

var r2Shape = new RSpline();
r2Shape.appendFitPoint(n5);
r2Shape.appendFitPoint(n6);
r2Shape.appendFitPoint(n7);
drawSpline(r2Shape);