mouseOver lines on wxBitmap Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

mouseOver lines on wxBitmap

Post by mael15 »

Hi everyone!
I have an image of a cogwheel in my app that the user can zoom in:
zahnradLinien.jpg
zahnradLinien.jpg (12.91 KiB) Viewed 715 times
I want to enable the user to click and select one of these lines. What would be the best way? I thought about saving a std::vector of the wxRect around each line during the paint process and loop the mouse position through it to see if one contains it. But the wxRect overlap quite a lot. Ideal would be a rotated wxRect, but that might result in a lot of calculations?
Any better idea?
Thanx!
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: mouseOver lines on wxBitmap

Post by doublemax@work »

I'd use a wxRect for a fast check, and a wxRegion (or a pure calculated method) for a second, precise check. Depending on how many lines you have - and it looks like there could be many - you might need a tree structure with nested rectangles for better performance.
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: mouseOver lines on wxBitmap

Post by mael15 »

Sounds good, thanx! I would say there will be max 4000 lines. You think wxRegion is much slower than wxRect? To use both like you suggested, I would have to fill and loop at least two vectors. I would tend to only use wxRegion, what do you think?
A nice pre test would be if the mouse is between the inner and outer radius of the cogs. Is there built in way to test it or do I have to calculate it myself?
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: mouseOver lines on wxBitmap

Post by doublemax@work »

mael15 wrote: Tue Oct 06, 2020 1:45 pmYou think wxRegion is much slower than wxRect?
It *is* much slower. Actually it uses a list of rectangles internally. In worst case this will be many rectangles with 1 pixel height.
(Edit: that's when you use a bitmap to create the region. I don't know what happens if you use vector points and i can't check this atm)
mael15 wrote: Tue Oct 06, 2020 1:45 pmA nice pre test would be if the mouse is between the inner and outer radius of the cogs. Is there built in way to test it or do I have to calculate it myself?
You'd have to calculate that yourself.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: mouseOver lines on wxBitmap

Post by doublemax »

After a look into the sources, it seems that at least under Windows there's dedicated platform specific code that creates a region from a polygon without rendering it into a bitmap. So it's possible that at least under Windows a wxRegion can be both small and fast. I didn't make any performance tests, but it might be worth a try.
Use the source, Luke!
mael15
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 539
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: mouseOver lines on wxBitmap

Post by mael15 »

doublemax wrote: Tue Oct 06, 2020 9:54 pm After a look into the sources, it seems that at least under Windows there's dedicated platform specific code that creates a region from a polygon without rendering it into a bitmap. So it's possible that at least under Windows a wxRegion can be both small and fast. I didn't make any performance tests, but it might be worth a try.
Thank you for taking the time! Since my app only runs on Windows, I will look into it. I have a first test if the mouse is within outer and inner radius and ordered the lines´ wxRects by x coordinate so I can access and leave the vector efficiently. Maybe using wxRect is fast enough and overlapping rects are not as bad as I thought.
Post Reply