PuzzleSDK
Bug-1174.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25//
26// Bug-1174
27// http://code.google.com/p/cocos2d-iphone/issues/detail?id=1174
28//
29
30#include "Bug-1174.h"
31
33
34int check_for_error( Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4, float s, float t );
35
36int check_for_error( Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4, float s, float t )
37{
38 // the hit point is p3 + t * (p4 - p3);
39 // the hit point also is p1 + s * (p2 - p1);
40
41 auto p4_p3 = p4 - p3;
42 auto p4_p3_t = p4_p3 * t;
43 auto hitPoint1 = p3 + p4_p3_t;
44
45 auto p2_p1 = p2 - p1;
46 auto p2_p1_s = p2_p1 * s;
47 auto hitPoint2 = p1 + p2_p1_s;
48
49 // Since float has rounding errors, only check if diff is < 0.05
50 if( (fabs( hitPoint1.x - hitPoint2.x) > 0.1f) || ( fabs(hitPoint1.y - hitPoint2.y) > 0.1f) )
51 {
52 log("ERROR: (%f,%f) != (%f,%f)", hitPoint1.x, hitPoint1.y, hitPoint2.x, hitPoint2.y);
53 return 1;
54 }
55
56 return 0;
57}
58
60{
61 if (BugsTestBase::init())
62 {
63// // seed
64// std::srand(0);
65
66 Vec2 A,B,C,D,p1,p2,p3,p4;
67 float s,t;
68
69 int err=0;
70 int ok=0;
71
72 //
73 // Test 1.
74 //
75 log("Test1 - Start");
76 for( int i=0; i < 10000; i++)
77 {
78 // A | b
79 // -----
80 // c | d
81 float ax = CCRANDOM_0_1() * -5000;
82 float ay = CCRANDOM_0_1() * 5000;
83
84 // a | b
85 // -----
86 // c | D
87 float dx = CCRANDOM_0_1() * 5000;
88 float dy = CCRANDOM_0_1() * -5000;
89
90 // a | B
91 // -----
92 // c | d
93 float bx = CCRANDOM_0_1() * 5000;
94 float by = CCRANDOM_0_1() * 5000;
95
96 // a | b
97 // -----
98 // C | d
99 float cx = CCRANDOM_0_1() * -5000;
100 float cy = CCRANDOM_0_1() * -5000;
101
102 A = Vec2(ax,ay);
103 B = Vec2(bx,by);
104 C = Vec2(cx,cy);
105 D = Vec2(dx,dy);
106 if( Vec2::isLineIntersect( A, D, B, C, &s, &t) ) {
107 if( check_for_error(A, D, B, C, s, t) )
108 err++;
109 else
110 ok++;
111 }
112 }
113 log("Test1 - End. OK=%i, Err=%i", ok, err);
114
115 //
116 // Test 2.
117 //
118 log("Test2 - Start");
119
120 p1 = Vec2(220,480);
121 p2 = Vec2(304,325);
122 p3 = Vec2(264,416);
123 p4 = Vec2(186,416);
124 s = 0.0f;
125 t = 0.0f;
126 if( Vec2::isLineIntersect(p1, p2, p3, p4, &s, &t) )
127 check_for_error(p1, p2, p3, p4, s,t );
128
129 log("Test2 - End");
130
131
132 //
133 // Test 3
134 //
135 log("Test3 - Start");
136
137 ok=0;
138 err=0;
139 for( int i=0;i<10000;i++)
140 {
141 // A | b
142 // -----
143 // c | d
144 float ax = CCRANDOM_0_1() * -500;
145 float ay = CCRANDOM_0_1() * 500;
146 p1 = Vec2(ax,ay);
147
148 // a | b
149 // -----
150 // c | D
151 float dx = CCRANDOM_0_1() * 500;
152 float dy = CCRANDOM_0_1() * -500;
153 p2 = Vec2(dx,dy);
154
155
157
158 float y = ay - ((ay - dy) /2.0f);
159
160 // a | b
161 // -----
162 // C | d
163 float cx = CCRANDOM_0_1() * -500;
164 p3 = Vec2(cx,y);
165
166 // a | B
167 // -----
168 // c | d
169 float bx = CCRANDOM_0_1() * 500;
170 p4 = Vec2(bx,y);
171
172 s = 0.0f;
173 t = 0.0f;
174 if( Vec2::isLineIntersect(p1, p2, p3, p4, &s, &t) ) {
175 if( check_for_error(p1, p2, p3, p4, s,t ) )
176 err++;
177 else
178 ok++;
179 }
180 }
181
182 log("Test3 - End. OK=%i, err=%i", ok, err);
183 return true;
184 }
185
186 return false;
187}
USING_NS_CC
Definition: Bug-1174.cpp:32
int check_for_error(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4, float s, float t)
Definition: Bug-1174.cpp:36
virtual bool init() override
Definition: Bug-1174.cpp:59