点在多边形内判断,网格法(从另一份92年的代码学起)
2022-01-07 本文已影响0人
寽虎非虫003
零、代码来源
一、一点小说明
我上网差的国内的大部分的网页都只说了射线法,少量的提到了什么弧度和,面积法啊之类的,几乎没人提过这个网格法,还有另一个Spackman法什么的,我也不知道是因为这些方法并不像文献对比里面那样高效,还是说,大部分人的了解直接就一个射线法能用就行了。
这篇文章只截取代码里面关于网格法的部分尝试学习,像看其他部分源代码的朋友请自行前往代码来源下载。
二、源代码
结构体部分
/* =========== Grid stuff ================================================= */
#define GR_FULL_VERT 0x01 /* line crosses vertically(线垂直穿过) */
#define GR_FULL_HORZ 0x02 /* line crosses horizontally(线水平穿过) */
/*这个应该是单个网格本身的内容*/
typedef struct {
double xa, ya; /*这个并不清楚是记录什么的*/
double minx, maxx, miny, maxy; /*单个网格的上下左右极值还是能看懂*/
double ax, ay; /*这个也不清楚是记录什么的*/
double slope, inv_slope; /*这个也不清楚是记录什么的*/
} GridRec, * pGridRec;
#define GC_BL_IN 0x0001 /* bottom left corner is in (else out) */
#define GC_BR_IN 0x0002 /* bottom right corner is in (else out) */
#define GC_TL_IN 0x0004 /* top left corner is in (else out) */
#define GC_TR_IN 0x0008 /* top right corner is in (else out) */
#define GC_L_EDGE_HIT 0x0010 /* left edge is crossed */
#define GC_R_EDGE_HIT 0x0020 /* right edge is crossed */
#define GC_B_EDGE_HIT 0x0040 /* bottom edge is crossed */
#define GC_T_EDGE_HIT 0x0080 /* top edge is crossed */
#define GC_B_EDGE_PARITY 0x0100 /* bottom edge parity */
#define GC_T_EDGE_PARITY 0x0200 /* top edge parity */
#define GC_AIM_L (0<<10) /* aim towards left edge */
#define GC_AIM_B (1<<10) /* aim towards bottom edge */
#define GC_AIM_R (2<<10) /* aim towards right edge */
#define GC_AIM_T (3<<10) /* aim towards top edge */
#define GC_AIM_C (4<<10) /* aim towards a corner */
#define GC_AIM 0x1c00
#define GC_L_EDGE_CLEAR GC_L_EDGE_HIT
#define GC_R_EDGE_CLEAR GC_R_EDGE_HIT
#define GC_B_EDGE_CLEAR GC_B_EDGE_HIT
#define GC_T_EDGE_CLEAR GC_T_EDGE_HIT
#define GC_ALL_EDGE_CLEAR (GC_L_EDGE_HIT | \
GC_R_EDGE_HIT | \
GC_B_EDGE_HIT | \
GC_T_EDGE_HIT )
typedef struct {
short tot_edges;
unsigned short gc_flags;
GridRec* gr;
} GridCell, * pGridCell;
typedef struct {
int xres, yres; /* grid size */
int tot_cells; /* xres * yres */
double minx, maxx, miny, maxy; /* bounding box */
double xdelta, ydelta;
double inv_xdelta, inv_ydelta;
double* glx, * gly;
GridCell* gc;
} GridSet, * pGridSet;