Skip to content

Instantly share code, notes, and snippets.

@cmb69
Created April 16, 2018 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmb69/40528e5880cd97647a9aaff6d5335c30 to your computer and use it in GitHub Desktop.
Save cmb69/40528e5880cd97647a9aaff6d5335c30 to your computer and use it in GitHub Desktop.
gdImageSetStyle() and gdImageLine() instead of gdImageDashedLine()
#include <string.h>
#include <gd.h>
/* does not try to create a symmetric pattern */
void dashed_line(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
{
int wid = im->thick; /* this may have to be adjusted */
int *oldStyle, oldStyleLength = im->styleLength, oldStylePos = im->stylePos;
int *newStyle, newStyleLength = wid * gdDashSize * 2;
int i;
oldStyle = malloc(oldStyleLength * sizeof(int)); /* should actually be gdMalloc() */
memcpy(oldStyle, im->style, oldStyleLength * sizeof(int));
newStyle = malloc(newStyleLength * sizeof(int)); /* should actually be gdMalloc() */
for (i = 0; i < newStyleLength; i++) {
newStyle[i] = (i < newStyleLength / 2 ? color : gdTransparent);
}
gdImageSetStyle(im, newStyle, newStyleLength);
gdImageLine(im, x1, y1, x2, y2, gdStyled);
free(newStyle); /* should actually be gdFree() */
memcpy(im->style, oldStyle, oldStyleLength * sizeof(int));
im->styleLength = oldStyleLength;
im->stylePos = oldStylePos;
free(oldStyle); /* should actually be gdFree() */
}
int main()
{
gdImagePtr im;
FILE *fp;
im = gdImageCreateTrueColor(100, 100);
gdImageFilledRectangle(im, 0, 0, 99, 99, 0xffffff);
gdImageSetThickness(im, 3);
dashed_line(im, 10, 10, 90, 80, 0x000000);
gdImageDashedLine(im, 10, 20, 90, 90, 0x000000);
fp = fopen("dashed_line.png", "wb");
gdImagePng(im, fp);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment