267{
270
271
272 const PointType2D edge0 (V1.x - V0.x, V1.y - V0.y);
273 const PointType2D edge1 (V2.x - V0.x, V2.y - V0.y);
274 const PointType2D PMinusV0( P.x - V0.x, P.y - V0.y);
275
276
278
279
280 const StorageType signedDistanceEdge0 = edge0.x * PMinusV0.y - edge0.y * PMinusV0.x;
281 const StorageType signedDistanceEdge1 = PMinusV0.x * edge1.y - PMinusV0.y * edge1.x;
282 const StorageType signedDistanceEdge2 = signedArea - signedDistanceEdge0 - signedDistanceEdge1;
283
284 if (signedArea > 0)
285 {
286
287 return signedDistanceEdge0 >= 0 && signedDistanceEdge1 >= 0 && signedDistanceEdge2 >= 0;
288 }
289 else if (signedArea < 0)
290 {
291
292 return signedDistanceEdge0 <= 0 && signedDistanceEdge1 <= 0 && signedDistanceEdge2 <= 0;
293 }
294 else {
295
296
297 if (signedDistanceEdge0 != 0 || signedDistanceEdge1 != 0)
298 {
299
300 return false;
301 }
302
303
304 const StorageType edge0LengthSquared = edge0.LengthSquared();
305 const StorageType edge1LengthSquared = edge1.LengthSquared();
306
307
308 if (edge0LengthSquared != 0)
309 {
310 if (edge1LengthSquared != 0)
311 {
312
313 const StorageType PDotEdge0 = PMinusV0.DotProd(edge0);
314 const StorageType PDotEdge1 = PMinusV0.DotProd(edge1);
315 return 0 <= PDotEdge0 && PDotEdge0 <= edge0LengthSquared ||
316 0 <= PDotEdge1 && PDotEdge1 <= edge1LengthSquared;
317 }
318 else
319 {
320
321 const StorageType PDotEdge0 = PMinusV0.DotProd(edge0);
322 return 0 <= PDotEdge0 && PDotEdge0 <= edge0LengthSquared;
323 }
324 }
325 else
326 {
327 if (edge1LengthSquared != 0)
328 {
329
330 const StorageType PDotEdge1 = PMinusV0.DotProd(edge1);
331 return 0 <= PDotEdge1 && PDotEdge1 <= edge1LengthSquared;
332 }
333 else
334 {
335
336 return PMinusV0.x == 0 && PMinusV0.y == 0;
337 }
338 }
339 }
340}