#include <iostream>
using namespace std;
double Mixed(double ax, double ay, double az, double bx, double by, double bz, double cx, double cy, double cz){
return (ax*by*cz)+(ay*bz*cx)+(az*bx*cy)-(az*by*cx)-(ay*bx*cz)-(ax*bz*cy); // Формула смешаного произведения, если = 0, то одна плоскость
}
bool Collinear(double ax, double bx, double ay, double by, double az, double bz){
return ((ax/bx) == (ay/by) && (ay/by) == (az/bz)) ? true:false; // Коллинеарность
}
bool VectorEquation(double x, double x0, double y, double y0, double z, double z0, double Vx, double Vy, double Vz){
return (x - x0)/Vx == (y - y0)/Vy && (y - y0)/Vy == (z - z0)/Vz ? true:false; // уравнение прямой в пространстве
}
double Min(double X1, double X2){
return X1 < X2 ? X1:X2;
}
double Max(double X1, double X2){
return X1 > X2 ? X1:X2;
}
bool CollinearforPerp(double ax, double ay, double az, double bx, double by, double bz){ // Условие коллинеарности при bx,by,bz=0
return ((ay*bz-az*by == 0) && (az*bx-ax*by == 0) && (ax*by-ay*bx == 0)) ? true:false;
}
// 0/0 ?
int main() {
double Xa, Ya, Za, Xb, Yb, Zb, Xc, Yc, Zc, Xd, Yd, Zd; // Координаты концов отрезков
cin >> Xa >> Ya >> Za >> Xb >> Yb >> Zb >> Xc >> Yc >> Zc >> Xd >> Yd >> Zd;
double VectorABx, VectorABy, VectorABz, VectorCDx, VectorCDy, VectorCDz, VectorACx, VectorACy, VectorACz; //Координаты векторов
VectorABx = Xb-Xa;
VectorABy = Yb-Ya;
VectorABz = Zb-Za;
VectorCDx = Xd-Xc;
VectorCDy = Yd-Yc;
VectorCDz = Zd-Zc;
VectorACx = Xc-Xa;
VectorACy = Yc-Ya;
VectorACz = Zc-Za;
if (Mixed(VectorABx, VectorABy, VectorABz, VectorCDx, VectorCDy, VectorCDz, VectorACx, VectorACy, VectorACz) == 0){ // Смешанное произведение
if(Xc == Xd || Yc == Yd || Zc == Zd){ // Частный случай, если прямая перпендикулярна какой-либо оси
if(CollinearforPerp(VectorABx,VectorABy,VectorABz,VectorCDx,VectorCDy,VectorCDz)){ // Прямые Коллинеарны
if((Xc == Xd && Xa != Xc) || (Yc == Yd && Ya != Yc) || (Zc == Zd && Za != Zd)){ // Условие параллельности
cout << "Прямые параллельны" << endl;
}
else{
if(((Min(Xa,Xb) <= Xc && Max(Xa,Xb) >= Xc) ||(Min(Xa,Xb) <= Xd && Max(Xa,Xb) >= Xd))||(((Min(Xc,Xd) <= Xa && Max(Xc,Xd) >= Xa) ||(Min(Xc,Xd) <= Xb && Max(Xc,Xd) >= Xb)))){
if(((Min(Ya,Yb) <= Yc && Max(Ya,Yb) >= Yc) ||(Min(Ya,Yb) <= Yd && Max(Ya,Yb) >= Yd))||(((Min(Yc,Yd) <= Ya && Max(Yc,Yd) >= Ya) ||(Min(Yc,Yd) <= Yb && Max(Yc,Yd) >= Yb)))){
if(((Min(Za,Zb) <= Zc && Max(Za,Zb) >= Zc) ||(Min(Za,Zb) <= Yd && Max(Za,Zb) >= Zd))||(((Min(Zc,Zd) <= Za && Max(Zc,Zd) >= Ya) ||(Min(Zc,Zd) <= Zb && Max(Zc,Zd) >= Zb)))){
cout << "Отрезки совпадают" << endl; // Пресечение отрезков
}
else cout << "Отрезки находятся на одной прямой но не совпадают" << endl;
}
else cout << "Отрезки находятся на одной прямой но не совпадают" << endl;
}
else cout << "Отрезки находятся на одной прямой но не совпадают" << endl;
}
}
else{
if(((Min(Xa,Xb) <= Xc && Max(Xa,Xb) >= Xc) ||(Min(Xa,Xb) <= Xd && Max(Xa,Xb) >= Xd))||(((Min(Xc,Xd) <= Xa && Max(Xc,Xd) >= Xa) ||(Min(Xc,Xd) <= Xb && Max(Xc,Xd) >= Xb)))){
if(((Min(Ya,Yb) <= Yc && Max(Ya,Yb) >= Yc) ||(Min(Ya,Yb) <= Yd && Max(Ya,Yb) >= Yd))||(((Min(Yc,Yd) <= Ya && Max(Yc,Yd) >= Ya) ||(Min(Yc,Yd) <= Yb && Max(Yc,Yd) >= Yb)))){
if(((Min(Za,Zb) <= Zc && Max(Za,Zb) >= Zc) ||(Min(Za,Zb) <= Yd && Max(Za,Zb) >= Zd))||(((Min(Zc,Zd) <= Za && Max(Zc,Zd) >= Ya) ||(Min(Zc,Zd) <= Zb && Max(Zc,Zd) >= Zb)))){
cout << "Отрезки пересекаются" << endl;
}
else cout << "Отрезки не пересекаются" << endl;
}
else cout << "Отрезки не пересекаются" << endl;
}
else cout << "Отрезки не пересекаются" << endl;
}
return 0;
}
else{
if (Collinear(VectorABx, VectorCDx, VectorABy, VectorCDy, VectorABz, VectorCDz)){ // Прямые коллинеарны
if(VectorEquation(Xa, Xc, Ya, Yc, Za, Zc, VectorCDx, VectorCDy, VectorCDz)){ // Совпадение прямых
if((Min(Xa,Xb) <= Xc && Max(Xa,Xb) >= Xc) ||(Min(Xa,Xb) <= Xd && Max(Xa,Xb) >= Xd)){ // Совпадение отрезков
cout<< "Отрезки совпадают" << endl;
}
else{
cout << "Отрезки на одной прямой но не совпадают" << endl;
}
}
else{
cout << "Прямые параллельны" << endl;
}
}
else{
if(((Min(Xa,Xb) <= Xc && Max(Xa,Xb) >= Xc) || (Min(Xa,Xb) <= Xd && Max(Xa,Xb) >= Xd))||(((Min(Xc,Xd) <= Xa && Max(Xc,Xd) >= Xa) ||(Min(Xc,Xd) <= Xb && Max(Xc,Xd) >= Xb)))){
cout << "Отрезки пересекаются" << endl;
}
else cout << "Отрезки не пересекаются" << endl;
}
}
}
else{
cout << "Прямые в разных плоскостях" << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCmRvdWJsZSBNaXhlZChkb3VibGUgYXgsIGRvdWJsZSBheSwgZG91YmxlIGF6LCBkb3VibGUgYngsIGRvdWJsZSBieSwgZG91YmxlIGJ6LCBkb3VibGUgY3gsIGRvdWJsZSBjeSwgZG91YmxlIGN6KXsKCXJldHVybiAoYXgqYnkqY3opKyhheSpieipjeCkrKGF6KmJ4KmN5KS0oYXoqYnkqY3gpLShheSpieCpjeikt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