thickLineRgba function

int thickLineRgba(
  1. Pointer<SdlRenderer> renderer,
  2. int x1,
  3. int y1,
  4. int x2,
  5. int y2,
  6. int width,
  7. int r,
  8. int g,
  9. int b,
  10. int a,
)

Implementation

int thickLineRgba(Pointer<SdlRenderer> renderer, int x1, int y1, int x2, int y2,
    int width, int r, int g, int b, int a) {
  int wh;
  double dx, dy, dx1, dy1, dx2, dy2;
  double l, wl2, nx, ny, ang, adj;
  var px = calloc<Int16>(4);
  var py = calloc<Int16>(4);

  if (renderer == nullptr) {
    return -1;
  }

  if (width < 1) {
    return -1;
  }

  /* Special case: thick "point" */
  if ((x1 == x2) && (y1 == y2)) {
    wh = width ~/ 2;
    return boxRgba(
        renderer, x1 - wh, y1 - wh, x2 + width, y2 + width, r, g, b, a);
  }

  /* Special case: width == 1 */
  if (width == 1) {
    return lineRgba(renderer, x1, y1, x2, y2, r, g, b, a);
  }

  /* Calculate offsets for sides */
  dx = (x2 - x1).toDouble();
  dy = (y2 - y1).toDouble();
  l = sdlSqrt(dx * dx + dy * dy);
  ang = sdlAtan2(dx, dy);
  adj = 0.1 + 0.9 * sdlFabs(sdlCos(2.0 * ang));
  wl2 = (width.toDouble() - adj) / (2.0 * l);
  nx = dx * wl2;
  ny = dy * wl2;

  /* Build polygon */
  dx1 = x1.toDouble();
  dy1 = y1.toDouble();
  dx2 = x2.toDouble();
  dy2 = y2.toDouble();
  px[0] = (dx1 + ny).toInt();
  px[1] = (dx1 - ny).toInt();
  px[2] = (dx2 - ny).toInt();
  px[3] = (dx2 + ny).toInt();
  py[0] = (dy1 - nx).toInt();
  py[1] = (dy1 + nx).toInt();
  py[2] = (dy2 + nx).toInt();
  py[3] = (dy2 - nx).toInt();

  /* Draw polygon */
  var result = filledPolygonRgba(renderer, px, py, 4, r, g, b, a);
  calloc.free(px);
  calloc.free(py);

  return result;
}