From 7679c3a7ff4c03afdb697184ef6621ff06c35fbe Mon Sep 17 00:00:00 2001 From: Peter Marquardt Date: Mon, 25 Jan 2016 23:47:14 +0100 Subject: [PATCH] first blood ... err stripes --- src/intersection.h | 109 +++++++++++++++++++++++++++++++++++++++++++++ src/squared.c | 25 ++++++++++- 2 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/intersection.h diff --git a/src/intersection.h b/src/intersection.h new file mode 100644 index 0000000..b970a9a --- /dev/null +++ b/src/intersection.h @@ -0,0 +1,109 @@ +#include + +#define DONT_INTERSECT 0 +#define DO_INTERSECT 1 +#define COLLINEAR 2 + +#define SAME_SIGNS( a, b ) \ + (((long) ((unsigned long) a ^ (unsigned long) b)) >= 0 ) + +int lines_intersect( + x1, y1, + x2, y2, + x3, y3, + x4, y4, + x, + y +) + +long x1, y1, x2, y2, x3, y3, x4, y4, *x, *y; + +{ + long a1, a2, b1, b2, c1, c2; /* Coefficients of line eqns. */ + long r1, r2, r3, r4; /* 'Sign' values */ + long denom, offset, num; /* Intermediate values */ + + a1 = y2 - y1; + b1 = x1 - x2; + c1 = x2 * y1 - x1 * y2; + + r3 = a1 * x3 + b1 * y3 + c1; + r4 = a1 * x4 + b1 * y4 + c1; + + if ( r3 != 0 && + r4 != 0 && + SAME_SIGNS( r3, r4 )) + return ( DONT_INTERSECT ); + + a2 = y4 - y3; + b2 = x3 - x4; + c2 = x4 * y3 - x3 * y4; + + r1 = a2 * x1 + b2 * y1 + c2; + r2 = a2 * x2 + b2 * y2 + c2; + + if ( r1 != 0 && + r2 != 0 && + SAME_SIGNS( r1, r2 )) + return ( DONT_INTERSECT ); + + denom = a1 * b2 - a2 * b1; + if ( denom == 0 ) + return ( COLLINEAR ); + offset = denom < 0 ? - denom / 2 : denom / 2; + + num = b1 * c2 - b2 * c1; + *x = ( num < 0 ? num - offset : num + offset ) / denom; + + num = a2 * c1 - a1 * c2; + *y = ( num < 0 ? num - offset : num + offset ) / denom; + + return ( DO_INTERSECT ); +} + +#define INTERSECTION_NONE 0 +#define INTERSECTION_PARTIAL 1 +#define INTERSECTION_FULL 2 + +typedef struct { + uint8_t success; + GPoint p1; + GPoint p2; +} Intersection; + +Intersection rectintersect(GPoint p1, GPoint p2, GRect rect) { + long x,y; + long x1=0,y1=0,x2=0,y2=0; + bool intersection = false, final_intersection = false; + if (lines_intersect(p1.x, p1.y, p2.x, p2.y, rect.origin.x, rect.origin.y, rect.origin.x, rect.origin.y+rect.size.h, &x, &y) == DO_INTERSECT) { + x1 = x; y1 = y; intersection = true; + } + if (lines_intersect(p1.x, p1.y, p2.x, p2.y, rect.origin.x, rect.origin.y, rect.origin.x+rect.size.w, rect.origin.y, &x, &y) == DO_INTERSECT) { + if (!intersection) { + x1 = x; y1 = y; intersection = true; + } else if (x != x1 && y != y1) { + x2 = x; y2 = y; final_intersection = true; + } + } + if (!final_intersection && lines_intersect(p1.x, p1.y, p2.x, p2.y, rect.origin.x, rect.origin.y+rect.size.h, rect.origin.x+rect.size.w, rect.origin.y+rect.size.h, &x, &y) == DO_INTERSECT) { + if (!intersection) { + x1 = x; y1 = y; intersection = true; + } else if (x != x1 && y != y1) { + x2 = x; y2 = y; final_intersection = true; + } + } + if (!final_intersection && lines_intersect(p1.x, p1.y, p2.x, p2.y, rect.origin.x+rect.size.w, rect.origin.y, rect.origin.x+rect.size.w, rect.origin.y+rect.size.h, &x, &y) == DO_INTERSECT) { + if (!intersection) { + x1 = x; y1 = y; + } else if (x != x1 && y != y1) { + x2 = x; y2 = y; final_intersection = true; + } + } + if(final_intersection) { + return (Intersection) { .success = INTERSECTION_FULL, .p1 = (GPoint) { .x = x1, .y = y1 }, .p2 = (GPoint) { .x = x2, .y = y2 } }; + } else if (intersection) { + return (Intersection) { .success = INTERSECTION_PARTIAL, .p1 = (GPoint) { .x = x1, .y = y1 }, .p2 = (GPoint) { .x = x2, .y = y2 } }; + } else { + return (Intersection) { .success = INTERSECTION_NONE, .p1 = (GPoint) { .x = x1, .y = y1 }, .p2 = (GPoint) { .x = x2, .y = y2 } }; + } +} \ No newline at end of file diff --git a/src/squared.c b/src/squared.c index 3381196..1d7e93d 100644 --- a/src/squared.c +++ b/src/squared.c @@ -6,6 +6,8 @@ */ #include +#include + Window *window; @@ -610,8 +612,11 @@ static void updateSlot(Layer *layer, GContext *ctx) { GColor8 newColor = getSlotColor(tx, ty, slot->curDigit, slot->slotIndex, slot->isalpha); graphics_context_set_fill_color(ctx, oldColor); - graphics_fill_rect(ctx, GRect((tx*tilesize)-(tx*widthadjust), ty*tilesize-(ty*widthadjust), tilesize-widthadjust, tilesize-widthadjust), 0, GCornerNone); - + + GRect singletile = GRect((tx*tilesize)-(tx*widthadjust), ty*tilesize-(ty*widthadjust), tilesize-widthadjust, tilesize-widthadjust); + + graphics_fill_rect(ctx, singletile, 0, GCornerNone); + if(!gcolor_equal(oldColor, newColor)) { w = (skewedNormTime*TILE_SIZE/ANIMATION_NORMALIZED_MAX)+shift-widthadjust; if (w < 0) { @@ -622,6 +627,22 @@ static void updateSlot(Layer *layer, GContext *ctx) { graphics_context_set_fill_color(ctx, newColor); graphics_fill_rect(ctx, GRect((tx*tilesize)-(tx*widthadjust), ty*tilesize-(ty*widthadjust), w, tilesize-widthadjust), 0, GCornerNone); } + + GPoint line_start = GPoint(0,0); + GPoint line_end = GPoint(90,180); + + graphics_context_set_stroke_width(ctx, 1); + graphics_context_set_stroke_color(ctx, GColorRed); + //graphics_draw_line(ctx, line_start, line_end); + + Intersection intersect = rectintersect(line_start, line_end, singletile); + + if (intersect.success >= INTERSECTION_FULL && !gcolor_equal(newColor, BACKGROUND_COLOR)) { + graphics_context_set_stroke_color(ctx, GColorWhite); + graphics_context_set_stroke_width(ctx, 2); + graphics_draw_line(ctx, intersect.p1, intersect.p2); + //APP_LOG(APP_LOG_LEVEL_INFO, "intersection at %d,%d and %d,%d", intersect.p1.x, intersect.p1.y, intersect.p2.x, intersect.p2.y); + } } }