mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-22 07:19:57 -05:00
Tests
This commit is contained in:
parent
ac05de7426
commit
b914c60c6f
1 changed files with 100 additions and 55 deletions
|
@ -10,71 +10,42 @@ function runTests() {
|
|||
|
||||
caption = prepareTest( 'Find Curve Intersections - 10,000 times.', container );
|
||||
|
||||
// pathA = new Path.Circle(new Point(70, 110), 50);
|
||||
// // pathA.rotate( 45 );
|
||||
// pathB = new Path.Circle(new Point(160, 110), 50);
|
||||
// // pathB.rotate( 45 );
|
||||
pathA = new Path.Circle(new Point(70, 110), 50);
|
||||
pathA.rotate( 45 );
|
||||
pathB = new Path.Circle(new Point(160, 110), 50);
|
||||
pathB.rotate( 45 );
|
||||
// pathB.segments[3].point = pathB.segments[3].point.add( [10, -120] );
|
||||
// // pathB.translate( [ -20, -30 ] );
|
||||
// pathB.translate( [ -20, -30 ] );
|
||||
// pathB.segments[0].handleIn = pathB.segments[0].handleIn.add( [-100, 30] );
|
||||
// pathB.translate( [ 20, -30 ] );
|
||||
// // testIntersection( pathA, pathB, caption );
|
||||
// testIntersection( pathA, pathB, caption );
|
||||
|
||||
pathA = new Path();
|
||||
pathA.add( new Segment( [47, 93], [-89, 100], [240, -239] ) );
|
||||
pathA.add( new Segment( [173, 44], [-281, 268], [-86, 152] ) );
|
||||
pathA.closed = true;
|
||||
pathB = pathA.clone();
|
||||
pathB.rotate( -90 );
|
||||
pathA.translate( [-10,0] );
|
||||
pathB.translate( [10,0] );
|
||||
// pathA = new Path();
|
||||
// pathA.add( new Segment( [47, 93], [-89, 100], [240, -239] ) );
|
||||
// pathA.add( new Segment( [173, 44], [-281, 268], [-86, 152] ) );
|
||||
// pathA.closed = true;
|
||||
// pathB = pathA.clone();
|
||||
// pathB.rotate( -90 );
|
||||
// pathA.translate( [-10,0] );
|
||||
// pathB.translate( [10,0] );
|
||||
|
||||
window.a = pathA;
|
||||
window.b = pathB;
|
||||
|
||||
var _p1U = new Path( pathA.segments[0], pathA.segments[1] );
|
||||
// var _p1U = new Path( pathA.segments[1], pathA.segments[2] );
|
||||
// _p1U.reverse();
|
||||
var _p2U = new Path( pathB.segments[0], pathB.segments[1] );
|
||||
_p1U.style = _p2U.style = pathStyleBoolean;
|
||||
var crvs = _p2U.curves;
|
||||
// for (var i = 0; i < crvs.length; i++) {
|
||||
drawFatline( _p1U.curves[0].getValues() );
|
||||
drawFatline( crvs[0].getValues() );
|
||||
// }
|
||||
|
||||
var maxCount = 1, count = maxCount, loc, loc2;
|
||||
var v1 = crvs[0].getValues(), v2 = _p1U.curves[0].getValues();
|
||||
console.time('fatline');
|
||||
while( count-- ){
|
||||
loc = [];
|
||||
Curve.getIntersections2( v1, v2, crvs[0], _p1U.curves[0], loc );
|
||||
// var ret = _clipFatLine( v1, v2, 0, 1, 0, 1, true, crvs[0], _p1U.curves[0], loc );
|
||||
}
|
||||
console.timeEnd('fatline');
|
||||
|
||||
count = maxCount;
|
||||
console.time('paperjs');
|
||||
while( count-- ){
|
||||
loc2 = [];
|
||||
Curve.getIntersections( v1, v2, crvs[0], _p1U.curves[0], loc2 );
|
||||
}
|
||||
console.timeEnd('paperjs');
|
||||
|
||||
console.log( ' ' );
|
||||
for( i =0; i < loc2.length; i++){
|
||||
markPoint( loc2[i].point, ' ', '#00f' );
|
||||
// markPoint( loc2[i].point, loc2[i].parameter, '#00f' );
|
||||
console.log( 'paperjs t = ' + loc2[i].parameter + ' , u = ' + loc2[i].getIntersection().parameter );
|
||||
}
|
||||
for( i =0; i < loc.length; i++){
|
||||
markPoint( loc[i].point, ' ', '#f00' );
|
||||
// markPoint( loc[i].point, loc[i].parameter, '#f00' );
|
||||
console.log( 'fatline t = ' + loc2[i].parameter + ' , u = ' + loc2[i].getIntersection().parameter );
|
||||
}
|
||||
|
||||
view.draw();
|
||||
|
||||
window.setTimeout( function(){doTest( pathA, pathB, 1000 )}, 100 );
|
||||
|
||||
// var v1 = [84.625,79.375,51,130.5,22.5,178,163,44];
|
||||
// var v2 = [91.81412502271213,92.91643774058434,142.93912502271212,126.54143774058434,190.43912502271212,155.04143774058434,56.43912502271214,14.541437740584342]
|
||||
// var c1 = new Curve( 84.625,79.375,51,130.5,22.5,178,163,44 );
|
||||
// var c2 = new Curve( 91.81412502271213,92.91643774058434,142.93912502271212,126.54143774058434,190.43912502271212,155.04143774058434,56.43912502271214,14.541437740584342);
|
||||
// var p1 = new Path( c1.segment1, c1.segment2 );
|
||||
// var p2 = new Path( c2.segment1, c2.segment2 );
|
||||
// p1.style = p2.style = pathStyleBoolean;
|
||||
// var loc = [];
|
||||
// Curve.getIntersections2( v1, v2, c1, c2, loc );
|
||||
// console.log( loc )
|
||||
|
||||
function prepareTest( testName, parentNode ){
|
||||
console.log( '\n' + testName );
|
||||
|
@ -105,6 +76,53 @@ var pathStyleBoolean = {
|
|||
strokeWidth: 1
|
||||
};
|
||||
|
||||
|
||||
function doTest( pathA, pathB, count ){
|
||||
// var _p1U = new Path( pathA.segments[0], pathA.segments[1] );
|
||||
var _p1U = new Path( pathA.segments[1], pathA.segments[2] );
|
||||
// _p1U.reverse();
|
||||
// var _p2U = new Path( pathB.segments[0], pathB.segments[1] );
|
||||
var _p2U = new Path( pathB.segments[3], pathB.segments[0] );
|
||||
_p1U.style = _p2U.style = pathStyleBoolean;
|
||||
// for (var i = 0; i < crvs.length; i++) {
|
||||
// drawFatline( _p1U.curves[0].getValues() );
|
||||
// drawFatline( _p2U.curves[0].getValues() );
|
||||
// }
|
||||
|
||||
var maxCount = count || 1, count = maxCount, loc, loc2;
|
||||
var v1 = _p1U.curves[0].getValues(), v2 = _p2U.curves[0].getValues();
|
||||
console.time('fatline');
|
||||
while( count-- ){
|
||||
loc = [];
|
||||
Curve.getIntersections2( v1, v2, _p1U.curves[0], _p2U.curves[0], loc );
|
||||
// Curve.getIntersections2( v2, v1, _p2U.curves[0], _p1U.curves[0], loc );
|
||||
// var ret = _clipFatLine( v1, v2, 0, 1, 0, 1, true, crvs[0], _p1U.curves[0], loc );
|
||||
}
|
||||
console.timeEnd('fatline');
|
||||
|
||||
count = maxCount;
|
||||
console.time('paperjs');
|
||||
while( count-- ){
|
||||
loc2 = [];
|
||||
Curve.getIntersections( v1, v2, _p1U.curves[0], _p2U.curves[0], loc2 );
|
||||
}
|
||||
console.timeEnd('paperjs');
|
||||
|
||||
console.log( ' ' );
|
||||
for( i =0; i < loc2.length; i++){
|
||||
markPoint( loc2[i].point, ' ', '#00f' );
|
||||
// markPoint( loc2[i].point, loc2[i].parameter, '#00f' );
|
||||
console.log( 'paperjs t = ' + loc2[i].parameter + ' , u = ' + loc2[i].getIntersection().parameter );
|
||||
}
|
||||
for( i =0; i < loc.length; i++){
|
||||
// console.log( loc[i].point )
|
||||
markPoint( loc[i].point, ' ', '#f00' );
|
||||
// markPoint( loc[i].point, loc[i].parameter, '#f00' );
|
||||
console.log( 'fatline t = ' + loc[i].parameter + ' , u = ' + loc[i].getIntersection().parameter );
|
||||
}
|
||||
view.draw();
|
||||
}
|
||||
|
||||
// Better if path1 and path2 fit nicely inside a 200x200 pixels rect
|
||||
function testIntersection( path1, path2, caption ) {
|
||||
try{
|
||||
|
@ -174,6 +192,33 @@ function markPoint( pnt, t, c, tc, remove ) {
|
|||
}
|
||||
}
|
||||
|
||||
function markCurve( crv, c, flag) {
|
||||
if( !crv ) return;
|
||||
c = c || '#000';
|
||||
if( flag ){
|
||||
if( window.__p1 ) window.__p1.remove();
|
||||
window.__p1 = new Path(
|
||||
new Segment( [crv[0], crv[1]], null, [crv[2] - crv[0], crv[3] - crv[1]] ),
|
||||
new Segment( [crv[6], crv[7]], [crv[4] - crv[6], crv[5] - crv[7]], null )
|
||||
);
|
||||
window.__p1.style.strokeColor = c;
|
||||
} else {
|
||||
if( window.__p2 ) window.__p2.remove();
|
||||
window.__p2 = new Path(
|
||||
new Segment( [crv[0], crv[1]], null, [crv[2] - crv[0], crv[3] - crv[1]] ),
|
||||
new Segment( [crv[6], crv[7]], [crv[4] - crv[6], crv[5] - crv[7]], null )
|
||||
);
|
||||
window.__p2.style.strokeColor = c;
|
||||
}
|
||||
view.draw();
|
||||
}
|
||||
|
||||
function waitFor( time ){
|
||||
var st = new Date();
|
||||
while( new Date() - st < time ){}
|
||||
}
|
||||
|
||||
|
||||
function annotatePath( path, t, c, tc, remove ) {
|
||||
if( !path ) return;
|
||||
var crvs = path.curves;
|
||||
|
|
Loading…
Reference in a new issue