loops.cc
来自「手写识别是模式识别中研究得一个热点」· CC 代码 · 共 176 行
CC
176 行
#include <SLList.h>#include "HWRawDataC.hh"#define MYDEBUG 0#define MIN(A,B) (A<B?A:B)#define MAX(A,B) (A>B?A:B)float max4(float A, float B, float C, float D);float min4(float A, float B, float C, float D);// Returns 1 if line P1-P2 crosses point P3-P4. int DoCross(HWDataPointC P1, HWDataPointC P2, HWDataPointC P3, HWDataPointC P4);int main(void){ HWHeaderC Header; cin >> Header; cout << Header; SLList<HWDataPointC> Points; int PenUp = 1; while (1) { HWRawDataC ThisRecord; cin >> ThisRecord; if (!cin) break; cout << ThisRecord;// cerr << "Working on record: " << ThisRecord;// cerr << "List length = " << Points.length() << "\n"; if (ThisRecord.Type == 1 || ThisRecord.Type == 2) { // keep track if pen is up or down. PenUp = ThisRecord.Type-1; } if (PenUp) Points.clear(); if (ThisRecord.Type == 0 && Points.length() > 3) { for (Pix FirstPoint = Points.first(); ; Points.next(FirstPoint)) { Pix SecPoint = FirstPoint; Points.next(SecPoint); Pix ThirdPoint = SecPoint; Points.next(ThirdPoint); if (ThirdPoint == 0) break; if (DoCross(Points(FirstPoint), Points(SecPoint), Points.rear(), ThisRecord.DataPoint)) { cout << "6 0\n"; cerr << "Cross at: " << Points(FirstPoint) << " - " << Points(SecPoint) << " crosses " << Points.rear() << " - " << ThisRecord.DataPoint << ".\n"; } } } if (ThisRecord.Type == 0) Points.append(ThisRecord.DataPoint); } return 0;}int DoCross(HWDataPointC P1, HWDataPointC P2, HWDataPointC P3, HWDataPointC P4){ if (MYDEBUG) cerr << "Finding cross on " << P1 << "-" << P2 << " and " << P3 << "-" << P4 << ".\n"; if (P1 == P2 || P3 == P4) { if (MYDEBUG) cerr << "Ignoring line with no length.\n"; return 0; } if (P1.XPos == P2.XPos && P3.YPos == P4.YPos) { if (MYDEBUG) cerr << "Doing special case cross. P1/P2 is horizontal.\n"; if (MIN(P3.XPos, P4.XPos) <= P1.XPos && P1.XPos <= MAX(P3.XPos, P4.XPos) && MIN(P1.YPos, P2.YPos) <= P3.YPos && P3.YPos <= MAX(P1.YPos, P2.YPos)) return 1; else return 0; } if (P1.YPos == P2.YPos && P3.XPos == P4.XPos) { if (MYDEBUG) cerr << "Doing special case cross. P1/P2 is vertical.\n"; if (MIN(P1.XPos, P2.XPos) <= P3.XPos && P3.XPos <= MAX(P1.XPos, P2.XPos) && MIN(P3.YPos, P4.YPos) <= P1.YPos && P1.YPos <= MAX(P3.YPos, P4.YPos)) return 1; else return 0; } float m1 = (float)(P2.YPos - P1.YPos) / (P2.XPos - P1.XPos); float m2 = (float)(P4.YPos - P3.YPos) / (P4.XPos - P3.XPos); float b1 = P1.YPos - m1*P1.XPos; float b2 = P3.YPos - m2*P3.XPos; float X,Y; if (P3.XPos == P4.XPos) { Y = m1*P3.XPos + b1; X = P3.XPos; } else if (P1.XPos == P2.XPos) { Y = m1*P1.XPos + b1; X = P1.XPos; } else if (P1.YPos == P2.YPos) { X = (P1.YPos-b2)/m2; Y = P1.YPos; } else if (P3.YPos == P4.YPos) { X = (P3.YPos-b1)/m1; Y = P3.YPos; } else { Y = (b1-m1*b2/m2)/(1-m1/m2); X = (Y-b1)/m1; } if (MYDEBUG) { cerr << "Calculating intercept of " << P1 << "-" << P2 << " and " << P3 << "-" << P4 << "\n"; cerr << "m1 = " << m1 << ", m2 = " << m2 << ", b1 = " << b1 << ", b2 = " << b2 << "\n"; cerr << "intersect at " << X << ", " << Y << ".\n"; cerr << "Output: " << (MIN(P1.XPos, P2.XPos) <= X) << (X <= MAX(P1.XPos, P2.XPos)) << (MIN(P1.YPos, P2.YPos) <= Y) << (Y <= MAX(P1.YPos, P2.YPos)) << (MIN(P3.XPos, P4.XPos) <= X) << (X <= MAX(P3.XPos, P4.XPos)) << (MIN(P3.YPos, P4.YPos) <= Y) << (Y <= MAX(P3.YPos, P4.YPos)) << "\n"; } return MIN(P1.XPos, P2.XPos) <= X && X <= MAX(P1.XPos, P2.XPos) && MIN(P1.YPos, P2.YPos) <= Y && Y <= MAX(P1.YPos, P2.YPos) && MIN(P3.XPos, P4.XPos) <= X && X <= MAX(P3.XPos, P4.XPos) && MIN(P3.YPos, P4.YPos) <= Y && Y <= MAX(P3.YPos, P4.YPos); }float min4(float A, float B, float C, float D){ float T1 = A<B?A:B; float T2 = T1<C?T1:C; float T3 = T2<D?T2:D; return T3;}float max4(float A, float B, float C, float D){ float T1 = A>B?A:B; float T2 = T1>C?T1:C; float T3 = T2>D?T2:D; return T3;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?