FastLED 3.10.10
Loading...
Searching...
No Matches

◆ drawLineCore()

template<typename PixelT, typename Coord, bool Overwrite>
void fl::gfx::detail::drawLineCore ( Canvas< PixelT > & canvas,
const PixelT & color,
Coord x0,
Coord y0,
Coord x1,
Coord y1 )
inline

Definition at line 908 of file primitives.h.

909 {
910 PixelT* pixels = canvas.pixels;
911 int width = canvas.width;
912 int height = canvas.height;
913
914 // Convert to 8.8 fixed-point
915 fl::i32 fx0 = detail::toFixed8(x0);
916 fl::i32 fy0 = detail::toFixed8(y0);
917 fl::i32 fx1 = detail::toFixed8(x1);
918 fl::i32 fy1 = detail::toFixed8(y1);
919
920 bool steep = fl::abs(fy1 - fy0) > fl::abs(fx1 - fx0);
921 if (steep) { fl::swap(fx0, fy0); fl::swap(fx1, fy1); }
922 if (fx0 > fx1) { fl::swap(fx0, fx1); fl::swap(fy0, fy1); }
923
924 fl::i32 dx8 = fx1 - fx0;
925 fl::i32 dy8 = fy1 - fy0;
926
927 // Gradient per pixel step in 8.8: (dy * 256) / dx
928 fl::i32 gradient = (dx8 == 0) ? 0 : ((dy8 << 8) / dx8);
929
930 // First endpoint: round x to nearest pixel boundary
931 fl::i32 xend = (fx0 + 128) & ~0xFF;
932 fl::i32 yend = fy0 + ((gradient * (xend - fx0)) >> 8);
933 fl::i32 xgap = 256 - ((fx0 + 128) & 0xFF); // rfpart(x0 + 0.5)
934 int xpxl1 = xend >> 8;
935 int ypxl1 = yend >> 8;
936 fl::u8 yfrac1 = static_cast<fl::u8>(yend & 0xFF);
937
938 // Second endpoint
939 fl::i32 xend2 = (fx1 + 128) & ~0xFF;
940 fl::i32 yend2 = fy1 + ((gradient * (xend2 - fx1)) >> 8);
941 fl::i32 xgap2 = (fx1 + 128) & 0xFF; // fpart(x1 + 0.5)
942 int xpxl2 = xend2 >> 8;
943 int ypxl2 = yend2 >> 8;
944 fl::u8 yfrac2 = static_cast<fl::u8>(yend2 & 0xFF);
945
946 // Draw first endpoint
947 {
948 fl::u8 b1 = static_cast<fl::u8>(((255 - yfrac1) * xgap) >> 8);
949 fl::u8 b2 = static_cast<fl::u8>((yfrac1 * xgap) >> 8);
950 if (steep) {
951 PixelT c = color; c.nscale8(b1);
952 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, ypxl1, xpxl1, c);
953 c = color; c.nscale8(b2);
954 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, ypxl1 + 1, xpxl1, c);
955 } else {
956 PixelT c = color; c.nscale8(b1);
957 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, xpxl1, ypxl1, c);
958 c = color; c.nscale8(b2);
959 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, xpxl1, ypxl1 + 1, c);
960 }
961 }
962
963 // Draw second endpoint
964 {
965 fl::u8 b1 = static_cast<fl::u8>(((255 - yfrac2) * xgap2) >> 8);
966 fl::u8 b2 = static_cast<fl::u8>((yfrac2 * xgap2) >> 8);
967 if (steep) {
968 PixelT c = color; c.nscale8(b1);
969 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, ypxl2, xpxl2, c);
970 c = color; c.nscale8(b2);
971 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, ypxl2 + 1, xpxl2, c);
972 } else {
973 PixelT c = color; c.nscale8(b1);
974 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, xpxl2, ypxl2, c);
975 c = color; c.nscale8(b2);
976 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, xpxl2, ypxl2 + 1, c);
977 }
978 }
979
980 // Main loop: integer counter, 8.8 intery accumulation
981 fl::i32 intery = yend + gradient;
982 if (steep) {
983 for (int x = xpxl1 + 1; x < xpxl2; ++x) {
984 int y = intery >> 8;
985 fl::u8 frac = static_cast<fl::u8>(intery & 0xFF);
986 PixelT c = color; c.nscale8(255 - frac);
988 c = color; c.nscale8(frac);
990 intery += gradient;
991 }
992 } else {
993 for (int x = xpxl1 + 1; x < xpxl2; ++x) {
994 int y = intery >> 8;
995 fl::u8 frac = static_cast<fl::u8>(intery & 0xFF);
996 PixelT c = color; c.nscale8(255 - frac);
998 c = color; c.nscale8(frac);
1000 intery += gradient;
1001 }
1002 }
1003}
fl::CanvasRGB canvas(fl::span< CRGB >(leds, NUM_LEDS), WIDTH, HEIGHT)
unsigned char u8
Definition s16x16x4.h:132
enable_if_t< is_same< T, U >::value > swap(T &a, U &b) FL_NO_EXCEPT
Definition s16x16x4.h:905
fl::i32 toFixed8(Coord val) FL_NO_EXCEPT
Convert any Coord type to 8.8 fixed-point (fl::i32).
Definition primitives.h:187
void addPixelToBuffer(PixelT *pixels, int width, int height, int x, int y, const PixelT &color) FL_NO_EXCEPT
Internal helper: add or set pixel to rectangular buffer with bounds checking Direct row-major indexin...
Definition primitives.h:129
u8 u8 height
Definition blur.h:186
u8 width
Definition blur.h:186
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NO_EXCEPT

References fl::abs(), fl::gfx::addPixelToBuffer(), canvas, fl::FL_NO_EXCEPT, fl::height, fl::fl::swap(), toFixed8(), fl::width, fl::x, and fl::y.

Referenced by fl::gfx::drawLine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: