this post was submitted on 13 Jan 2025
26 points (96.4% liked)
Learn Programming
1684 readers
22 users here now
Posting Etiquette
-
Ask the main part of your question in the title. This should be concise but informative.
-
Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.
-
Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.
-
Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/
Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
One simple way should be to "colour" the areas as A,B,C ^[Using an algorithm similar to what the Fill Paint tools use]. 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.