FLTK logo

Re: [fltk.general] complex_polygon

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: complex_polygon "'Matthias Melcher' via fltk.general" Jan 02, 2022  
  I doubt that this is solving your original problem, especially if you are in the 3D OpenGL world. Triangulation is IMHO the correct way to go, and it has no limitations that I am aware of. Anyway, the code below draws a complex polygon by drawing horizontal lines to fill the area.

```

  class Vertex {

  public:

    void set(float x, float y, bool gap = false) { pX = x; pY = y; pIsGap = gap; }

    float pX, pY;

    bool pIsGap;

  };


void Fl_Android_Graphics_Driver::end_complex_polygon()

{

  if (pnVertex < 2) return;


  gap(); // adds the first coordinate of this loop and marks it as a gap

  int begin = 0, end = pnVertex;


  Vertex *v = pVertex+0;

  int xMin = v->pX, xMax = xMin, yMin = v->pY, yMax = yMin;

  for (int i = begin+1; i < end; i++) {

    v = pVertex+i;

    if (v->pX < xMin) xMin = v->pX;

    if (v->pX > xMax) xMax = v->pX;

    if (v->pY < yMin) yMin = v->pY;

    if (v->pY > yMax) yMax = v->pY;

  }

  xMax++; yMax++;


  int nodes, nodeX[end - begin], pixelX, pixelY, i, j, swap;


  //  Loop through the rows of the image.

  for (pixelY = yMin; pixelY < yMax; pixelY++) {

    //  Build a list of nodes.

    nodes = 0;

    for (i = begin+1; i < end; i++) {

      j = i-1;

      if (pVertex[j].pIsGap)

        continue;

      if (   (pVertex[i].pY < pixelY && pVertex[j].pY >= pixelY)

          || (pVertex[j].pY < pixelY && pVertex[i].pY >= pixelY) )

      {

        float dy = pVertex[j].pY - pVertex[i].pY;

        if (fabsf(dy)>.0001) {

          nodeX[nodes++] = (int)(pVertex[i].pX +

                                 (pixelY - pVertex[i].pY) / dy

                                 * (pVertex[j].pX - pVertex[i].pX));

        } else {

          nodeX[nodes++] = pVertex[i].pX;

        }

      }

    }

    //Fl_Android_Application::log_e("%d nodes (must be even!)", nodes);


    //  Sort the nodes, via a simple “Bubble” sort.

    i = 0;

    while (i < nodes - 1) {

      if (nodeX[i] > nodeX[i + 1]) {

        swap = nodeX[i];

        nodeX[i] = nodeX[i + 1];

        nodeX[i + 1] = swap;

        if (i) i--;

      } else {

        i++;

      }

    }


    //  Fill the pixels between node pairs.

    for (i = 0; i < nodes; i += 2) {

      if (nodeX[i] >= xMax) break;

      if (nodeX[i + 1] > xMin) {

        if (nodeX[i] < xMin) nodeX[i] = xMin;

        if (nodeX[i + 1] > xMax) nodeX[i + 1] = xMax;

        xyline(nodeX[i], pixelY, nodeX[i + 1]);

      }

    }

  }

}

```

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/8eead561-1bca-4268-9142-998ec76eb57bn%40googlegroups.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.