I have this question and I am struggling to find an approach, but I can't find any so far. If someone has a solution, please share it with me.
So, I have this simple code that extracts from a .png image only the pixels that are not transparent, and then draws points every 5 pixels with color taken from that specific area of the image. So far so good, no harm caused.
However, I would like to be able to control the position of each point with motion algorithms based on interaction with the user (i.e. the points start moving when the mouse is closer). How can I control ONLY the points that are drawn from the image - lets assume there are just 200 points that have been created and I want each one to be controlled by the motion algorithms?
I understand the process of creating the PVector and applying an ArrayList afterwards, however my main question remains:
How can I gain access ONLY to the position of the pixels of the .png image that they are not transparent 100%? I don't want to create points that correspond to the position of the transparent pixels of the .png image. I can't find a way to isolate the transparent pixels yet.
I don't understand your issue. You say you draw some 200 points taken out of the image. While you draw them (or actually before, in setup()), just store them in the array list, then use this list to draw the points and move them. If you need to store the color as well, you can make a class distinct from PVector, that you make yourself, something like:
int x, y;
// Can add other info like original location, if needed
Ok based on the code I posted earlier, I get the following image. I draw points only to the pixel positions that there is color and alpha values. However, I can't find a way to pass ONLY the x,y values of those pixels, and drop the rest pixels that exist in the image (i.e. the transparent pixels). Can I do a conditional statement and say, ok if the alpha value of the pixel == 0, then add this position to the ArrayList? But then I need to have the total x,y positions that have been added to the ArrayList in order to create a for loop to initialize the motion algorithm. Can this happen, or there is another better way? I also wonder how I can use for example 200 separate pixels that get a random pixel position from the available ones that have been added to the ArrayList.
Apologies for these rather simple questions, but it is some information I can't find anywhere - I have looked in various forums, books, websites.
Thanks for your answer, I'll work on this example to see if I can get the results I want - although I was doing something similar. One difference is that I was initializing the ArrayList inside the setup function, but outside the for loop - I don't know if there is any difference.
Also I don't understand why you use the <> symbols in the following line:
ArrayList<SparsePoint> sps = new ArrayList<SparsePoint>();
and if you would like to explain how this line inside draw() works:
No difference for array list init, they are both OK. The <> indicates to the compiler that the array list contains only SparsePoints. So you don't need to cast them back when using get(). And it allows to use the for-each loop. for (SparsePoint sp : sps) is the Java 1.5 syntax, recently introduced in Processing (the <> too). It basically says: iterate on the sps collection, and put each element in the sp variable of type SparsePoint. It is equivalent, but much terser, than a for loop with i index, using sps.get(i).
Leave a comment on PhiLho's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic