complex_polygon

27 views
Skip to first unread message

holm.h...@gmail.com

unread,
Jun 19, 2017, 6:50:08 AM6/19/17
to fltk.general

Hello,
I want to draw a polygon and cut out some holes. I try the following approach :

fl_begin_complex_polygon();

    fl_begin_complex_polygon();  // surrounding polygon
           fl_vertex.... - in clockwise direction
    fl_end_complex_polygon();
 fl_gap();
    fl_begin_complex_polygon();  // hole
           fl_vertex.... - in anti-clockwise direction
    fl_end_complex_polygon();

  fl_end_complex_polygon();
 

It seem to me that this approach has been used in arc.cxx (test-directory).

The above approach do however not work.

Have I missed something ?

Best regards
Håvard

Albrecht Schlosser

unread,
Jun 19, 2017, 1:40:25 PM6/19/17
to fltkg...@googlegroups.com
On 19.06.2017 at 12:50 holm.haavard@.. wrote:

> I want to draw a polygon and cut out some holes. I try the following
> approach :
>
> fl_begin_complex_polygon();
>
> fl_begin_complex_polygon(); // surrounding polygon
> fl_vertex.... - in clockwise direction
> fl_end_complex_polygon();
> fl_gap();
> fl_begin_complex_polygon(); // hole
> fl_vertex.... - in anti-clockwise direction
> fl_end_complex_polygon();
>
> fl_end_complex_polygon();

This looks like trying to use nested fl_begin_complex_polygon() calls.
I'm not an expert in drawing polygons ;-) but I don't think that this
can successfully be done. A quick glance at the code in FLTK 1.4 seems
to show that there are some counters reset for each
fl_begin_complex_polygon() call, so your code will likely not work. As I
read the docs, your code should more look like:

fl_begin_complex_polygon(); // surrounding polygon
fl_vertex.... - in clockwise direction
fl_gap();
fl_vertex.... - in anti-clockwise direction
fl_end_complex_polygon();

> It seem to me that this approach has been used in arc.cxx (test-directory).

A quick grep shows only one fl_begin_complex_polygon() in arc.cxx, so
this is at least not nested (unless there is a loop in the code - I
didn't check this).

> Have I missed something ?

Maybe... please post a short, but complete (compileable and linkable)
code example that shows what exactly you are doing so we can easily test
if the above suggestion doesn't help.

holm.h...@gmail.com

unread,
Jun 19, 2017, 3:44:53 PM6/19/17
to fltk.general, Albrech...@online.de

Thank you, Albrecht

I will give it a try.

Btw, I am trying to clip a "complex polygon" (convex/concave/holes) , by a bounding box (x,y_min .. x,y_max). I need to
calculate and store all new coordinates/points of the remaining polygon.

Do you know if there are algorithms in fltk to do this ?

Best regards
Håvard

Albrecht Schlosser

unread,
Jun 19, 2017, 5:22:30 PM6/19/17
to fltkg...@googlegroups.com
On 19.06.2017 21:44 holm.haavard@.. wrote:
>
> Thank you, Albrecht
>
> I will give it a try.
>
> Btw, I am trying to clip a "complex polygon" (convex/concave/holes) , by
> a bounding box (x,y_min .. x,y_max). I need to
> calculate and store all new coordinates/points of the remaining polygon.

Do you really need to calculate and store the coordinates? If yes, this
is obviously a math/geometry problem, not a FLTK one.

> Do you know if there are algorithms in fltk to do this ?

Not that I know of, if you really want to calculate coordinates. But if
it's for drawing only, you could set a clip region and let FLTK (or,
precise: the underlying OS) do the clipping.

See fl_push_clip(), fl_pop_clip():
<http://www.fltk.org/doc-1.3/group__fl__drawings.html#gabe60242ba3b975800717b95cb6bc06d2>
<http://www.fltk.org/doc-1.3/group__fl__drawings.html#ga7abb216a9a87408c8926126cc9efce22>

May be interesting as well: fl_not_clipped()
<http://www.fltk.org/doc-1.3/group__fl__drawings.html#ga2bd899d466196ffc26cc61542ee10539>

holm.h...@gmail.com

unread,
Jun 20, 2017, 6:19:20 AM6/20/17
to fltk.general, Albrech...@online.de

Hi,

Still struggeling.. The following code was expected to draw three separate quads, however I also get something between..
    fl_begin_complex_polygon();
   
    fl_vertex(40, 140);
    fl_vertex(20, 100);
    fl_vertex( 0, 140);
    fl_vertex(20, 180);
    fl_vertex(40, 140);
    fl_gap();
    fl_vertex(60, 140);
    fl_vertex(80, 100);
    fl_vertex(100, 140);
    fl_vertex(80,  180);
    fl_vertex(60,  140);
    fl_gap();
    fl_vertex(70,  240);
    fl_vertex(90,  280);
    fl_vertex(110, 240);
    fl_vertex(90,  200);
    fl_vertex(70,  240);
   
    fl_end_complex_polygon();

What is the problem with the code above ??

Best regards
Håvard


Message has been deleted
Message has been deleted

Albrecht Schlosser

unread,
Jun 21, 2017, 7:01:12 AM6/21/17
to fltkg...@googlegroups.com
On 20.06.2017 12:19 schrieb holm.haavard@.. wrote:

> Still struggeling.. The following code was expected to draw three
> separate quads, however I also get something between..

What is "something" ?

> fl_begin_complex_polygon();
>
> fl_vertex(40, 140);
> fl_vertex(20, 100);
> fl_vertex( 0, 140);
> fl_vertex(20, 180);
> fl_vertex(40, 140);
> fl_gap();
> fl_vertex(60, 140);
> fl_vertex(80, 100);
> fl_vertex(100, 140);
> fl_vertex(80, 180);
> fl_vertex(60, 140);
> fl_gap();
> fl_vertex(70, 240);
> fl_vertex(90, 280);
> fl_vertex(110, 240);
> fl_vertex(90, 200);
> fl_vertex(70, 240);
>
> fl_end_complex_polygon();
>
> What is the problem with the code above ??

I'd like to help, really, but ...

*My* problem with this code is that I can't imagine what all these
coordinates look like and/or if there are overlaps in the "three
separate quads" you would like to draw. Please post a short, but
complete, compileable demo source code to make it easier for us to help you.

Albrecht Schlosser

unread,
Jun 21, 2017, 8:48:05 AM6/21/17
to fltkg...@googlegroups.com
On 21.06.2017 13:01 Albrecht Schlosser wrote:
> On 20.06.2017 12:19 schrieb holm.haavard@.. wrote:
>
>> Still struggeling.. The following code was expected to draw three
>> separate quads, however I also get something between..
>
> What is "something" ?

Ah, I see, it's a filled triangle connecting the quads.
Okay, I was curious and did it myself, see the attached file polygon.cxx
that got longer and more complex than I had intended.

Some comments:

If you want to draw separate objects you should use separate polygon
drawing calls in FLTK, i.e. don't use fl_gap() but use fl_end*polygon;
fl_begin*polygon; between these separate objects.

In your example you're drawing three separate, complex, poygons. There's
no need to use fl_begin/end_complex_polygon though if that's what you
intend to do: you can use the faster and more accurate
fl_begin/end_polygon instead. polygon.cxx shows this if you define
COMPLEX = 0.

Your original code is kinda replicated if you set COMPLEX = 1 and
USE_GAP = 1. I'm additionally setting fl_color() to different values.
Change USE_GAP => 0 to get what you supposedly intended to get.

Separate objects can have different colors, which the example program
shows. If you draw one complex polygon only (with USE_GAP = 1) there is
only one color (in this case the last one: FL_RED).

To avoid changing coordinates of your original code so would draw inside
the box boundaries I added a translation.

There's no guarantee that this code is entirely correct or best
practice, but it works for me and does what I think you intended to do:

set COMPLEX = 0 or 1 (0 preferred, see above)
set USE_GAP 0 (only relevant if you use COMPLEX = 1)
polygon.cxx
Reply all
Reply to author
Forward
0 new messages