Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
1,870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. SDL_Point rotatePoint(const SDL_Point & origin, const SDL_Point & point, const float & s, const float & c){
  2.  
  3.     float xDif = point.x - origin.x;
  4.     float yDif = point.y - origin.y;
  5.  
  6.     float x3 = c * xDif  + (-s * yDif) + origin.x;
  7.     float y3 = s * xDif + c * yDif + origin.y;
  8.  
  9.     return {(int)x3, (int)y3};
  10.    
  11. }
  12.  
  13. bool pointOnSideOfEdge(const SDL_Point &A,const SDL_Point &B,const SDL_Point &test){
  14.     int a = -(B.y - A.y);
  15.     int b = B.x - A.x;
  16.     int c = -(a * A.x + b * A.y);
  17.     int d = a * test.x + b * test.y + c;
  18.     return d > 0;
  19. }
  20.  
  21. bool pointInRotatedRect(const SDL_Point & M, const SDL_Rect & R, const float & angle){
  22.     SDL_SetRenderDrawColor(gRenderer, 0xFF, 0, 0, 0xFF);
  23.     //SDL_RenderFillRect(gRenderer, &overLapRect);
  24.     float inRad = degreeToRadian(angle);
  25.  
  26.     float cosineOfAngle = std::cos(inRad);
  27.     float sineOfAngle = std::sin(inRad);
  28.  
  29.     SDL_Point center = { R.x + R.w / 2, R.y + R.h / 2 };
  30.  
  31.     SDL_Point A = rotatePoint(center, { R.x, R.y }, sineOfAngle, cosineOfAngle);
  32.     SDL_Point B = rotatePoint(center, { R.x + R.w, R.y }, sineOfAngle, cosineOfAngle);
  33.     SDL_Point C = rotatePoint(center, { R.x + R.w, R.y + R.h }, sineOfAngle, cosineOfAngle);
  34.     SDL_Point D = rotatePoint(center, { R.x, R.y + R.h }, sineOfAngle, cosineOfAngle);
  35.  
  36.     return pointOnSideOfEdge(A, B, M) && pointOnSideOfEdge(B, C, M) && pointOnSideOfEdge(C, D, M) && pointOnSideOfEdge(D, A, M);
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement