Определить находится ли точка внутри прямоугольника - C (СИ)
Формулировка задачи:
нужно определить находится ли точка внутри прямоугольника, прямоугольник может быть не параллелен оси координат и буквы вершин могут быть рандомно расположены ABCD
Решение задачи: «Определить находится ли точка внутри прямоугольника»
textual
Листинг программы
// vodilov.cpp: . // #include "stdafx.h" #include "math.h" #include "testing.h" int _tmain(int argc, _TCHAR* argv[]) { int x_m, y_m, x_a, y_a, x_b, y_b, x_c, y_c, x_d, y_d; double S_p, S_t1, S_t2, S_t3, S_t4, S_t5; input_printf ( "Vvedite coordinati" ); scanf ("%d", "%d", "%d", "%d", "%d", "%d", "%d", "%d", "%d", "%d", &x_m, &y_m, &x_a, &y_a, &x_b, &y_b, &x_c, &y_c, &x_d, &y_d); if ( x_m<-1000 || x_m>1000 || y_m<-1000 || y_m>1000 || x_a<-1000 || x_a>1000 || y_a<-1000 || y_a>1000 || x_b<-1000 || x_b>1000 || y_b<-1000 || y_b>1000 || x_c<-1000 || x_c>1000 || y_c<-1000 || y_c>1000 || x_d<-1000 || x_d>1000 || y_d<-1000 || y_d>1000) { error_printf("Coordinati ne v diapozone"); } else if ((x_a==x_b && x_d==x_c && y_a==y_d && y_b==y_c) && (x_m>=x_a && x_m<=x_b && y_m<=y_a && y_m>=y_d))// ABCD { S_p=(fabs(double((x_a*y_b-y_a*x_b)+(x_b*y_c-y_b*x_c)+(x_c*y_d-y_c*x_d)+(x_d*y_a-y_d*x_a)))); S_t1=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t2=0,5*((x_b-x_m)*(y_c-y_m)-(x_c-x_m)*(y_b-y_m)); S_t3=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_c-y_m)); S_t4=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_a==x_b && x_d==x_c && y_a==y_c && y_b==y_d) && (x_m>=x_a && x_m<=x_b && y_m<=y_a && y_m>=y_c))// ABDC { S_p=(fabs(double((x_a*y_b-y_a*x_b)+(x_b*y_d-y_b*x_d)+(x_d*y_c-y_d*x_c)+(x_c*y_a-y_c*x_a)))); S_t1=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t2=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t3=0,5*((x_d-x_m)*(y_c-y_m)-(x_c-x_m)*(y_d-y_m)); S_t4=0,5*((x_c-x_m)*(y_a-y_m)-(x_a-x_m)*(y_c-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_a==x_c && x_d==x_b && y_a==y_d && y_b==y_c) && (x_m>=x_a && x_m<=x_c && y_m<=y_a && y_m>=y_d))// ACBD { S_p=(fabs(double((x_a*y_b-y_a*x_b)+(x_c*y_b-y_c*x_b)+(x_b*y_d-y_b*x_d)+(x_d*y_a-y_d*x_a)))); S_t1=0,5*((x_a-x_m)*(y_c-y_m)-(x_c-x_m)*(y_a-y_m)); S_t2=0,5*((x_c-x_m)*(y_b-y_m)-(x_b-x_m)*(y_c-y_m)); S_t3=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t4=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_a==x_d && x_b==x_c && y_a==y_b && y_c==y_d) && (x_m>=x_a && x_m<=x_d && y_m<=y_a && y_m>=y_b))// ADCB { S_p=(fabs(double((x_a*y_d-y_a*x_d)+(x_d*y_c-y_d*x_c)+(x_c*y_b-y_c*x_b)+(x_b*y_a-y_b*x_a)))); S_t1=0,5*((x_a-x_m)*(y_d-y_m)-(x_d-x_m)*(y_a-y_m)); S_t2=0,5*((x_d-x_m)*(y_c-y_m)-(x_c-x_m)*(y_d-y_m)); S_t3=0,5*((x_c-x_m)*(y_b-y_m)-(x_b-x_m)*(y_c-y_m)); S_t4=0,5*((x_b-x_m)*(y_a-y_m)-(x_a-x_m)*(y_b-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_a==x_d && x_b==x_c && y_a==y_c && y_b==y_d) && (x_m>=x_a && x_m<=x_d && y_m<=y_a && y_m>=y_c))// ADBC { S_p=(fabs(double((x_a*y_d-y_a*x_d)+(x_d*y_b-y_d*x_b)+(x_b*y_c-y_b*x_c)+(x_c*y_a-y_c*x_a)))); S_t1=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t2=0,5*((x_b-x_m)*(y_c-y_m)-(x_c-x_m)*(y_b-y_m)); S_t3=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_c-y_m)); S_t4=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_a==x_c && x_d==x_b && y_a==y_b && y_c==y_d) && (x_m>=x_a && x_m<=x_b && y_m<=y_a && y_m>=y_b))// ACDB { S_p=(fabs(double((x_a*y_c-y_a*x_c)+(x_c*y_d-y_c*x_d)+(x_d*y_b-y_d*x_b)+(x_b*y_a-y_b*x_a)))); S_t1=0,5*((x_a-x_m)*(y_c-y_m)-(x_c-x_m)*(y_a-y_m)); S_t2=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_c-y_m)); S_t3=0,5*((x_d-x_m)*(y_b-y_m)-(x_b-x_m)*(y_b-y_m)); S_t4=0,5*((x_b-x_m)*(y_a-y_m)-(x_a-x_m)*(y_b-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_b==x_a && x_d==x_c && y_b==y_d && y_a==y_c) && (x_m>=x_b && x_m<=x_a && y_m<=y_b && y_m>=y_d))// BACD { S_p=(fabs(double((x_b*y_a-y_b*x_a)+(x_a*y_c-y_a*x_c)+(x_c*y_d-y_c*x_d)+(x_d*y_b-y_d*x_b)))); S_t1=0,5*((x_b-x_m)*(y_a-y_m)-(x_a-x_m)*(y_b-y_m)); S_t2=0,5*((x_a-x_m)*(y_c-y_m)-(x_c-x_m)*(y_a-y_m)); S_t3=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_c-y_m)); S_t4=0,5*((x_d-x_m)*(y_b-y_m)-(x_b-x_m)*(y_d-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_b==x_a && x_d==x_c && y_b==y_c && y_d==y_a) && (x_m>=x_b && x_m<=x_a && y_m<=y_b && y_m>=y_c))// BADC { S_p=(fabs(double((x_b*y_a-y_b*x_a)+(x_a*y_d-y_a*x_d)+(x_d*y_c-y_d*x_c)+(x_c*y_b-y_c*x_b)))); S_t1=0,5*((x_b-x_m)*(y_a-y_m)-(x_a-x_m)*(y_b-y_m)); S_t2=0,5*((x_a-x_m)*(y_d-y_m)-(x_d-x_m)*(y_a-y_m)); S_t3=0,5*((x_d-x_m)*(y_c-y_m)-(x_c-x_m)*(y_c-y_m)); S_t4=0,5*((x_c-x_m)*(y_b-y_m)-(x_b-x_m)*(y_c-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_b==x_d && x_a==x_c && y_b==y_a && y_d==y_c) && (x_m>=x_b && x_m<=x_d && y_m<=y_b && y_m>=y_a))// BDCA { S_p=(fabs(double((x_b*y_d-y_b*x_d)+(x_d*y_c-y_d*x_c)+(x_c*y_a-y_c*x_a)+(x_a*y_b-y_a*x_b)))); S_t1=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t2=0,5*((x_d-x_m)*(y_c-y_m)-(x_c-x_m)*(y_d-y_m)); S_t3=0,5*((x_c-x_m)*(y_a-y_m)-(x_a-x_m)*(y_c-y_m)); S_t4=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_b==x_d && x_a==x_c && y_b==y_c && y_a==y_d) && (x_m>=x_b && x_m<=x_d && y_m<=y_b && y_m>=y_c))// BDAC { S_p=(fabs(double((x_b*y_d-y_b*x_d)+(x_d*y_a-y_d*x_a)+(x_a*y_c-y_a*x_c)+(x_c*y_b-y_c*x_b)))); S_t1=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t2=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t3=0,5*((x_a-x_m)*(y_c-y_m)-(x_c-x_m)*(y_a-y_m)); S_t4=0,5*((x_c-x_m)*(y_b-y_m)-(x_b-x_m)*(y_c-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_b==x_c && x_d==x_a && y_b==y_d && y_a==y_c) && (x_m>=x_b && x_m<=x_c && y_m<=y_b && y_m>=y_d))// BCAD { S_p=(fabs(double((x_b*y_c-y_b*x_c)+(x_c*y_a-y_c*x_a)+(x_a*y_d-y_a*x_d)+(x_d*y_b-y_d*x_b)))); S_t1=0,5*((x_b-x_m)*(y_c-y_m)-(x_c-x_m)*(y_b-y_m)); S_t2=0,5*((x_c-x_m)*(y_a-y_m)-(x_a-x_m)*(y_c-y_m)); S_t3=0,5*((x_a-x_m)*(y_d-y_m)-(x_d-x_m)*(y_a-y_m)); S_t4=0,5*((x_d-x_m)*(y_b-y_m)-(x_b-x_m)*(y_d-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_b==x_c && x_a==x_d && y_b==y_a && y_d==y_c) && (x_m>=x_b && x_m<=x_c && y_m<=y_b && y_m>=y_a))// BCDA { S_p=(fabs(double((x_b*y_c-y_b*x_c)+(x_c*y_d-y_c*x_d)+(x_d*y_a-y_d*x_a)+(x_a*y_b-y_a*x_b)))); S_t1=0,5*((x_b-x_m)*(y_c-y_m)-(x_c-x_m)*(y_b-y_m)); S_t2=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_d-y_m)); S_t3=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t4=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_c==x_a && x_d==x_b && y_c==y_d && y_a==y_b) && (x_m>=x_c && x_m<=x_a && y_m<=y_c && y_m>=y_d))// CABD { S_p=(fabs(double((x_c*y_a-y_c*x_a)+(x_a*y_b-y_a*x_b)+(x_b*y_d-y_b*x_d)+(x_d*y_c-y_d*x_c)))); S_t1=0,5*((x_c-x_m)*(y_a-y_m)-(x_a-x_m)*(y_c-y_m)); S_t2=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t3=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t4=0,5*((x_d-x_m)*(y_c-y_m)-(x_c-x_m)*(y_d-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_c==x_a && x_d==x_b && y_c==y_b && y_a==y_d) && (x_m>=x_c && x_m<=x_a && y_m<=y_c && y_m>=y_b))// CADB { S_p=(fabs(double((x_c*y_a-y_c*x_a)+(x_a*y_d-y_a*x_d)+(x_d*y_b-y_d*x_b)+(x_b*y_c-y_b*x_c)))); S_t1=0,5*((x_c-x_m)*(y_a-y_m)-(x_a-x_m)*(y_c-y_m)); S_t2=0,5*((x_a-x_m)*(y_d-y_m)-(x_d-x_m)*(y_a-y_m)); S_t3=0,5*((x_d-x_m)*(y_b-y_m)-(x_b-x_m)*(y_d-y_m)); S_t4=0,5*((x_b-x_m)*(y_c-y_m)-(x_c-x_m)*(y_b-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_c==x_d && x_a==x_b && y_c==y_b && y_a==y_d) && (x_m>=x_c && x_m<=x_d && y_m<=y_c && y_m>=y_b))// CDAB { S_p=(fabs(double((x_c*y_d-y_c*x_d)+(x_d*y_a-y_d*x_a)+(x_a*y_b-y_a*x_b)+(x_b*y_c-y_b*x_c)))); S_t1=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_c-y_m)); S_t2=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t3=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t4=0,5*((x_b-x_m)*(y_c-y_m)-(x_c-x_m)*(y_b-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_c==x_d && x_a==x_b && y_c==y_a && y_d==y_b) && (x_m>=x_c && x_m<=x_d && y_m<=y_c && y_m>=y_a))// CDBA { S_p=(fabs(double((x_c*y_d-y_c*x_d)+(x_d*y_b-y_d*x_b)+(x_b*y_a-y_b*x_a)+(x_a*y_c-y_a*x_c)))); S_t1=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_c-y_m)); S_t2=0,5*((x_d-x_m)*(y_b-y_m)-(x_b-x_m)*(y_d-y_m)); S_t3=0,5*((x_b-x_m)*(y_a-y_m)-(x_a-x_m)*(y_b-y_m)); S_t4=0,5*((x_a-x_m)*(y_c-y_m)-(x_c-x_m)*(y_a-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_c==x_d && x_a==x_b && y_c==y_a && y_d==y_b) && (x_m>=x_c && x_m<=x_d && y_m<=y_c && y_m>=y_a))// CBDA { S_p=(fabs(double((x_c*y_b-y_c*x_b)+(x_b*y_d-y_b*x_d)+(x_d*y_a-y_d*x_a)+(x_a*y_c-y_a*x_c)))); S_t1=0,5*((x_c-x_m)*(y_b-y_m)-(x_b-x_m)*(y_c-y_m)); S_t2=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t3=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t4=0,5*((x_a-x_m)*(y_c-y_m)-(x_c-x_m)*(y_a-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_c==x_b && x_a==x_d && y_c==y_d && y_b==y_a) && (x_m>=x_c && x_m<=x_b && y_m<=y_c && y_m>=y_d))// CBAD { S_p=(fabs(double((x_c*y_b-y_c*x_b)+(x_b*y_a-y_b*x_a)+(x_a*y_d-y_a*x_d)+(x_d*y_c-y_d*x_c)))); S_t1=0,5*((x_c-x_m)*(y_b-y_m)-(x_b-x_m)*(y_c-y_m)); S_t2=0,5*((x_b-x_m)*(y_a-y_m)-(x_a-x_m)*(y_b-y_m)); S_t3=0,5*((x_a-x_m)*(y_d-y_m)-(x_d-x_m)*(y_a-y_m)); S_t4=0,5*((x_d-x_m)*(y_c-y_m)-(x_c-x_m)*(y_d-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_d==x_b && x_c==x_a && y_d==y_c && y_b==y_a) && (x_m>=x_d && x_m<=x_b && y_m<=y_d && y_m>=y_c))// DBAC { S_p=(fabs(double((x_d*y_b-y_d*x_b)+(x_b*y_a-y_b*x_a)+(x_a*y_c-y_a*x_c)+(x_c*y_d-y_c*x_d)))); S_t1=0,5*((x_d-x_m)*(y_b-y_m)-(x_b-x_m)*(y_d-y_m)); S_t2=0,5*((x_b-x_m)*(y_a-y_m)-(x_a-x_m)*(y_b-y_m)); S_t3=0,5*((x_a-x_m)*(y_c-y_m)-(x_c-x_m)*(y_a-y_m)); S_t4=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_c-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_d==x_b && x_c==x_a && y_d==y_a && y_b==y_c) && (x_m>=x_d && x_m<=x_b && y_m<=y_d && y_m>=y_a))// DBCA { S_p=(fabs(double((x_d*y_b-y_d*x_b)+(x_b*y_c-y_b*x_c)+(x_c*y_a-y_c*x_a)+(x_a*y_d-y_a*x_d)))); S_t1=0,5*((x_d-x_m)*(y_b-y_m)-(x_b-x_m)*(y_d-y_m)); S_t2=0,5*((x_b-x_m)*(y_c-y_m)-(x_c-x_m)*(y_b-y_m)); S_t3=0,5*((x_c-x_m)*(y_a-y_m)-(x_a-x_m)*(y_c-y_m)); S_t4=0,5*((x_a-x_m)*(y_d-y_m)-(x_d-x_m)*(y_a-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_d==x_a && x_c==x_b && y_d==y_b && y_a==y_c) && (x_m>=x_d && x_m<=x_a && y_m<=y_d && y_m>=y_b))// DACB { S_p=(fabs(double((x_d*y_a-y_d*x_a)+(x_a*y_c-y_a*x_c)+(x_c*y_d-y_c*x_d)+(x_b*y_d-y_b*x_d)))); S_t1=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t2=0,5*((x_a-x_m)*(y_c-y_m)-(x_c-x_m)*(y_a-y_m)); S_t3=0,5*((x_c-x_m)*(y_b-y_m)-(x_b-x_m)*(y_c-y_m)); S_t4=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_d==x_a && x_c==x_b && y_d==y_c && y_b==y_a) && (x_m>=x_d && x_m<=x_a && y_m<=y_d && y_m>=y_c))// DABC { S_p=(fabs(double((x_d*y_a-y_d*x_a)+(x_a*y_b-y_a*x_b)+(x_b*y_c-y_b*x_c)+(x_c*y_d-y_c*x_d)))); S_t1=0,5*((x_d-x_m)*(y_a-y_m)-(x_a-x_m)*(y_d-y_m)); S_t2=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t3=0,5*((x_b-x_m)*(y_c-y_m)-(x_c-x_m)*(y_b-y_m)); S_t4=0,5*((x_c-x_m)*(y_d-y_m)-(x_d-x_m)*(y_c-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_d==x_c && x_b==x_a && y_d==y_b && y_c==y_a) && (x_m>=x_d && x_m<=x_c && y_m<=y_d && y_m>=y_b))// DCAB { S_p=(fabs(double((x_d*y_c-y_d*x_c)+(x_c*y_a-y_c*x_a)+(x_a*y_b-y_a*x_b)+(x_b*y_d-y_b*x_d)))); S_t1=0,5*((x_d-x_m)*(y_c-y_m)-(x_c-x_m)*(y_d-y_m)); S_t2=0,5*((x_c-x_m)*(y_a-y_m)-(x_a-x_m)*(y_c-y_m)); S_t3=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t4=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if ((x_d==x_c && x_a==x_b && y_d==y_a && y_c==y_b) && (x_m>=x_d && x_m<=x_c && y_m<=y_d && y_m>=y_a))// DCAB { S_p=(fabs(double((x_d*y_c-y_d*x_c)+(x_c*y_a-y_c*x_a)+(x_a*y_b-y_a*x_b)+(x_b*y_d-y_b*x_d)))); S_t1=0,5*((x_d-x_m)*(y_c-y_m)-(x_c-x_m)*(y_d-y_m)); S_t2=0,5*((x_c-x_m)*(y_a-y_m)-(x_a-x_m)*(y_c-y_m)); S_t3=0,5*((x_a-x_m)*(y_b-y_m)-(x_b-x_m)*(y_a-y_m)); S_t4=0,5*((x_b-x_m)*(y_d-y_m)-(x_d-x_m)*(y_b-y_m)); S_t5=S_t1+S_t2+S_t3+S_t4; } if (S_t5==S_p) { printf("Tochka vnutri chetirehugolnika"); } else { printf("Tochka snaryzhi chetirehugolnika"); } WAIT_ANY_KEY return 0; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д