We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
Is there any way to select a custom shape in Processing (using a mouse press). I have created a simple program which outlines an simple irregular shape using vertex(); within beginShape(); and endShape(); which has over 50 vertex() declarations. I want to be able to select within this shape and change it's colour after a mouse click. Is there any possible way of doing this?
So far, I have only been able to do this within shapes such as a rectangle or eclipse.
Thanks
Answers
the trick is to get the color on the mouse click pos (get(mouseX,mouseY);)
check for each shape if its color is == the color of the mouse pos
(when each shape has another color this approach works
when not so you can paint the shapes on a 2nd hidden canvas with unique colors and do the check there)
;-)
here
The answer did solve the problem as I asked, but maybe I shouldn't of overcomplicated my question by using the color example, I simply want to identify when the mouse is pressed within the vertex object. Although your example worked perfectly, it depended on the color of the object, whereas my program contains 10s of objects which are the same color, but I would like to identify when each is pressed, changing the interface depending on which is pressed.
i.e
Understood
Use 2 different colors for each shape:
One col to display
One col to draw on the 2nd hidden canvas - do get() there
I still think this is easiest
If you draw a line from the click point to a known point outside the object and count the number of edges crossed you can determine if it is inside or outside. An even count is outside object, odd count is inside.
As I do have a limited number of shapes, the colour solution is probably the best anyway and I'll give it a try and let you know how successful I am with it! Thanks!
Like so
The objects are fairly close and some share edges so theres no way, as far as I can see, I could identify which object the point is within, just that it is within one of them. The color solution seems to work, however the application fails to identify the color underneath the above. i.e I've coloured the visible layer (0,0,255), and the hidden mask behind (0,255,255), yet it is only identified when I define:
not the ideal:
feels like I'm missing something fairly obvious.
For hidden canvas see PGraphics...
See quarks answer on my last post
http://forum.processing.org/two/discussion/comment/21248#Comment_21248
Given the "mouse click to point outside" line you can for each object check to see if the line intersect the edges defined by the vertex, counting the number of edges. If the shapes overlap then the click could be inside multiple shapes. If so, you need to appreciate the stack order, the object on top is the one clicked.
See http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect for a discussion of detecting line segment intersections.
In line 1 you use get alone
But you must use pg.get....
To get the right color 0,255,255
Pg and your visible canvas must be same size etc.
If you store the vertices in an array of PVectors then you can use this method to see whether a point is inside or not.