#include <stdlib.h>
#include <stdbool.h>
-struct line { int x1; int y1; int x2; int y2; };
+struct line { enum {horz, vert} d; int x1; int y1; int x2; int y2; };
#define max(x, y) ((x)>(y) ? (x) : (y))
#define min(x, y) ((x)<(y) ? (x) : (y))
lines[i].x2 = strtol(p, &p, 10);
p++;
lines[i].y2 = strtol(p, &p, 10);
+ if (lines[i].x1 == lines[i].x2)
+ lines[i].d = vert;
+ else if (lines[i].y1 == lines[i].y2)
+ lines[i].d = horz;
+ else
+ continue;
*maxx = max(*maxx, lines[i].x1);
*maxx = max(*maxx, lines[i].x2);
*maxy = max(*maxy, lines[i].y1);
bool online(int x, int y, struct line line)
{
- //Vertical
- if (line.x1 == line.x2 && x == line.x1)
- return between(y, line.y1, line.y2);
- //Horizontal
- if (line.y1 == line.y2 && y == line.y2)
- return between(x, line.x1, line.x2);
+ switch (line.d) {
+ case vert:
+ return x == line.x1 && between(y, line.y1, line.y2);
+ case horz:
+ return y == line.y1 && between(x, line.x1, line.x2);
+ }
return false;
}
#include <stdlib.h>
#include <stdbool.h>
-struct line { int x1; int y1; int x2; int y2; };
+struct line { enum {horz, vert, diag} d; int x1; int y1; int x2; int y2; };
#define max(x, y) ((x)>(y) ? (x) : (y))
#define min(x, y) ((x)<(y) ? (x) : (y))
lines[i].x2 = strtol(p, &p, 10);
p++;
lines[i].y2 = strtol(p, &p, 10);
+ if (lines[i].x1 == lines[i].x2)
+ lines[i].d = vert;
+ else if (lines[i].y1 == lines[i].y2)
+ lines[i].d = horz;
+ else if (abs(lines[i].y2-lines[i].y1) == abs(lines[i].x2-lines[i].x1))
+ lines[i].d = diag;
+ else
+ continue;
*maxx = max(*maxx, lines[i].x1);
*maxx = max(*maxx, lines[i].x2);
*maxy = max(*maxy, lines[i].y1);
bool online(int x, int y, struct line line)
{
- //Vertical
- if (line.x1 == line.x2 && x == line.x1)
- return between(y, line.y1, line.y2);
- //Horizontal
- if (line.y1 == line.y2 && y == line.y2)
- return between(x, line.x1, line.x2);
- //Diagonal
- if (abs(line.y2-line.y1) == abs(line.x2-line.x1)) {
- int slope = (line.y2 - line.y1) / (line.x2 - line.x1);
+ int slope;
+ switch (line.d) {
+ case horz:
+ return y == line.y1 && between(x, line.x1, line.x2);
+ case vert:
+ return x == line.x1 && between(y, line.y1, line.y2);
+ case diag:
+ slope = (line.y2 - line.y1) / (line.x2 - line.x1);
return y-line.y1 == slope*(x-line.x1)
&& between(y, line.y1, line.y2) && between(x, line.x1, line.x2);
}