[cairo] Check collision via cairo_clip_extents
Uli Schlachter
psychon at znc.in
Fri Nov 23 16:54:07 UTC 2018
Hi,
first of all, your scale parameter confused me a lot. :-)
Then: I do not really think that cairo clip extents is the right API for
collision detection, but okay...
On 23.11.18 10:16, Yang Hong wrote:
> I tried to use cairo_clip_extents() to find intersect of triangle and
> rectangle, but it doesn't work, because the returned bounding is bigger than
> the accurate intersect.
I fail to see this "too big". What's the output that you want to get
from this?
Let's look at each printf:
[...]
> cairo_new_path(cr);
> create_triangle(cr, rec36, ARRAY_SIZE(rec36), 1);
[...]
> cairo_clip(cr);
> cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
> //cairo_fill_extents(cr, &x1, &y1, &x2, &y2);
>
> printf("Clip extents: %f, %f, %f, %f\n", x1, y1, x2, y2);
This prints 0 0 60 120 for me (well, with lots of trailing ".0000"). And
indeed, rec36 contains the points (0,0), (3,0), (3,6), (0,6). The
extents of this are (0,0) to (3,6), which after multiplying by scale=20
gives (0,0) and (60,120), which is just what cairo is giving you.
[...]
> cairo_reset_clip(cr);
> cairo_new_path(cr);
> create_triangle(cr, tri4, ARRAY_SIZE(tri4), 1);
[...]
> cairo_clip(cr);
tri4 has the points (3,0), (6,3), (0,3). The extents of this are (0,0)
to (6,3).
>
> cairo_translate(cr, 100, 0);
> cairo_new_path(cr);
> create_triangle(cr, rec36, ARRAY_SIZE(rec36), 1);
[...]
> x1 = y1 = x2 = y2 = 0.0;
> cairo_clip(cr);
> cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
> //cairo_fill_extents(cr, &x1, &y1, &x2, &y2);
>
> printf("Clip extents: %f, %f, %f, %f\n", x1, y1, x2, y2);
Okay... output for me here is 0 0 20 60.
Extents of tri4 are (-5,0) to (1,3) (because of the translate by (100,0)
above, which is (5,0) when divided by scale=20).
Extents of rec36 are (0,0) to (3,6).
The intersection of these to is from (0,0) to (1,3) (and all of this is
actually "touched" by one of the shapes, so the extents here are right,
as far as I can see). After multiplying by scale=20, this is exactly the
numbers you get here.
[etc for the other cases; I got a bit bored here].
So, I fail to understand the problem. Sorry.
Could you perhaps send an example that actually just reproduces the
problem and explain why you think that cairo should produce different
values here (and what those values are)?
Uli
--
Bitte nicht mit dem verbleibenden Auge in den Laser gucken.
- Vincent Ebert
More information about the cairo
mailing list