b2Triangle.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2007 Eric Jordan
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "b2Triangle.h"
  19. namespace b2ConvexDecomp {
  20. //Constructor automatically fixes orientation to ccw
  21. b2Triangle::b2Triangle(float32 x1, float32 y1, float32 x2, float32 y2, float32 x3, float32 y3){
  22. x = new float32[3];
  23. y = new float32[3];
  24. float32 dx1 = x2-x1;
  25. float32 dx2 = x3-x1;
  26. float32 dy1 = y2-y1;
  27. float32 dy2 = y3-y1;
  28. float32 cross = dx1*dy2-dx2*dy1;
  29. bool ccw = (cross>0);
  30. if (ccw){
  31. x[0] = x1; x[1] = x2; x[2] = x3;
  32. y[0] = y1; y[1] = y2; y[2] = y3;
  33. } else{
  34. x[0] = x1; x[1] = x3; x[2] = x2;
  35. y[0] = y1; y[1] = y3; y[2] = y2;
  36. }
  37. }
  38. b2Triangle::b2Triangle(){
  39. x = new float32[3];
  40. y = new float32[3];
  41. }
  42. b2Triangle::~b2Triangle(){
  43. delete[] x;
  44. delete[] y;
  45. }
  46. void b2Triangle::Set(const b2Triangle& toMe) {
  47. for (int32 i=0; i<3; ++i) {
  48. x[i] = toMe.x[i];
  49. y[i] = toMe.y[i];
  50. }
  51. }
  52. bool b2Triangle::IsInside(float32 _x, float32 _y){
  53. if (_x < x[0] && _x < x[1] && _x < x[2]) return false;
  54. if (_x > x[0] && _x > x[1] && _x > x[2]) return false;
  55. if (_y < y[0] && _y < y[1] && _y < y[2]) return false;
  56. if (_y > y[0] && _y > y[1] && _y > y[2]) return false;
  57. float32 vx2 = _x-x[0]; float32 vy2 = _y-y[0];
  58. float32 vx1 = x[1]-x[0]; float32 vy1 = y[1]-y[0];
  59. float32 vx0 = x[2]-x[0]; float32 vy0 = y[2]-y[0];
  60. float32 dot00 = vx0*vx0+vy0*vy0;
  61. float32 dot01 = vx0*vx1+vy0*vy1;
  62. float32 dot02 = vx0*vx2+vy0*vy2;
  63. float32 dot11 = vx1*vx1+vy1*vy1;
  64. float32 dot12 = vx1*vx2+vy1*vy2;
  65. float32 invDenom = 1.0f / (dot00*dot11 - dot01*dot01);
  66. float32 u = (dot11*dot02 - dot01*dot12)*invDenom;
  67. float32 v = (dot00*dot12 - dot01*dot02)*invDenom;
  68. return ((u>=0)&&(v>=0)&&(u+v<=1));
  69. }
  70. }