SHARE
TWEET

OZN în lan

rontav Jun 21st, 2015 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Vector2{
  6.     int x;
  7.     int y;
  8.     Vector2(int x = 0, int y = 0):x(x),y(y){}
  9. };
  10.  
  11. int DotProduct(Vector2 a, Vector2 b){
  12.     return a.x * b.x + a.y * b.y;
  13. }
  14. Vector2 CrossProduct(Vector2 a, Vector2 b){
  15.     return Vector2(a.x * b.y - a.y * b.x, 0);
  16. }
  17. bool SameSide(Vector2 p1, Vector2 p2, Vector2 a, Vector2 b){
  18.     Vector2 t, q, r;
  19.     t.x = b.x - a.x;
  20.     t.y = b.y - a.y;
  21.     q.x = p1.x - a.x;
  22.     q.y = p1.y - a.y;
  23.     r.x = p2.x - a.x;
  24.     r.y = p2.y - a.y;
  25.     Vector2 cp1 = CrossProduct(t, q);
  26.     Vector2 cp2 = CrossProduct(t, r);
  27.     if(DotProduct(cp1, cp2) >= 0){
  28.         return true;
  29.     }
  30.     else {
  31.         return false;
  32.     }
  33. }
  34. bool PointInTriangle(Vector2 p, Vector2 a, Vector2 b, Vector2 c){
  35.     if (SameSide(p, a, b, c) && SameSide(p, b, a, c) && SameSide(p, c, a, b)){
  36.         return true;
  37.     }
  38.     else{
  39.         return false;
  40.     }
  41. }
  42.  
  43. int main(){
  44.     Vector2 A, B, C;
  45.    
  46.     /* cout << "Ax:";
  47.     cin >> A.x;
  48.     cout << "Ay:";
  49.     cin >> A.y;
  50.     cout << "Bx:";
  51.     cin >> B.x;
  52.     cout << "By:";
  53.     cin >> B.y;
  54.     cout << "Cx:";
  55.     cin >> C.x;
  56.     cout << "Cy:";
  57.     cin >> C.y;*/
  58.    
  59.     A = Vector2(3, 1);
  60.     B = Vector2(1, 5);
  61.     C = Vector2(5, 4);
  62.  
  63.     int sum = 0;
  64.     int xmin = min(min(A.x, B.x), C.x);
  65.     int xmax = max(max(A.x, B.x), C.x);
  66.     int ymin = min(min(A.y, B.y), C.y);
  67.     int ymax = max(max(A.y, B.y), C.y);
  68.  
  69.     for(int i = ymin; i <= ymax; i++){
  70.         for(int j = xmin; j <= xmax; j++){
  71.             if(PointInTriangle(Vector2(j, i), A, B, C)){
  72.                 sum++;
  73.             }
  74.         }
  75.     }
  76.     cout << sum << '\n';
  77. }
RAW Paste Data
Top