One simple way should be to “colour” the areas as A,B,C [1]. Assuming you are working with a bitmap, just use a 1:1 2d char array to represent it, with the values in each cell corresponding to each pixel in the image. You can then check the value corresponding to the point that was clicked. This can be simplified by instead mapping it 1.5:1 or 2:1 or further, depending upon your requirement.
Another way: If you are using an SVG, you are probably going for lower RAM usage and don’t want an array as big as the image dimensions, in which case, you will have to assign sides of every separate path (border). Then whenever there is a click, you can calculate where it lies WRT each path. This would be more useful if the number of paths is low, but you care more about user clicking very close to the borders and you are using higher precision when calculating the position. But having too many border paths will make the computation time longer, while at least keeping the total RAM usage low.
Please note, I have never implemented a solution to this before and just gave the first solution I came up with, that I thought, could be done using C++. You might want to wait for ppl with higher XP to give their answers, or if this is a competition problem, their recommended answer.
Using an algorithm similar to what the Fill Paint tools use ↩︎
One simple way should be to “colour” the areas as A,B,C [1]. Assuming you are working with a bitmap, just use a 1:1 2d
char
array to represent it, with the values in each cell corresponding to each pixel in the image. You can then check the value corresponding to the point that was clicked. This can be simplified by instead mapping it 1.5:1 or 2:1 or further, depending upon your requirement.Another way: If you are using an SVG, you are probably going for lower RAM usage and don’t want an array as big as the image dimensions, in which case, you will have to assign sides of every separate path (border). Then whenever there is a click, you can calculate where it lies WRT each path. This would be more useful if the number of paths is low, but you care more about user clicking very close to the borders and you are using higher precision when calculating the position. But having too many border paths will make the computation time longer, while at least keeping the total RAM usage low.
Please note, I have never implemented a solution to this before and just gave the first solution I came up with, that I thought, could be done using C++. You might want to wait for ppl with higher XP to give their answers, or if this is a competition problem, their recommended answer.
Using an algorithm similar to what the Fill Paint tools use ↩︎