Add dpoly() function #29

Open
opened 2024-01-19 21:34:09 +01:00 by Slyvtt · 1 comment

Just don't want to mix the PRs so I just copy paste the code of the dpoly() rendering function here:

prototype is as follows (for gint\display.h):

/* dpoly(): Polygone with border

   This function renders a closed-polygone defined by its vertices coordinates.

   @x @y          Pointer to arrays containing the x / y coordinates of each
                  individual vertex
   @nbvertices    Number of vertices
   @fill_color    Color of the surface inside the polygone, C_NONE to disable
   @border_color  Color of the polygon itself, C_NONE to disable */

void dpoly(int *x, int *y, int nbvertices, int fill_color, int border_color);

and dpoly.c looks like this :

#include "../render/render.h"
#include <gint/defs/util.h>
#include <gint/display.h>

void dpoly(int *x, int *y, int nb_vertices, int fill_color, int border_color) {

  int i, ymin, ymax, xmin, xmax, *xmint, *xmaxt;
  char *emptyt;

  if (fill_color == C_NONE && border_color == C_NONE)
    return;

  if (fill_color != C_NONE) {
    int i, ymin, ymax, xmin2, xmax2, *xmin, *xmax;
    char *empty;

    if (nb_vertices < 3)
      return;

    ymin = ymax = y[0];
    xmin2 = xmax2 = x[0];
    for (i = 0; i < nb_vertices; i++) {
      if (y[i] < ymin)
        ymin = y[i];
      if (y[i] > ymax)
        ymax = y[i];
      if (x[i] < xmin2)
        xmin2 = x[i];
      if (x[i] > xmax2)
        xmax2 = x[i];
    }

    if (xmax2 < 0 || xmin2 > DWIDTH - 1 || ymax < 0 || ymin > DHEIGHT - 1)
      return;

    xmin = malloc((ymax - ymin + 1) * sizeof(int));
    xmax = malloc((ymax - ymin + 1) * sizeof(int));
    empty = malloc(ymax - ymin + 1);

    if (xmin && xmax && empty) {
      for (i = 0; i < ymax - ymin + 1; i++)
        empty[i] = 1;

      for (i = 0; i < nb_vertices; i++) {
        int j, px, py, dx, dy, sx, sy, cumul;
        px = x[i];
        py = y[i];
        dx = x[(i + 1) % nb_vertices] - px;
        dy = y[(i + 1) % nb_vertices] - py;
        sx = (dx > 0) ? 1 : -1;
        sy = (dy > 0) ? 1 : -1;
        dx = (dx > 0) ? dx : -dx;
        dy = (dy > 0) ? dy : -dy;

        if (empty[py - ymin])
          xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0;
        else
          xmax[py - ymin] = px;

        if (dx > dy) {
          cumul = dx >> 1;
          for (j = 1; j < dx; j++) {
            px += sx;
            cumul += dy;
            if (cumul > dx)
              cumul -= dx, py += sy;
            if (empty[py - ymin])
              xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0;
            else
              xmax[py - ymin] = px;
          }
        } else {
          cumul = dy >> 1;
          for (j = 1; j < dy; j++) {
            py += sy;
            cumul += dx;
            if (cumul > dy)
              cumul -= dy, px += sx;
            if (empty[py - ymin])
              xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0;
            else
              xmax[py - ymin] = px;
          }
        }
      }

      for (i = 0; i < ymax - ymin + 1; i++)
        gint_dhline(xmin[i], xmax[i], ymin + i, fill_color);
    }

    free(xmin);
    free(xmax);
    free(empty);
  }

  if (border_color != C_NONE) {
    for (int i = 0; i < nb_vertices; i++) {
      int px = x[i];
      int py = y[i];
      int px2 = x[(i + 1) % nb_vertices];
      int py2 = y[(i + 1) % nb_vertices];
      dline(px, py, px2, py2, border_color);
    }
  }
  return;
}

Attached is an exemple or application with the corresponding screenshot

#include <gint/display.h>
#include <gint/keyboard.h>

int main(void) {
  dclear(C_WHITE);
  dtext(1, 1, C_BLACK, "Sample fxSDK add-in.");

  int x[5] = {0, 50, 100, 100, 50};
  int y[5] = {0, 0, 100, 200, 50};

  dpoly(&x[0], &y[0], 5, C_RED, C_BLUE);

  dupdate();

  getkey();
  return 1;
}

Just don't want to mix the PRs so I just copy paste the code of the dpoly() rendering function here: prototype is as follows (for `gint\display.h`): ``` /* dpoly(): Polygone with border This function renders a closed-polygone defined by its vertices coordinates. @x @y Pointer to arrays containing the x / y coordinates of each individual vertex @nbvertices Number of vertices @fill_color Color of the surface inside the polygone, C_NONE to disable @border_color Color of the polygon itself, C_NONE to disable */ void dpoly(int *x, int *y, int nbvertices, int fill_color, int border_color); ``` and `dpoly.c` looks like this : ``` #include "../render/render.h" #include <gint/defs/util.h> #include <gint/display.h> void dpoly(int *x, int *y, int nb_vertices, int fill_color, int border_color) { int i, ymin, ymax, xmin, xmax, *xmint, *xmaxt; char *emptyt; if (fill_color == C_NONE && border_color == C_NONE) return; if (fill_color != C_NONE) { int i, ymin, ymax, xmin2, xmax2, *xmin, *xmax; char *empty; if (nb_vertices < 3) return; ymin = ymax = y[0]; xmin2 = xmax2 = x[0]; for (i = 0; i < nb_vertices; i++) { if (y[i] < ymin) ymin = y[i]; if (y[i] > ymax) ymax = y[i]; if (x[i] < xmin2) xmin2 = x[i]; if (x[i] > xmax2) xmax2 = x[i]; } if (xmax2 < 0 || xmin2 > DWIDTH - 1 || ymax < 0 || ymin > DHEIGHT - 1) return; xmin = malloc((ymax - ymin + 1) * sizeof(int)); xmax = malloc((ymax - ymin + 1) * sizeof(int)); empty = malloc(ymax - ymin + 1); if (xmin && xmax && empty) { for (i = 0; i < ymax - ymin + 1; i++) empty[i] = 1; for (i = 0; i < nb_vertices; i++) { int j, px, py, dx, dy, sx, sy, cumul; px = x[i]; py = y[i]; dx = x[(i + 1) % nb_vertices] - px; dy = y[(i + 1) % nb_vertices] - py; sx = (dx > 0) ? 1 : -1; sy = (dy > 0) ? 1 : -1; dx = (dx > 0) ? dx : -dx; dy = (dy > 0) ? dy : -dy; if (empty[py - ymin]) xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0; else xmax[py - ymin] = px; if (dx > dy) { cumul = dx >> 1; for (j = 1; j < dx; j++) { px += sx; cumul += dy; if (cumul > dx) cumul -= dx, py += sy; if (empty[py - ymin]) xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0; else xmax[py - ymin] = px; } } else { cumul = dy >> 1; for (j = 1; j < dy; j++) { py += sy; cumul += dx; if (cumul > dy) cumul -= dy, px += sx; if (empty[py - ymin]) xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0; else xmax[py - ymin] = px; } } } for (i = 0; i < ymax - ymin + 1; i++) gint_dhline(xmin[i], xmax[i], ymin + i, fill_color); } free(xmin); free(xmax); free(empty); } if (border_color != C_NONE) { for (int i = 0; i < nb_vertices; i++) { int px = x[i]; int py = y[i]; int px2 = x[(i + 1) % nb_vertices]; int py2 = y[(i + 1) % nb_vertices]; dline(px, py, px2, py2, border_color); } } return; } ``` Attached is an exemple or application with the corresponding screenshot ``` #include <gint/display.h> #include <gint/keyboard.h> int main(void) { dclear(C_WHITE); dtext(1, 1, C_BLACK, "Sample fxSDK add-in."); int x[5] = {0, 50, 100, 100, 50}; int y[5] = {0, 0, 100, 200, 50}; dpoly(&x[0], &y[0], 5, C_RED, C_BLUE); dupdate(); getkey(); return 1; } ```
Owner

So, part of this is implemented with b802e8edef and e0ac25fbb0.

Looking back to it, I think there was an issue about precision and edges not lining up properly. @Slyvtt Do you remember exactly if we sorted that out?

So, part of this is implemented with https://git.planet-casio.com/Lephenixnoir/gint/commit/b802e8edef68937e3ab00fb9b2e74b03ff5195bc and https://git.planet-casio.com/Lephenixnoir/gint/commit/e0ac25fbb0e71dd0b522b31facdc794c53345f21. Looking back to it, I think there was an issue about precision and edges not lining up properly. @Slyvtt Do you remember exactly if we sorted that out?
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Lephenixnoir/gint#29
No description provided.