What is the way to detect mouse clicks on object with cairo

what are the ways to implement collision detection I mean how can I make cairo context detect my clicks on objects

I mean howto implement picking which is essential for creating custom widgets.

Cairo offers only drawing functionality. You may draw a rectangle, but that rectangle is not an object for cairo. Only four lines. I used cairo to draw on a GtkDrawingArea. The GtkDrawingArea offers functions to catch mouse events, and you then can compare that events with your shapes. But maybe you just want another lib then cairo, maybe a lib which works with real objects internally, that is stores objects, automatically redraws that objects when necessary, and offers collision detection and all that. Cairo does not offer that, we have to redraw whenever the window is resized. I can not remember a name of such a lib, but there should be many, used for 2D games.

GooCanvas might be an appropriate library for this.

I understand what you mean but it is my mistake I should be more specific

how can I check if point is inside complex object like graphs, beziur curve, rotated rectangle

I found resources for circle which with straight rectangle are easy but what about oval shape

what about other things, what I want is to see if someone has experience implementing those

and what he use what algorithms and data structures

I want to implement things from scratch for learning

There are many nice UI created with cairo in the GNU/Linux audio scene
which include nice controls and visualization like fil4.lv2

my purpose is to learn how they create this nice custom widgets and how they draw it and handle it

thank you @ptomato for your suggestion GooCanvas is interesting project I should look into in the future

That is a non trivial topic. I am using my own RTree lib for that. With RTrees we can detect overlap of rectangles. So we give our objects a bounding box rectangle, and then can detect if the bounding box of an object overlaps with a selection rectangle. The selection rectangle can be just a single point. Then we continue to test for the real object shape. Not difficult for a line or circle, but can be difficult, i.e. for rotated elipsis.

One google search term is “collision detection”. Beside RTrees QuadTrees, KdTrees are used.

You’ve chosen a rather non-trivial task really

@zbrown @StefanSalewski

yes that’s what I looking for I’ve already implemented circles and non-rotated rectangles
so, I understand that Trees is good for efficient implementation

anyway I think my next step is to learn about rotated rectangles any suggestion

comp.graphics.algorithms Frequently Asked Questions (collection of geometric algorithm)

http://paulbourke.net/geometry/pointlineplane/

http://paulbourke.net/geometry/circlesphere/

http://www.faqs.org/faqs/graphics/algorithms-faq/

That is amazing Thanks alot :exploding_head:

The way I do it in my image and vector viewer http://giv.sourceforge.net/giv/index.html is that I use two images. Besides the image being displayed, I have a second image of the same size called the label image. For each pixel in the display image I also draw its label value in the label image. I can then test for which object that I’m pointing at in O(1) by just looking up its label in the label image. The thing you have to keep in mind is that when you are painting the label image you have to turn off the antialiasing so you don’t get any interpolated values.

Hope this helps.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.