Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct point
- {
- int x, y;
- };
- int inRectangle(struct point p1, struct point p2, int dotX, int dotY)
- {
- struct point dot;
- dot.x = dotX;
- dot.y = dotY;
- if (dot.x >= p1.x && dot.x <= p2.x)
- {
- if ( dot.y >= p1.y && dot.y <= p2.y)
- {
- return 1;
- }
- }
- return 0;
- }
- int main(void)
- {
- struct point first_p1, first_p2; // Первый прямоугольник
- struct point second_p1, second_p2; // Второй прямоугольник
- struct point dot1, dot2; // Точки пересечения
- char a = 0, b = 0, f = 0, f2 = 0, ni = 1;
- /*
- a - кол-во точек в первом прямоугольнике
- b - кол-во точек в втором прямоугольнике
- f, f2 - стороны
- Формат прямоугольников и точек прересечения:
- |-----p2
- | |
- p1-----|
- */
- printf("First rectangle:\n");
- printf("x1 y1: ");
- scanf("%d %d", &first_p1.x, &first_p1.y);
- printf("x2 y2: ");
- scanf("%d %d", &first_p2.x, &first_p2.y);
- printf("Second rectangle:\n");
- printf("x1 y1: ");
- scanf("%d %d", &second_p1.x, &second_p1.y);
- printf("x2 y2: ");
- scanf("%d %d", &second_p2.x, &second_p2.y);
- /*
- Узнаем кол-во точек первого прямоугольника в втором
- и кол-во точек второго прямоугольника в первом.
- + Узнаем сторону.
- Проверка точек в порядке(f, f2):
- 4 1
- /\ \/
- 3 < 2
- */
- // Сколько точек второго прямоугольника в первом
- if (inRectangle(first_p1, first_p2, second_p2.x, second_p2.y) == 1)
- {
- a++;
- f = 1;
- }
- if (inRectangle(first_p1, first_p2, second_p2.x, second_p1.y) == 1)
- {
- a++;
- f = 2;
- }
- if (inRectangle(first_p1, first_p2, second_p1.x, second_p1.y) == 1)
- {
- a++;
- f = 3;
- }
- if (inRectangle(first_p1, first_p2, second_p1.x, second_p2.y) == 1)
- {
- a++;
- f = 4;
- }
- // Скольо точек первого прямоугольника в втором
- if (inRectangle(second_p1, second_p2, first_p2.x, first_p2.y) == 1)
- {
- b++;
- f2 = 1;
- }
- if (inRectangle(second_p1, second_p2, first_p2.x, first_p1.y) == 1)
- {
- b++;
- f2 = 2;
- }
- if (inRectangle(second_p1, second_p2, first_p1.x, first_p1.y) == 1)
- {
- b++;
- f2 = 3;
- }
- if (inRectangle(second_p1, second_p2, first_p1.x, first_p2.y) == 1)
- {
- b++;
- f2 = 4;
- }
- /*
- Узнаем точки пересечения
- */
- if (a == b && a != 0) // Кол-во точек одинаково
- {
- if (f == 1)
- {
- dot1 = first_p1;
- dot2 = second_p2;
- }
- else if (f == 2)
- {
- dot1.x = first_p1.x;
- dot1.y = second_p1.y;
- dot2.x = second_p2.x;
- dot2.y = first_p2.y;
- }
- else if (f == 3)
- {
- dot1 = second_p1;
- dot2 = first_p2;
- }
- else if (f == 4)
- {
- dot1.x = second_p1.x;
- dot1.y = first_p1.y;
- dot2.x = first_p2.x;
- dot2.y = second_p2.y;
- }
- }
- else if (a == 2 && b == 0) // Если две точки второго в первом
- {
- if (f == 2)
- {
- dot1.x = first_p1.x;
- dot1.y = second_p1.y;
- dot2 = second_p2;
- }
- else if (f == 3)
- {
- dot1 = second_p1;
- dot2.x = second_p2.x;
- dot2.y = first_p2.y;
- }
- else if (f == 4)
- {
- if (inRectangle(first_p1, first_p2, second_p1.x, second_p1.y) == 1)
- {
- dot1 = second_p1;
- dot2.x = first_p2.x;
- dot2.y = second_p2.y;
- }
- else if (inRectangle(first_p1, first_p2, second_p2.x, second_p2.y) == 1)
- {
- dot1.x = second_p1.x;
- dot1.y = first_p1.y;
- dot2 = second_p2;
- }
- }
- }
- else if (a == 0 && b == 2) // Если две точки первого в втором
- {
- if (f2 == 2)
- {
- dot1.x = second_p1.x;
- dot1.y = first_p1.y;
- dot2 = first_p2;
- }
- else if (f2 == 3)
- {
- dot1 = first_p1;
- dot2.x = first_p2.x;
- dot2.y = second_p2.y;
- }
- else if (f2 == 4)
- {
- if (inRectangle(second_p1, second_p2, first_p1.x, first_p1.y) == 1)
- {
- dot1 = first_p1;
- dot2.x = second_p2.x;
- dot2.y = first_p2.y;
- }
- else if (inRectangle(second_p1, second_p2, first_p2.x, first_p2.y) == 1)
- {
- dot1.x = first_p1.x;
- dot1.y = second_p1.y;
- dot2 = first_p2;
- }
- }
- }
- else if (a == 4 && b == 0) // Если второй входит в первый
- {
- dot1 = second_p1;
- dot2 = second_p2;
- }
- else if (a == 0 && b == 4) // Если первый входит в второй
- {
- dot1 = first_p1;
- dot2 = first_p2;
- }
- else if (a == 0 && b == 0) //
- {
- /* if (first_p1.x == second_p1.x && first_p1.y == second_p1.y) // Если один наложен на другой
- {
- if (first_p2.x == second_p2.x && first_p2.y == second_p2.y)
- {
- dot1 = first_p1;
- dot2 = second_p2;
- }
- } */
- if (second_p1.x < first_p1.x && second_p2.x > first_p2.x)
- {
- if (second_p1.y > first_p1.y && second_p1.y < first_p2.y)
- {
- if (second_p2.y > first_p1.y && second_p2.y < first_p2.y)
- {
- dot1.x = first_p1.x;
- dot1.y = second_p1.y;
- dot2.x = first_p2.x;
- dot2.y = second_p2.y;
- }
- }
- }
- else if (second_p1.y < first_p1.y && second_p2.y > first_p2.y)
- {
- if (second_p1.x > first_p1.x && second_p1.x < first_p2.x)
- {
- if (second_p2.x > first_p1.x && second_p2.x < first_p2.x)
- {
- dot1.x = second_p1.x;
- dot1.y = first_p1.y;
- dot2.x = second_p2.x;
- dot2.y = first_p2.y;
- }
- }
- }
- else
- {
- ni = 0;
- printf("not intersect\n");
- }
- }
- if (ni == 1)
- {
- printf("\n");
- printf("x1 = %d\ny1 = %d\n", dot1.x, dot1.y);
- printf("x2 = %d\ny2 = %d\n", dot2.x, dot2.y);
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment