please help me with this array?
in
Programming Questions
•
6 months ago
I'm trying to get this to work, but i keep getting this error in sketch "cannot find anything named collections" am i missing something? the bit of code in question is the last bit right at the bottom
-
// an extended polygon class with my own customized createPolygon() method (feel free to improve!)// Courtesy Amnon Owed
class PolygonBlob extends Polygon {
// took me some time to make this method fully self-sufficient// now it works quite well in creating a correct polygon from a person's blob// of course many thanks to v3ga, because the library already does a lot of the workvoid createPolygon () {// an arrayList... of arrayLists... of PVectors// the arrayLists of PVectors are basically the person's contours (almost but not completely in a polygon-correct order)ArrayList < ArrayList < PVector >> contours = new ArrayList < ArrayList < PVector >>();// helpful variables to keep track of the selected contour and point (start/end point)int selectedContour = 0 ;int selectedPoint = 0 ;
// create contours from blobs// go over all the detected blobsfor ( int n = 0 ; n < theBlobDetection . getBlobNb (); n ++) {Blob b = theBlobDetection . getBlob ( n );// for each substantial blob...if ( b != null && b . getEdgeNb () > 100 ) {// create a new contour arrayList of PVectorsArrayList < PVector > contour = new ArrayList < PVector >();// go over all the edges in the blobfor ( int m = 0 ; m < b . getEdgeNb (); m ++) {// get the edgeVertices of the edgeEdgeVertex eA = b . getEdgeVertexA ( m );EdgeVertex eB = b . getEdgeVertexB ( m );// if both ain't null...if ( eA != null && eB != null ) {// get next and previous edgeVertexAEdgeVertex fn = b . getEdgeVertexA (( m + 1 ) % b . getEdgeNb ());EdgeVertex fp = b . getEdgeVertexA (( max ( 0 , m - 1 )));// calculate distance between vertexA and next and previous edgeVertexA respectively// positions are multiplied by kinect dimensions because the blob library returns normalized valuesfloat dn = dist ( eA . x * kinectWidth , eA . y * kinectHeight , fn . x * kinectWidth , fn . y * kinectHeight );float dp = dist ( eA . x * kinectWidth , eA . y * kinectHeight , fp . x * kinectWidth , fp . y * kinectHeight );// if either distance is bigger than 15if ( dn > 15 || dp > 15 ) {// if the current contour size is bigger than zeroif ( contour . size () > 0 ) {// add final pointcontour . add ( new PVector ( eB . x * kinectWidth , eB . y * kinectHeight ));// add current contour to the arrayListcontours . add ( contour );// start a new contour arrayListcontour = new ArrayList < PVector >();// if the current contour size is 0 (aka it's a new list)} else {// add the point to the listcontour . add ( new PVector ( eA . x * kinectWidth , eA . y * kinectHeight ));}// if both distance are smaller than 15 (aka the points are close)} else {// add the point to the listcontour . add ( new PVector ( eA . x * kinectWidth , eA . y * kinectHeight ));}}}}}// at this point in the code we have a list of contours (aka an arrayList of arrayLists of PVectors)// now we need to sort those contours into a correct polygon. To do this we need two things:// 1. The correct order of contours// 2. The correct direction of each contour
// as long as there are contours left...while ( contours . size () > 0 ) {// find next contourfloat distance = 999999999 ;// if there are already points in the polygonif ( npoints > 0 ) {// use the polygon's last point as a starting pointPVector lastPoint = new PVector ( xpoints [ npoints - 1 ], ypoints [ npoints - 1 ]);// go over all contoursfor ( int i = 0 ; i < contours . size (); i ++) {ArrayList < PVector > c = contours . get ( i );// get the contour's first pointPVector fp = c . get ( 0 );// get the contour's last pointPVector lp = c . get ( c . size ()- 1 );// if the distance between the current contour's first point and the polygon's last point is smaller than distanceif ( fp . dist ( lastPoint ) < distance ) {// set distance to this distancedistance = fp . dist ( lastPoint );// set this as the selected contourselectedContour = i ;// set selectedPoint to 0 (which signals first point)selectedPoint = 0 ;}// if the distance between the current contour's last point and the polygon's last point is smaller than distanceif ( lp . dist ( lastPoint ) < distance ) {// set distance to this distancedistance = lp . dist ( lastPoint );// set this as the selected contourselectedContour = i ;// set selectedPoint to 1 (which signals last point)selectedPoint = 1 ;}}// if the polygon is still empty} else {// use a starting point in the lower-rightPVector closestPoint = new PVector ( width , height );// go over all contoursfor ( int i = 0 ; i < contours . size (); i ++) {ArrayList < PVector > c = contours . get ( i );// get the contour's first pointPVector fp = c . get ( 0 );// get the contour's last pointPVector lp = c . get ( c . size ()- 1 );// if the first point is in the lowest 5 pixels of the (kinect) screen and more to the left than the current closestPointif ( fp . y > kinectHeight - 5 && fp . x < closestPoint . x ) {// set closestPoint to first pointclosestPoint = fp ;// set this as the selected contourselectedContour = i ;// set selectedPoint to 0 (which signals first point)selectedPoint = 0 ;}// if the last point is in the lowest 5 pixels of the (kinect) screen and more to the left than the current closestPointif ( lp . y > kinectHeight - 5 && lp . x < closestPoint . y ) {// set closestPoint to last pointclosestPoint = lp ;// set this as the selected contourselectedContour = i ;// set selectedPoint to 1 (which signals last point)selectedPoint = 1 ;}}}
// add contour to polygonArrayList < PVector > contour = contours . get ( selectedContour );// if selectedPoint is bigger than zero (aka last point) then reverse the arrayList of pointsif ( selectedPoint > 0 ) { Collections . reverse ( contour ); }// add all the points in the contour to the polygonfor ( PVector p : contour ) {addPoint ( int ( p . x ), int ( p . y ));}// remove this contour from the list of contourscontours . remove ( selectedContour );// the while loop above makes all of this code loop until the number of contours is zero// at that time all the points in all the contours have been added to the polygon... in the correct order (hopefully)}}}
1