Created
August 16, 2018 03:26
-
-
Save wujianguo/6fcee339c54c4fa4e7ba2f5585006b13 to your computer and use it in GitHub Desktop.
Seven-segment_display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct point { | |
double x,y; | |
} POINT; | |
typedef struct ray { | |
POINT p,v; | |
} RAY; | |
typedef struct polygon { | |
int n; | |
POINT *v; | |
} POLYGON; | |
/* | |
* This function implements the Jordan-curve test. | |
* It returns 1 if the point is inside the polygon, | |
* and 0 if it is outside. | |
*/ | |
static int pointIsInside(POINT *pt, POLYGON *poly) { | |
int i, j, c = 0; | |
int n = poly->n; | |
POINT *v = poly->v; | |
double x = pt->x; | |
double y = pt->y; | |
for (i = 0, j = n-1; i < n; j = i++) { | |
/* | |
* Flip a bit for each edge that: | |
* straddles the x-axis (going up or going down), and | |
* intersects the x-axis at a positive value of x. | |
*/ | |
if ((((v[i].y <= y) && (y < v[j].y)) || | |
((v[j].y <= y) && (y < v[i].y))) && | |
(x < (v[j].x - v[i].x) * (y - v[i].y) / (v[j].y - v[i].y) + v[i].x)) | |
c = !c; | |
} | |
return c; | |
} | |
static void one_drawer(unsigned char *buf, unsigned x, unsigned y, unsigned w, unsigned h) { | |
POINT a[6] = { | |
{.x = 1, .y = 1}, | |
{.x = 2, .y = 0}, | |
{.x = 8, .y = 0}, | |
{.x = 9, .y = 1}, | |
{.x = 8, .y = 2}, | |
{.x = 2, .y = 2} | |
}; | |
POLYGON polygon_a = { | |
.n = 6, | |
.v = a | |
}; | |
POINT b[6] = { | |
{.x = 9, .y = 1}, | |
{.x = 10,.y = 2}, | |
{.x = 10,.y = 8}, | |
{.x = 9, .y = 9}, | |
{.x = 8, .y = 8}, | |
{.x = 8, .y = 2} | |
}; | |
POLYGON polygon_b = { | |
.n = 6, | |
.v = b | |
}; | |
POINT c[6] = { | |
{.x = 9, .y = 9}, | |
{.x = 10,.y = 10}, | |
{.x = 10,.y = 16}, | |
{.x = 9, .y = 17}, | |
{.x = 8, .y = 16}, | |
{.x = 8, .y = 10} | |
}; | |
POLYGON polygon_c = { | |
.n = 6, | |
.v = c | |
}; | |
POINT d[6] = { | |
{.x = 9, .y = 17}, | |
{.x = 8, .y = 18}, | |
{.x = 2, .y = 18}, | |
{.x = 1, .y = 17}, | |
{.x = 2, .y = 16}, | |
{.x = 8, .y = 16} | |
}; | |
POLYGON polygon_d = { | |
.n = 6, | |
.v = d | |
}; | |
POINT e[6] = { | |
{.x = 1, .y = 17}, | |
{.x = 0, .y = 16}, | |
{.x = 0, .y = 10}, | |
{.x = 1, .y = 9}, | |
{.x = 2, .y = 10}, | |
{.x = 2, .y = 16} | |
}; | |
POLYGON polygon_e = { | |
.n = 6, | |
.v = e | |
}; | |
POINT f[6] = { | |
{.x = 1, .y = 9}, | |
{.x = 0, .y = 8}, | |
{.x = 0, .y = 2}, | |
{.x = 1, .y = 1}, | |
{.x = 2, .y = 2}, | |
{.x = 2, .y = 8} | |
}; | |
POLYGON polygon_f = { | |
.n = 6, | |
.v = f | |
}; | |
POINT g[6] = { | |
{.x = 1, .y = 9}, | |
{.x = 2, .y = 8}, | |
{.x = 8, .y = 8}, | |
{.x = 9, .y = 9}, | |
{.x = 8, .y = 10}, | |
{.x = 2, .y = 10} | |
}; | |
POLYGON polygon_g = { | |
.n = 6, | |
.v = g | |
}; | |
POINT z[6] = { | |
{.x = -1,.y = -1}, | |
{.x = -1,.y = -1}, | |
{.x = -1,.y = -1}, | |
{.x = -1,.y = -1}, | |
{.x = -1,.y = -1}, | |
{.x = -1,.y = -1} | |
}; | |
POLYGON polygon_z = { | |
.n = 6, | |
.v = z | |
}; | |
POLYGON polygons[7] = {0}; | |
POINT p = {.x = x, .y = y}; | |
int inside = 0; | |
POINT temp_point[6] = {0}; | |
POLYGON temp_polygon = { | |
.n = 6, | |
.v = temp_point | |
}; | |
char str[] = "20180909120909"; | |
// char str[] = "8"; | |
unsigned x_offset_base = 10; | |
unsigned y_offset_base = 50; | |
for (unsigned str_index = 0; str_index<strlen(str); ++str_index) { | |
unsigned index = 0; | |
switch (str[str_index]) { | |
case '0': | |
polygons[index++] = polygon_a; | |
polygons[index++] = polygon_b; | |
polygons[index++] = polygon_c; | |
polygons[index++] = polygon_d; | |
polygons[index++] = polygon_e; | |
polygons[index++] = polygon_f; | |
polygons[index++] = polygon_z; | |
break; | |
case '1': | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_b; | |
polygons[index++] = polygon_c; | |
break; | |
case '2': | |
polygons[index++] = polygon_a; | |
polygons[index++] = polygon_b; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_d; | |
polygons[index++] = polygon_e; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_g; | |
break; | |
case '3': | |
polygons[index++] = polygon_a; | |
polygons[index++] = polygon_b; | |
polygons[index++] = polygon_c; | |
polygons[index++] = polygon_d; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_g; | |
break; | |
case '4': | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_b; | |
polygons[index++] = polygon_c; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_f; | |
polygons[index++] = polygon_g; | |
break; | |
case '5': | |
polygons[index++] = polygon_a; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_c; | |
polygons[index++] = polygon_d; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_f; | |
polygons[index++] = polygon_g; | |
break; | |
case '6': | |
polygons[index++] = polygon_a; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_c; | |
polygons[index++] = polygon_d; | |
polygons[index++] = polygon_e; | |
polygons[index++] = polygon_f; | |
polygons[index++] = polygon_g; | |
break; | |
case '7': | |
polygons[index++] = polygon_a; | |
polygons[index++] = polygon_b; | |
polygons[index++] = polygon_c; | |
break; | |
case '8': | |
polygons[index++] = polygon_a; | |
polygons[index++] = polygon_b; | |
polygons[index++] = polygon_c; | |
polygons[index++] = polygon_d; | |
polygons[index++] = polygon_e; | |
polygons[index++] = polygon_f; | |
polygons[index++] = polygon_g; | |
break; | |
case '9': | |
polygons[index++] = polygon_a; | |
polygons[index++] = polygon_b; | |
polygons[index++] = polygon_c; | |
polygons[index++] = polygon_d; | |
polygons[index++] = polygon_z; | |
polygons[index++] = polygon_f; | |
polygons[index++] = polygon_g; | |
break; | |
default: | |
break; | |
} | |
for (unsigned i = 0; i < index; ++i) { | |
for (unsigned z = 0; z < temp_polygon.n; ++z) { | |
temp_point[z] = polygons[i].v[z]; | |
} | |
for (unsigned j = 0; j < temp_polygon.n; ++j) { | |
temp_polygon.v[j].x *= 2; | |
temp_polygon.v[j].y *= 2; | |
temp_polygon.v[j].x += x_offset_base + str_index * 30; | |
temp_polygon.v[j].y += y_offset_base; | |
if (i == 0 || i == 3 || i == 6) { | |
temp_polygon.v[j].x += 1; | |
} | |
if (i == 1 || i == 2) { | |
temp_polygon.v[j].x += 1 * 2; | |
} | |
if (i == 1 || i == 5) { | |
temp_polygon.v[j].y += 1; | |
} | |
if (i == 6) { | |
temp_polygon.v[j].y += 1 * 2; | |
} | |
if (i == 2 || i == 4) { | |
temp_polygon.v[j].y += 1 * 3; | |
} | |
if (i == 3) { | |
temp_polygon.v[j].y += 1 * 4; | |
} | |
} | |
inside = pointIsInside(&p, &temp_polygon); | |
if (inside > 0) { | |
break; | |
} | |
} | |
if (inside > 0) { | |
break; | |
} | |
} | |
if (inside > 0) { | |
fill_color(buf, 0xFF, 0xFF, 0xFF); | |
} else { | |
fill_color(buf, 0, 0, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment