mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
convex hull code working
This commit is contained in:
parent
f08bc9b752
commit
c10f6373a4
1 changed files with 16 additions and 11 deletions
|
@ -63,7 +63,14 @@ function runTests() {
|
|||
}
|
||||
|
||||
|
||||
convexhull( 20, 30 , 3 , -20 );
|
||||
convexhull( 20, -30 , -30 , -20, 250, 80 );
|
||||
convexhull( 20, -30 , -10 , -20, 250, 180 );
|
||||
convexhull( 20, -15 , -40 , -20, 500, 80 );
|
||||
convexhull( 20, -5 , -40 , -20, 500, 180 );
|
||||
// convexhull( 20, 30 , 10 , -20, 250, 80 );
|
||||
// convexhull( 20, 30 , 2 , -20, 250, 180 );
|
||||
// convexhull( 20, 10 , 30 , -20, 500, 80 );
|
||||
// convexhull( 20, 30 , 30 , -20, 500, 180 );
|
||||
|
||||
view.draw();
|
||||
|
||||
|
@ -143,7 +150,7 @@ function markIntersections( ixs ){
|
|||
}
|
||||
}
|
||||
|
||||
function convexhull( dq0, dq1, dq2, dq3 ){
|
||||
function convexhull( dq0, dq1, dq2, dq3 , x, y){
|
||||
// Prepare the convex hull for D(ti, di(t))
|
||||
var distq1 = _getSignedDist( 0.0, dq0, 1.0, dq3, 0.3333333333333333, dq1 );
|
||||
var distq2 = _getSignedDist( 0.0, dq0, 1.0, dq3, 0.6666666666666666, dq2 );
|
||||
|
@ -161,7 +168,6 @@ function convexhull( dq0, dq1, dq2, dq3 ){
|
|||
distq1 = Math.abs(distq1);
|
||||
distq2 = Math.abs(distq2);
|
||||
var vqa1a2x, vqa1a2y, vqa1Maxx, vqa1Maxy, vqa1Minx, vqa1Miny;
|
||||
console.log( " distq1 = " + distq1 + " , distq2 = " + distq2 )
|
||||
if( distq1 > distq2 ){
|
||||
dqapex1 = [ 1.0, dq3 ];
|
||||
dqapex2 = [ 0.0, dq0 ];
|
||||
|
@ -174,20 +180,19 @@ function convexhull( dq0, dq1, dq2, dq3 ){
|
|||
dqmax = [ 0.6666666666666666, dq2 ];
|
||||
}
|
||||
// vector dqapex1->dqapex2
|
||||
var vqa1a2x = dqapex1[0] - dqapex2[0], vqa1a2y = dqapex1[1] - dqapex2[1];
|
||||
vqa1a2x = dqapex1[0] - dqapex2[0]; vqa1a2y = dqapex1[1] - dqapex2[1];
|
||||
// vector dqapex1->dqmax
|
||||
var vqa1Maxx = dqapex1[0] - dqmax[0], vqa1Maxy = dqapex1[1] - dqmax[1];
|
||||
vqa1Maxx = dqapex1[0] - dqmax[0]; vqa1Maxy = dqapex1[1] - dqmax[1];
|
||||
// vector dqapex1->dqmin
|
||||
var vqa1Minx = dqapex1[1] - dqmin[0], vqa1Miny = dqapex1[1] - dqmin[1];
|
||||
vqa1Minx = dqapex1[0] - dqmin[0]; vqa1Miny = dqapex1[1] - dqmin[1];
|
||||
// compare cross products of these vectors to determine, if
|
||||
// point is in triangles [ dq3, dqMax, dq0 ] or [ dq0, dqMax, dq3 ]
|
||||
var vcrossa1a2_a1Max = vqa1a2x * vqa1Maxy - vqa1a2y * vqa1Maxx;
|
||||
var vcrossa1a2_a1Min = vqa1a2x * vqa1Miny - vqa1a2y * vqa1Minx;
|
||||
var vcrossa1Max_a1Min = vqa1Maxx * vqa1Miny - vqa1Maxy * vqa1Minx;
|
||||
var sign1 = signum( vcrossa1Max_a1Min );
|
||||
var sign2 = signum( vcrossa1a2_a1Min );
|
||||
console.log( vcrossa1a2_a1Max, vcrossa1a2_a1Min, vcrossa1Max_a1Min )
|
||||
if( sign1 === sign2 ){
|
||||
var sign2 = signum( vcrossa1a2_a1Max );
|
||||
if( sign1 !== sign2 ){
|
||||
// Point [2/3, dq2] is inside the triangle and the convex hull is a triangle
|
||||
Dt = [
|
||||
[ 0.0, dq0, dqmax[0], dqmax[1] ],
|
||||
|
@ -210,8 +215,8 @@ function convexhull( dq0, dq1, dq2, dq3 ){
|
|||
[ 0.6666666666666666, dq2, 1.0, dq3 ],
|
||||
[ 1.0, dq3, 0.0, dq0 ]
|
||||
];
|
||||
var yscale = 2;
|
||||
var x = 500, y = 110;
|
||||
var yscale = 1;
|
||||
// var x = 500, y = 110;
|
||||
for (var i = 0; i < Dt.length; i++) {
|
||||
var pth = new Path.Line( new Point( x + Dt[i][0] * 190, y + Dt[i][1] * yscale ),
|
||||
new Point( x + Dt[i][2] * 190, y + Dt[i][3] * yscale ) );
|
||||
|
|
Loading…
Reference in a new issue