1
1
Fork 0

first blood ... err stripes

squarelines
Alina Marquardt 2016-01-25 23:47:14 +01:00
parent 517d0180dc
commit 7679c3a7ff
2 changed files with 132 additions and 2 deletions

109
src/intersection.h Normal file
View File

@ -0,0 +1,109 @@
#include <pebble.h>
#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 } };
}
}

View File

@ -6,6 +6,8 @@
*/ */
#include <pebble.h> #include <pebble.h>
#include <intersection.h>
Window *window; Window *window;
@ -610,8 +612,11 @@ static void updateSlot(Layer *layer, GContext *ctx) {
GColor8 newColor = getSlotColor(tx, ty, slot->curDigit, slot->slotIndex, slot->isalpha); GColor8 newColor = getSlotColor(tx, ty, slot->curDigit, slot->slotIndex, slot->isalpha);
graphics_context_set_fill_color(ctx, oldColor); 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)) { if(!gcolor_equal(oldColor, newColor)) {
w = (skewedNormTime*TILE_SIZE/ANIMATION_NORMALIZED_MAX)+shift-widthadjust; w = (skewedNormTime*TILE_SIZE/ANIMATION_NORMALIZED_MAX)+shift-widthadjust;
if (w < 0) { if (w < 0) {
@ -622,6 +627,22 @@ static void updateSlot(Layer *layer, GContext *ctx) {
graphics_context_set_fill_color(ctx, newColor); 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); 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);
}
} }
} }