diff options
Diffstat (limited to 'FrameRender')
| -rw-r--r-- | FrameRender/P_LineSegment.c | 44 | ||||
| -rw-r--r-- | FrameRender/P_LineSegment.h | 17 |
2 files changed, 61 insertions, 0 deletions
diff --git a/FrameRender/P_LineSegment.c b/FrameRender/P_LineSegment.c new file mode 100644 index 0000000..f2236ff --- /dev/null +++ b/FrameRender/P_LineSegment.c @@ -0,0 +1,44 @@ +#include "P_LineSegment.h" +#include "PixelCallback.h" +#include <math.h> +#include <stdlib.h> +#include <string.h> + +void rotatePoint(i32* x, i32* y, double angle) { + i32 xR = *x * cos(angle) + *y * sin(angle); + i32 yR = - *x * sin(angle) + *y * cos(angle); + *x = xR; + *y = yR; +} + +void LineSegment_callback(LineSegment* self, struct PixelContext* fc) { + /* IDK if this is the most efficient, online results seem + * to do more complicated calculations. + * Center coordianates around aX,aY + * Then unrotate bX,bY and x,y relative to 0,0 (aX,aY), + * so that bX,bY lies on the horizontal axis. + * Now the line is flat from 0,0 to the "right" (increasing x). + */ + i32 x = fc->x - self->aX, y = fc->y - self->aY; + i32 bX = self->bX - self->aX, bY = self->bY - self->aY; + + double phi = atan2(bY, bX); + rotatePoint(&y, &x, -phi); + rotatePoint(&bY, &bX, -phi); + + if (0 <= x && x <= bX && + -(i32)self->width / 2 <= y && y <= self->width / 2) + { + ARGB_merge(&fc->pixel, self->color); + } +} + +PixelCallback* LineSegment_setup(LineSegment obj) { + PixelCallback* pca = malloc(sizeof(struct PixelCallback)); + + pca->callback = (PixelCallback_callback)LineSegment_callback; + pca->callback_self = malloc(sizeof(obj)); + memcpy(pca->callback_self, &obj, sizeof(obj)); + + return pca; +} diff --git a/FrameRender/P_LineSegment.h b/FrameRender/P_LineSegment.h new file mode 100644 index 0000000..2e52766 --- /dev/null +++ b/FrameRender/P_LineSegment.h @@ -0,0 +1,17 @@ +#ifndef P_LINE_SEGMENT +#define P_LINE_SEGMENT + +#include "PixelCallback.h" + +typedef struct LineSegment { + i32 aX; + i32 aY; + i32 bX; + i32 bY; + u32 width; + color color; +} LineSegment; + +PixelCallback* LineSegment_setup(LineSegment obj); + +#endif /* P_LINE_SEGMENT */ |
