Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Vector2{
- int x;
- int y;
- Vector2(int x = 0, int y = 0):x(x),y(y){}
- };
- int DotProduct(Vector2 a, Vector2 b){
- return a.x * b.x + a.y * b.y;
- }
- Vector2 CrossProduct(Vector2 a, Vector2 b){
- return Vector2(a.x * b.y - a.y * b.x, 0);
- }
- bool SameSide(Vector2 p1, Vector2 p2, Vector2 a, Vector2 b){
- Vector2 t, q, r;
- t.x = b.x - a.x;
- t.y = b.y - a.y;
- q.x = p1.x - a.x;
- q.y = p1.y - a.y;
- r.x = p2.x - a.x;
- r.y = p2.y - a.y;
- Vector2 cp1 = CrossProduct(t, q);
- Vector2 cp2 = CrossProduct(t, r);
- if(DotProduct(cp1, cp2) >= 0){
- return true;
- }
- else {
- return false;
- }
- }
- bool PointInTriangle(Vector2 p, Vector2 a, Vector2 b, Vector2 c){
- if (SameSide(p, a, b, c) && SameSide(p, b, a, c) && SameSide(p, c, a, b)){
- return true;
- }
- else{
- return false;
- }
- }
- int main(){
- Vector2 A, B, C;
- /* cout << "Ax:";
- cin >> A.x;
- cout << "Ay:";
- cin >> A.y;
- cout << "Bx:";
- cin >> B.x;
- cout << "By:";
- cin >> B.y;
- cout << "Cx:";
- cin >> C.x;
- cout << "Cy:";
- cin >> C.y;*/
- A = Vector2(3, 1);
- B = Vector2(1, 5);
- C = Vector2(5, 4);
- int sum = 0;
- int xmin = min(min(A.x, B.x), C.x);
- int xmax = max(max(A.x, B.x), C.x);
- int ymin = min(min(A.y, B.y), C.y);
- int ymax = max(max(A.y, B.y), C.y);
- for(int i = ymin; i <= ymax; i++){
- for(int j = xmin; j <= xmax; j++){
- if(PointInTriangle(Vector2(j, i), A, B, C)){
- sum++;
- }
- }
- }
- cout << sum << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement