We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Pages: 1 2 
returns (Read 1098 times)
returns
Mar 18th, 2009, 3:48am
 
hello,
i drew a map with different districts on different layers in an svg file. each district has its own unique color and is a different odd shape.

i am trying draw a dot into a district onto a map, which i have each defined a bounding box area...but before i draw that dot to the district on the map, i would like to check if that dot is sitting in the color of that area and not in a non-colored area within the bounding box.

am i not using the correct syntax?

this is part of my code below. thanks in advance!

/*note

the "grp" is the svg file of my map (grp = RG.loadShape("data/sf-map2hoods.svg");), where each district on the map is on its own layer with its own color.

>>> color neighborhoodColor = grp.get(xpos, ypos);

*/



//constructor
 DrawDot (int h, int c, String w, String s, String sx, String u){  
   hood = h; //assign areas to hood  

   
   //get a random coordinate for the dot....
   xpos = int(random(lowerBoundX[hood], upperBoundX[hood])); //width. reads the district number and gets the assigned coordinates and dimentions
   ypos = int(random(lowerBoundY[hood], upperBoundY[hood])); //height  
   
   
  //...while the dot is not in it's correct area, keep checking    
    while (IsInNeighborhood == false){ //while the dot is not in the correct place...
 IsInHood(xpos, ypos);   //send x&ypos to function to check to see if the x & y positions are in the correct area
   
    if(IsInNeighborhood == true){  //if the dot is in the correct place...
   //...assign the spot!
   xpos = random(lowerBoundX[hood], upperBoundX[hood]); //width. reads the district number and gets the assigned coordinates and dimentions
   ypos = random(lowerBoundY[hood], upperBoundY[hood]); //height  
}//end if true
   }//end while false
 
   //check to see if dot is in the coorect  
   int IsInHood(int xpos, int ypos){ //passing x & y to check if they are sitting in the hood    
color neighborhoodColor = sfmap.get(xpos, ypos);  //read a point hood color  
return(neighborhoodColor); //return the hood color  
//return(xpos, ypos); //return the hood color??  
IsInNeighborhood == true;
   }
 
    } //end constructor
Re: returns
Reply #1 - Mar 18th, 2009, 8:06am
 
You can't put a function inside a function (this is not Pascal language! Wink I wished yesterday to be able to do that, though).

You can't put instructions after a non-conditional return, because these lines will never be executed.

You should rely on the return value, not on a semi-global variable.

In your code, if you find a correct place... you draw another random place! Smiley

Let see how we could do that.

//constructor
DrawDot(int hood, int c, String w, String s, String sx, String u){  
 do {    
   //get a random coordinate for the dot....
   int xpos = int(random(lowerBoundX[hood], upperBoundX[hood])); //width. reads the district number and gets the assigned coordinates and dimentions
   int ypos = int(random(lowerBoundY[hood], upperBoundY[hood])); //height  

  //...while the dot is not in it's correct area, keep checking    
 } while (!IsInHood(xpos, ypos));
 
} //end constructor

   //check to see if dot is in the coorect  
int IsInHood(int xpos, int ypos){ //passing x & y to check if they are sitting in the hood    
color neighborhoodColor = sfmap.get(xpos, ypos);  //read a point hood color  
return neighborhoodColor == requestedColor; //return the hood color  
}
Re: returns
Reply #2 - Mar 18th, 2009, 8:32am
 
haha. only if. thanks! i'm getting an error that says "The operator ! is undefined for the argument types(s) int"? I tried doing something like this : (!(IsInHood(xpos, ypos))); , but that doesn't seem to work either. for the return, do i just want to "return neighborhoodColor"? Not sure what to == it to?

   do{
   //get a random coordinate for the dot....
   xpos = int(random(lowerBoundX[hood], upperBoundX[hood]));
   ypos = int(random(lowerBoundY[hood], upperBoundY[hood]));
   } while (!IsInHood(xpos, ypos)); //while the dot is not in the correct place...
 }//end constructor
       
   //check to see if dot is in the coorect  
   int IsInHood(int xpos, int ypos){ //passing x & y to check if they are sitting in the hood  
     color neighborhoodColor = grp.get(xpos, ypos);  //read a point hood color
     return neighborhoodColor==hood; //NOT SURE WHAT DO DO HERE?
   }
Re: returns
Reply #3 - Mar 18th, 2009, 9:50am
 
Sorry, it was a hasty fix, the return value of IsInHood must be boolean, of course:

boolean IsInHood(int xpos, int ypos)

Is your hood variable holding a color? If so, your bound arrays will be big! Otherwise your test won't work.
Re: returns
Reply #4 - Mar 18th, 2009, 10:10am
 
thanks, no worries at all!

my hood is an array of neighborhoods. they are all read as ints, so each dot that i want to put on the map has its own assigned neighborhood. so say this dot belongs in neighborhood "1". does that make sense?


now i'm getting:
"The function get(int,int) does not exist." ?

   //check to see if dot is in the coorect  
   boolean IsInHood(int xpos, int ypos){ //passing x & y to check if they are sitting in the hood  
     color neighborhoodColor = grp.get(xpos, ypos);  //read a point hood color
     return neighborhoodColor;
   }
Re: returns
Reply #5 - Mar 18th, 2009, 10:20am
 
these are my bounding boxes

//int   [] neighborhoo = {  1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37}; //neighborhood

 float [] lowerBoundX = {423,  53, 286, 478, 373, 291, 275, 414, 217, 482, 338, 275, 363,  61, 213, 194, 168, 365, 426, 323, 321, 421, 424, 328, 426, 321, 412, 486,  49, 416,  53, 437, 580, 299, 407, 196, 355}; //x position

 float [] upperBoundX = {579, 357, 395, 682, 501, 432, 339, 503, 433, 548, 424, 364, 424, 324, 272, 274, 326, 423, 485, 416, 422, 504, 484, 427, 533, 426, 508, 608, 271, 469, 217, 478, 653, 366, 516, 356, 420}; //width

 float [] lowerBoundY = {181, 243, 262, 400, 406, 298, 321, 203, 493, 167, 445, 287, 264, 472, 231, 323,  99, 296, 196, 213, 108, 306, 176, 381, 105, 170, 480, 266, 201, 124, 324, 229, 120, 363, 547, 340, 236};  //y position

 float [] upperBoundY = {311, 326, 296, 618, 494, 384, 358, 295, 619, 223, 500, 335, 301, 619, 287, 436, 284, 308, 210, 245, 182, 445, 202, 448, 177, 224, 571, 405, 287, 182, 482, 261, 247, 481, 618, 540, 273};//height
Re: returns
Reply #6 - Mar 18th, 2009, 10:44am
 
Mmm, let summarize.

IsInHood is a function returning a boolean, true or false. It must not return a color, it must compare color at given coordinates to expected color. Maybe you must also pass the expected color as parameter of the function.
You need probably another array, the expected color corresponding to the defined bounds.

I overlooked another fact: grp is a PShape. It doesn't has a get(int, int) method, indeed. You need to render the PShape on a PGraphics and get the pixel color value from this image.
Re: returns
Reply #7 - Mar 18th, 2009, 6:47pm
 
i'm not sure how to convert that? i'm using this library for to read the shapes (and would like to read the colors) from an svg file: http://www.ricardmarxer.com/processing/geomerative/documentation/index.htm

this is what it looks like in my code:
 //geomerative declarations
 RShape grp;
 boolean ignoringStyles = false;

 // in setup
 RG.init(this);
 RG.ignoreStyles(ignoringStyles);  
 RG.setPolygonizer(RG.ADAPTATIVE);
 grp = RG.loadShape("data/sf-map2hoods.svg");
 RG.ignoreStyles(true);



cool, below is actually what i had in my code. i think the code below that is getting closer, but i'm having that problem with that library conversion?

color [] colorValue = {
-3428766, -2832210, -12245468, -958685, -14054845, -6105686, -8887770, -963805, -995293, -6648767, -1386640, -6413694, -5491, -12693342, -12756825, -5365981, -2734951, -9089715, -7373896, -5713012, -6074210, -1236855, -3089107, -9516070, -2527580, -9653030, -331374, -9125535, -806009, -13816710, -2104801, -11680299, -5057447, -8086671, -3392227, -9949921, -80384
};


   do{
       //get a random coordinate for the dot....
       xpos = int(random(lowerBoundX[hood], upperBoundX[hood]));
       ypos = int(random(lowerBoundY[hood], upperBoundY[hood]));
   }while (!IsInHood(xpos, ypos));

 }//end constructor
       
   //check to see if dot is in the correct  
   boolean IsInHood(int xpos, int ypos){
   //passing x & y to check if they are sitting in the hood  
   color neighborhoodColor = sfmap.get(xpos, ypos);  
   //read a point hood color and assign    
     if(neighborhoodColor == colorValue[hood]){    
   //if that color = the correct color, return true    
     }
     return true;
   }



thanks for your help again!
Re: returns
Reply #8 - Mar 19th, 2009, 10:06am
 
rrrr wrote on Mar 18th, 2009, 6:47pm:
i'm having that problem with that library conversion
What problem

   }while (!IsInHood(xpos, ypos));

make that

   }while (!IsInHood(xpos, ypos, hood));

and

   boolean IsInHood(int xpos, int ypos){
   color neighborhoodColor = sfmap.get(xpos, ypos);  
     if(neighborhoodColor == colorValue[hood]){    
     }
     return true;
   }

Uh hood isn't known there. Should be:

   boolean IsInHood(int xpos, int ypos, int hood){
     color neighborhoodColor = sfmap.get(xpos, ypos);  
     return neighborhoodColor == colorValue[hood];
   }

And if you are uneasy with boolean expressions, you can write:

 if (neighborhoodColor == colorValue[hood])
   return true;
 return false;

but I find this less elegant.
Re: returns
Reply #9 - Mar 19th, 2009, 10:21am
 
oh, i was referring to what you wrote earlier:

"I overlooked another fact: grp is a PShape. It doesn't has a get(int, int) method, indeed. You need to render the PShape on a PGraphics and get the pixel color value from this image."

i'm using that library that reads this:

//geomerative declarations
 RShape grp;
 boolean ignoringStyles = false;

 // in setup
 RG.init(this);
 RG.ignoreStyles(ignoringStyles);  
 RG.setPolygonizer(RG.ADAPTATIVE);
 grp = RG.loadShape("data/sf-map2hoods.svg");
 RG.ignoreStyles(true);


i am using this library to read the different colored shapes from the different layers in my svg file.

i'm actually using grp instead of sfmap, i just forgot to change it in the code i posted:

color neighborhoodColor = grp.get(xpos, ypos);
Re: returns
Reply #10 - Mar 20th, 2009, 7:14pm
 
so i'm still using the geomerative library: (http://www.ricardmarxer.com/processing/geomerative/documentation/rpoint_class_rpoint.htm) and trying to do something like this get (http://processing.org/reference/get_.html)

i'm getting an error saying: "cannot convet from boolean to int" on the "color neighborhoodColor = grp.children[hood].contains(p);" line.

it's almost there, but not quite :(

do{ xpos = random(lowerBoundX[hood], upperBoundX[hood]);
   ypos = random(lowerBoundY[hood], upperBoundY[hood]);
 }while (!IsInHood(xpos, ypos, hood));
       
boolean IsInHood(float xpos, float ypos, int hood){  
 RPoint p = new RPoint(xpos, ypos);
 color neighborhoodColor = grp.children[hood].contains(p);
 return neighborhoodColor == colorValue[hood];
   }
Re: returns
Reply #11 - Mar 20th, 2009, 7:32pm
 
grp.children[hood].contains(p) will return either true or false, not a colour.
Re: returns
Reply #12 - Mar 20th, 2009, 7:56pm
 
thanks. i just don't know how i can write it like "color cp = get(30, 20);" but using the syntax of the geomerative library with my svg file, which i'm trying to read the color of each shape in a different bounding box. if the xpos and ypos matches a specific color within that bounding box, and not some other color in that box, then return true.


Re: returns
Reply #13 - Mar 20th, 2009, 9:53pm
 
As I wrote earlier, I think you need to render/rasterize the shape to an image, so you can get the color value of pixels there.
Re: returns
Reply #14 - Mar 28th, 2009, 1:46pm
 
//Here is a rough set up of what I have now:

PImage sfmap;

void setup() {
 sfmap = loadImage("data/sf-map2hoods.png");
}

//below is a section of a class i have

do{ //get a random coordinate for the dot....
       xpos = int(random(lowerBoundX[hood], upperBoundX[hood]));
       ypos = int(random(lowerBoundY[hood], upperBoundY[hood]));
       println("LowerX/UpperX: " + lowerBoundX[hood] + ", " + upperBoundX[hood]);
       println("LowerY/UpperY: " + lowerBoundY[hood] + ", " + upperBoundY[hood]);
       println("RandomPlaceLocation: " + xpos + ", " + ypos);  
       println("test test test");
   }while (!IsInHood(xpos, ypos, hood)); //while the dot is not in the correct place...

  //is this necessary for after the points are found?
   //if (IsInHood(xpos, ypos, hood)){
   //  x = xpos;
   // y = ypos;
   //}

 }//end constructor
     
 boolean IsInHood(int xpos, int ypos, int hood){
    color neighborhoodColor = sfmap.get(xpos, ypos);
    println("i am returning");
    return neighborhoodColor == colorValue[hood];
  }

//i'm getting a "java.lang.NullPointerException" and it doesn't seem to be running through the boolean function cause it's not println("i am returning"). it's getting a random xpos and ypos in the do{}while, but i don't think it's checking to see if that point matches the color of that area? i thought i could use "sfmap.get" just like sfmap.pixel"?

Pages: 1 2