tolyan
tolyan

Reputation: 879

How can I get a segment of a Path between two points using paper.js?

I have a Path that intersects with another Path. Paper.js can give me points of intersection of these paths. Next, I need to get a segment of one path from one point of intersection to another. Example on the picture:

enter image description here

How can I get it using paper.js?

Upvotes: 2

Views: 1863

Answers (1)

sasensi
sasensi

Reputation: 4650

You can use Path.Line constructor to create a line between 2 points.

Here is a Sketch drawing a line between 2 circles intersections.

// draw 2 circles
// one red
var redCircle = new Path.Circle({
    center     : view.center,
    radius     : 50,
    strokeColor: 'red'
});
// one blue
var blueCircle         = redCircle.clone();
blueCircle.strokeColor = 'blue';
blueCircle.position += [ 50, 0 ];

// get intersection points
var intersections = redCircle.getIntersections(blueCircle);
var point1        = intersections[ 0 ].point;
var point2        = intersections[ 1 ].point;

// draw a black circle at both intersections
var intersection1Circle = new Path.Circle({
    center     : point1,
    radius     : 5,
    strokeColor: 'black'
});
var intersection2Circle      = intersection1Circle.clone();
intersection2Circle.position = point2;

// draw a line between intersections
var intersectionsJoinLine = new Path.Line({
    from       : point1,
    to         : point2,
    strokeColor: 'black'
});

Upvotes: 1

Related Questions