Sg2.9
simply cut like the scissor t...
Recent RSS
simply cut like the scissor tool
Date:  28. May 2010, 19:02

Hi There,

I'm very new to scriptographer (even if i used to be in the same classroom as Jürg some years ago...(Hi Jürg!)).

I'm trying to make my first script. I have a circle drawn by the script, i've defined a new point wich is exactly on the circle path.
Now i'd like to cut the circle at the very point defined above, but i don't get how to use the divide or split fonction (or something else?)

I'm used to use AI by "hand", and i achieve this kind of thing using the scissor tool: you just point the tool to the circle, click and it cuts the circle.
how do i do the same via scripts?

Thank you very much.

Raphael

Re: simply cut like the scissor tool
Date:  30. May 2010, 15:08

You need to use path.hitTest with your point, this will return a HitResult, which you can use to split the path:

var position = new Point(50, 50);
var circle = new Path.Circle(position, 5);

var splitPoint = position + new Point(5, 0);

var hitResult = circle.hitTest(splitPoint);

if(hitResult) {
	circle.split(hitResult);
	circle.segments.last.point += new Point(5, 0)
} else {
	print('The point was not on the path.');
}

ps: currently when you use a point, it returns an error - while actually it should tell you that it doesn't accept a point as a parameter. I will add this as a bug to GitHub. Also, it might be nice to have divide also accept a point as a parameter.

Re: simply cut like the scissor tool
Date:  31. May 2010, 13:02

Thank you!

it works for what i needed.

one precision though: what does this line in your code do? circle.segments.last.point += new Point(5, 0)

Re: simply cut like the scissor tool
Date:  31. May 2010, 13:51

well, maybe i can answer that myself.
i tried to define the points used by hitTest at random using the following code:

//FIRST CIRCLE
var xorig = Math.random()*40;
var yorig = Math.random()*40;
var orig = new Point(xorig, yorig);
var radius = Math.random()*20;
var circle0 = new Path.Circle(orig, radius);

var theta1 = Math.random();
var theta2 = Math.random();

var cos1 = Math.cos(theta1);
var sin1 = Math.sin(theta1);
var cos2 = Math.cos(theta2);
var sin2 = Math.sin(theta2);

var diffx1 = xorig + cos1*radius;
var diffy1 = yorig + sin1*radius;
var point1 = new Point(diffx1, diffy1);

var diffx2 = xorig + cos2*radius;
var diffy2 = yorig + sin2*radius;
var point2 = new Point(diffx2, diffy2);

//CUT THE CIRCLE	
var hitResult1 = circle0.hitTest(point1);
var seg1 = circle0.split(hitResult1); 
//seg1.selected=true

var hitResult2 = circle0.hitTest(point2);
var seg2 = circle0.split(hitResult2); 
var seg;

if (seg1.length>seg2.length)
	{
	seg2.remove();
	var seg = seg1;
	}
else 
	{
	seg1.remove();
	var seg = seg2;
	}

but once in a while i got a "NullPointerException" in the console. So i guess it's a matter of precision. AI (or SG?) can't be infinitely precise. So sometime the points chosen at random are not on the circle.
Am I right?

thanks!

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Gearwheels 18.08.08