// This file is part of the computer game Kartofel. // Copyright (C) 2008 Pawel Aleksander Fedorynski // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include #include #include #include #include "util.h" using namespace kartofel; using namespace std; namespace { void TestFI(int x0, int y0, int x1, int y1, int xc, int yc, int r, int res_size, int res0x = 0, int res0y = 0, int res1x = 0, int res1y = 0) { if (res_size == 2 && (res0x > res1x || (res0x == res1x && res0y > res1y))) { swap(res0x, res1x); swap(res0y, res1y); } vector res; stringstream ss; ss << "x0: " << x0 << ", y0: " << y0 << ", x1: " << x1 << ", y1: " << y1 << ", xc: " << xc << ", yc: " << yc << ", r: " << r << endl; ss << "Expected result: ["; if (res_size >= 1) { ss << "(" << res0x << ", " << res0y << ")"; } if (res_size >= 2) { ss << ", (" << res1x << ", " << res1y << ")"; } ss << "]\n"; FindIntersections(x0, y0, x1, y1, xc, yc, r, &res); if (res.size() == 2 && (res[0].x > res[1].x || (res[0].x == res[1].x && res[0].y > res[1].y))) { swap(res[0], res[1]); } ss << "Actual result: ["; for (int i = 0; i < res.size(); i++) { if (i == 1) { ss << ", "; } ss << "(" << res[i].x << ", " << res[i].y << ")"; } ss << "]\n"; if (res.size() != res_size) { cout << ss.str() << "FAIL\n"; exit(-1); } if (res_size >= 1 && (res[0].x != res0x || res[0].y != res0y)) { cout << ss.str() << "FAIL\n"; exit(-1); } if (res_size == 2 && (res[1].x != res1x || res[1].y != res1y)) { cout << ss.str() << "FAIL\n"; exit(-1); } //cout << ss.str(); } void TestLSI(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, bool res, int res_ix = 0, int res_iy = 0) { stringstream ss; ss << "x1: " << x1 << ", y1: " << y1 << ", x2: " << x2 << ", y2: " << y2 << ", x3: " << x3 << ", y3: " << y3 << ", x4: " << x4 << ", y4: " << y4 << endl; ss << "Expected result: " << (res ? "true" : "false"); if (res) { ss << " (" << res_ix << ", " << res_iy << ")"; } ss << endl; LineSegment ls1(x1, y1, x2, y2); LineSegment ls2(x3, y3, x4, y4); int ix, iy; bool isec = LineSegmentsIntersect(ls1, ls2, &ix, &iy); ss << "Actual result: " << (isec ? "true" : "false"); if (isec) { ss << " (" << ix << ", " << iy << ")"; } ss << endl; if (res != isec || (res && isec && (ix != res_ix || iy != res_iy))) { cout << ss.str() << "FAIL\n"; exit(-1); } //cout << ss.str(); } } // anonymous namespace int main() { TestFI(0, 0, 2, 0, 1, 1, 1, 1, 1, 0); TestFI(1, 1, 1000, 1000, 1000, 500, 30, 0); TestFI(2, 3, 100, 4, 1000, 200, 25, 0); TestFI(0, 0, 102, 102, 103, 103, 4, 1, 100, 100); TestFI(3, -1, 9, 5, 4, 4, 4, 2, 4, 0, 8, 4); TestFI(0, 0, 10000, 1, 0, 1, 1, 1, 0, 0); TestFI(5, 1, 9, 5, 4, 4, 4, 1, 8, 4); TestFI(0, 0, 8000, 6000, 1000, 7000, 5000, 1, 4000, 3000); TestFI(-1000, 1000, 9000, 6000, 1000, 7000, 5000, 2, 1000, 2000, 5000, 4000); TestFI(1300, 3200, 1600, 3500, 1100, 3700, 500, 2, 1400, 3300, 1500, 3400); TestFI(4, 3, 6, 7, 1, 7, 5, 2, 4, 3, 6, 7); TestFI(2, 0, 102, 100, 1, 7, 5, 0); TestFI(1000, 2300, 1040, 2300, 1305, 2300, 300, 1, 1005, 2300); TestLSI(1, 3, 5, 1, 1, 1, 7, 4, true, 3, 2); TestLSI(5000, 1000, 1000, 3000, 1000, 1000, 7000, 4000, true, 3000, 2000); TestLSI(0, 0, 10, 0, 5, -5, 5, 5, true, 5, 0); TestLSI(1, 1, 2, 2, 1, 1, 5, 0, true, 1, 1); TestLSI(0, 1, 1, 10, 1, 0, 10, 1, false); TestLSI(10, 10, 100, 10, 20, 10, 200, 10, true, 20, 10); TestLSI(100, 100, 100, 200, 100, 50, 100, 150, true, 100, 100); TestLSI(100, 10, 10, 10, 200, 10, 20, 10, true, 100, 10); TestLSI(100, 100, 100, 200, 100, 150, 100, 50, true, 100, 100); TestLSI(0, 0, 5000, 5000, 1, 0, 5001, 5000, false); TestLSI(0, 0, 100, 0, 200, 0, 90, 0, true, 90, 0); TestLSI(10, 1000, 20, 900, 30, 800, 60, 500, false); TestLSI(1000, 1000, 1060, 1060, 1030, 1050, 1031, 900, true, 1030, 1030); TestLSI(0, 0, 2, 4, 1, 2, 13, 1, true, 1, 2); TestLSI(10000, 1000, 1000, 2000, 2000, 4000, 0, 0, true, 1000, 2000); TestLSI(2800, 1700, 3000, 1700, 2400, 1700, 2600, 1700, false); TestLSI(2800, 1700, 3000, 1700, 3000, 8000, 3000, 8200, false); cout << "OK\n"; }