Sg2.9
Precision
Recent RSS
Precision
Date:  31. May 2010, 17:37

I make a new post out of the one on the scissors cut...

I play with random numbers, points and a circle.
My script aims at cutting an arc from the circle, an arc whose length is defined by two points chosen at random on the perimeter of the circle.

I could manage to define the points like this:

var xorig = Math.random()*20; // x coordinate for the center of circle.
var yorig = Math.random()*20;
var orig = new Point(xorig, yorig);

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);

but the when a try to draw a line from the center of the circle to the point on the circle,

var myPath1 = new Path.Line(orig, point1); 

either the line comes short of intersecting with the circle, or there's a offset, so it doesn't match.

to cut the circle i used the hitTest fonction. I tried to play with the tolerance number, but i can't get something satisfying. Furthermore, since in the following of the script i loop this kind of "circle-cutting", each and every time it misses the circle, the script doesn't go further...

very often i get a NullPointerException with this line:

var hitResult1 = circle0.hitTest(point1, 5);

so i'm kind of stuck.

any help with precision?

thank you.

Re: Precision
Date:  1. June 2010, 12:26

At the moment there is a little bug in path.split - while we fix this, I wrote some code for you that should achieve what you need. Instead of creating a circle shaped path and removing bits from it, it draws an arc in between two angles.

I'm a bit too busy to explain exactly whats going on, but I hope this can help you for the time being. When the bug is fixed, I will post the code how you would achieve this by using path.split.

// a random point in the document:
var center = Point.random() * document.size;

var radius = 5;

var firstAngle = (Math.random() * 360).toRadians();
var secondAngle = (Math.random() * 360).toRadians();

// the angle in the middle of first and second,
// rotated by 180 degrees
var middleAngle = (firstAngle + secondAngle) / 2 + (180).toRadians();

var vector = new Point(radius, 0);

var path = new Path();

var arcStart = center + vector.rotate(firstAngle);
var arcMiddle = center + vector.rotate(middleAngle);
var arcEnd = center + vector.rotate(secondAngle);

path.add(arcStart);
path.arcThrough(arcMiddle, arcEnd);
Re: Precision
Date:  1. June 2010, 23:23

Thank you.
I can now iterate!
:)

Re: Precision
Date:  2. June 2010, 12:07

Go forth and iterate! : )

Re: Precision
Date:  2. June 2010, 17:42

After looking again, there is no bug in path.split(position) - but since the way I described above is more precise, I think its the best way. If the positions that you are hit testing are both very close to a segment point, the hit tests will both return the same point.

Alternatively you can work with the length of the path:

var radius = 5;
var position = Point.random() * document.size;

var circle = new Path.Circle(position, radius);

var firstLength = Math.random() * circle.length;

var secondLength = Math.random() * circle.length;

circle.split(firstLength);

// The second time we split,
// the path is split into two and it will return a path:
var leftOver = circle.split(secondLength);

leftOver.remove();
Re: Precision
Date:  2. June 2010, 18:20

Ÿes. The inacuracy of my method was probably related to the fact that my random points were generated from a random angle at the center of the circle. By working directly with circle.length, you can't be off the curve!

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
Parallel Arrangement 07.04.12