void draw_line_antialias_(GBitmap* img, int16_t x1, int16_t y1, int16_t x2, int16_t y2, GColor8 color) { uint8_t* img_pixels = gbitmap_get_data(img); int16_t w = gbitmap_get_bounds(img).size.w; int16_t h = gbitmap_get_bounds(img).size.h; fixed dx = int_to_fixed(abs_(x1 - x2)); fixed dy = int_to_fixed(abs_(y1 - y2)); bool steep = dy > dx; if(steep){ swap_(x1, y1); swap_(x2, y2); } if(x1 > x2){ swap_(x1, x2); swap_(y1, y2); } dx = x2 - x1; dy = y2 - y1; fixed intery; int x; for(x=x1; x <= x2; x++) { intery = int_to_fixed(y1) + (int_to_fixed(x - x1) * dy / dx); if(x>=0){ if(steep){ _plot(img_pixels, w, h, ipart_(intery) , x, color, rfpart_(intery)); _plot(img_pixels, w, h, ipart_(intery) + 1, x, color, fpart_(intery)); } else { _plot(img_pixels, w, h, x, ipart_(intery) , color, rfpart_(intery)); _plot(img_pixels, w, h, x, ipart_(intery) + 1, color, fpart_(intery)); } } } }
void ag_surface32__draw_line_aa(struct ag_surface32* img, struct ag_vec2i start, struct ag_vec2i end, struct ag_color32 color) { int x1 = start.x, x2 = end.x, y1 = start.y, y2 = end.y; double dx = (double)x2 - (double)x1; double dy = (double)y2 - (double)y1; if ( fabs(dx) > fabs(dy) ) { if ( x2 < x1 ) { swap_(x1, x2); swap_(y1, y2); } double gradient = dy / dx; double xend = round_(x1); double yend = y1 + gradient*(xend - x1); double xgap = rfpart_(x1 + 0.5); int xpxl1 = xend; int ypxl1 = ipart_(yend); plot_(xpxl1, ypxl1, rfpart_(yend)*xgap); plot_(xpxl1, ypxl1+1, fpart_(yend)*xgap); double intery = yend + gradient; xend = round_(x2); yend = y2 + gradient*(xend - x2); xgap = fpart_(x2+0.5); int xpxl2 = xend; int ypxl2 = ipart_(yend); plot_(xpxl2, ypxl2, rfpart_(yend) * xgap); plot_(xpxl2, ypxl2 + 1, fpart_(yend) * xgap); int x; for(x=xpxl1+1; x <= (xpxl2-1); x++) { plot_(x, ipart_(intery), rfpart_(intery)); plot_(x, ipart_(intery) + 1, fpart_(intery)); intery += gradient; } } else { if ( y2 < y1 ) { swap_(x1, x2); swap_(y1, y2); } double gradient = dx / dy; double yend = round_(y1); double xend = x1 + gradient*(yend - y1); double ygap = rfpart_(y1 + 0.5); int ypxl1 = yend; int xpxl1 = ipart_(xend); plot_(xpxl1, ypxl1, rfpart_(xend)*ygap); plot_(xpxl1, ypxl1+1, fpart_(xend)*ygap); double interx = xend + gradient; yend = round_(y2); xend = x2 + gradient*(yend - y2); ygap = fpart_(y2+0.5); int ypxl2 = yend; int xpxl2 = ipart_(xend); plot_(xpxl2, ypxl2, rfpart_(xend) * ygap); plot_(xpxl2, ypxl2 + 1, fpart_(xend) * ygap); int y; for(y=ypxl1+1; y <= (ypxl2-1); y++) { plot_(ipart_(interx), y, rfpart_(interx)); plot_(ipart_(interx) + 1, y, fpart_(interx)); interx += gradient; } } }
void MemoryImage::DrawLine(double x1, double y1, double x2, double y2, double color, Mode mode) { x1 *= m_oversample; x2 *= m_oversample; y1 *= m_oversample; y2 *= m_oversample; double dx = (double)x2 - (double)x1; double dy = (double)y2 - (double)y1; if (abs(dx) > abs(dy)) { if (x2 < x1) { swap(x1, x2); swap(y1, y2); } double gradient = dy / dx; double xend = round_(x1); double yend = y1 + gradient*(xend - x1); double xgap = rfpart_(x1 + 0.5); int xpxl1 = xend; int ypxl1 = ipart_(yend); DrawPoint(xpxl1, ypxl1, color* rfpart_(yend)*xgap,mode); DrawPoint(xpxl1, ypxl1 + 1, color* fpart_(yend)*xgap,mode); double intery = yend + gradient; xend = round_(x2); yend = y2 + gradient*(xend - x2); xgap = fpart_(x2 + 0.5); int xpxl2 = xend; int ypxl2 = ipart_(yend); DrawPoint(xpxl2, ypxl2, color* rfpart_(yend) * xgap,mode); DrawPoint(xpxl2, ypxl2 + 1, color* fpart_(yend) * xgap,mode); int x; for (x = xpxl1 + 1; x <= (xpxl2 - 1); x++) { DrawPoint(x, ipart_(intery), color* rfpart_(intery),mode); DrawPoint(x, ipart_(intery) + 1, color* fpart_(intery),mode); intery += gradient; } } else { if (y2 < y1) { swap(x1, x2); swap(y1, y2); } double gradient = dx / dy; double yend = round_(y1); double xend = x1 + gradient*(yend - y1); double ygap = rfpart_(y1 + 0.5); int ypxl1 = yend; int xpxl1 = ipart_(xend); DrawPoint(xpxl1, ypxl1, color* rfpart_(xend)*ygap, mode); DrawPoint(xpxl1, ypxl1 + 1, color* fpart_(xend)*ygap, mode); double interx = xend + gradient; yend = round_(y2); xend = x2 + gradient*(yend - y2); ygap = fpart_(y2 + 0.5); int ypxl2 = yend; int xpxl2 = ipart_(xend); DrawPoint(xpxl2, ypxl2, color* rfpart_(xend) * ygap, mode); DrawPoint(xpxl2, ypxl2 + 1, color* fpart_(xend) * ygap,mode); int y; for (y = ypxl1 + 1; y <= (ypxl2 - 1); y++) { DrawPoint(ipart_(interx), y, color* rfpart_(interx),mode); DrawPoint(ipart_(interx) + 1, y, color* fpart_(interx),mode); interx += gradient; } } }
void draw_line( unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, uint8_t r, uint8_t g, uint8_t b ) { double dx = (double)x2 - (double)x1; double dy = (double)y2 - (double)y1; if ( fabs(dx) > fabs(dy) ) { if ( x2 < x1 ) { swap_(x1, x2); swap_(y1, y2); } double gradient = dy / dx; double xend = round_(x1); double yend = y1 + gradient*(xend - x1); double xgap = rfpart_(x1 + 0.5); int xpxl1 = xend; int ypxl1 = ipart_(yend); dla_plot(xpxl1, ypxl1, r,g,b,rfpart_(yend)*xgap); dla_plot(xpxl1, ypxl1+1, r,g,b,fpart_(yend)*xgap); double intery = yend + gradient; xend = round_(x2); yend = y2 + gradient*(xend - x2); xgap = fpart_(x2+0.5); int xpxl2 = xend; int ypxl2 = ipart_(yend); dla_plot(xpxl2, ypxl2, r,g,b,rfpart_(yend) * xgap); dla_plot(xpxl2, ypxl2 + 1, r,g,b,fpart_(yend) * xgap); int x; for(x=xpxl1+1; x <= (xpxl2-1); x++) { dla_plot(x, ipart_(intery), r,g,b,rfpart_(intery)); dla_plot(x, ipart_(intery) + 1, r,g,b,fpart_(intery)); intery += gradient; } } else { if ( y2 < y1 ) { swap_(x1, x2); swap_(y1, y2); } double gradient = dx / dy; double yend = round_(y1); double xend = x1 + gradient*(yend - y1); double ygap = rfpart_(y1 + 0.5); int ypxl1 = yend; int xpxl1 = ipart_(xend); dla_plot(xpxl1, ypxl1, r,g,b,rfpart_(xend)*ygap); dla_plot(xpxl1, ypxl1+1, r,g,b,fpart_(xend)*ygap); double interx = xend + gradient; yend = round_(y2); xend = x2 + gradient*(yend - y2); ygap = fpart_(y2+0.5); int ypxl2 = yend; int xpxl2 = ipart_(xend); dla_plot(xpxl2, ypxl2, r,g,b,rfpart_(xend) * ygap); dla_plot(xpxl2, ypxl2 + 1, r,g,b,fpart_(xend) * ygap); int y; for(y=ypxl1+1; y <= (ypxl2-1); y++) { dla_plot(ipart_(interx), y, r,g,b,rfpart_(interx)); dla_plot(ipart_(interx) + 1, y, r,g,b,fpart_(interx)); interx += gradient; } } }