Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- struct Point
- {
- public:
- double x, y;
- };
- class otrezok {
- private:
- double aX,aY,bX,bY;
- public:
- otrezok(double ax, double ay, double bx, double by)
- {
- this->aX = ax;
- this->aY = ay;
- this->bX = bx;
- this->bY = by;
- }
- void ToConsole()
- {
- cout<<"Otrerzok ("<<aX<<","<<aY<<") - ("<<bX<<","<<bY<<")"<<endl;
- }
- bool Equals (otrezok Drugoi_otrezok)
- {
- return this->Lenght()==Drugoi_otrezok.Lenght();
- }
- int Lenght()
- {
- return sqrt( (bX - aX)*(bX - aX) + (bY - aY) * (bY - aY) );
- }
- int CompareTo(otrezok Drugoi_otrezok)
- {
- if(this->Equals(Drugoi_otrezok))
- return 0;
- else
- return this->Lenght() > Drugoi_otrezok.Lenght();
- }
- Point Intersect(otrezok O)
- {
- Point toReturn;
- toReturn.x = 0;
- toReturn.y = 0;
- double t = 0;
- t = - ( (O.aX - this->aX) * (this->aY - this->bY) + (O.aY - this->aY ) * (this->bX - this->aX) )/
- ( (O.bX - O.aX ) * (this->aY - this->bY) + ( O.bY - O.aY ) * (this->bX - this->aX) );
- if(t >= 0 && t <= 1)
- {
- toReturn.x = O.aX + t * (O.bX - O.aX);
- toReturn.y = O.aY + t * (O.bY - O.aY);
- return toReturn;
- }
- else
- {
- cout<<"NET TOCHKI PERESECHENIYA"<<endl;
- return toReturn;
- }
- }
- double Distance (otrezok Drugoi_otrezok)
- {
- double first = Distance_between_2_points(this->aX, this->aY, Drugoi_otrezok.aX, Drugoi_otrezok.aY);
- double second = Distance_between_2_points(this->aX, this->aY, Drugoi_otrezok.bX, Drugoi_otrezok.bY);
- double third = Distance_between_2_points(this->bX, this->bY, Drugoi_otrezok.aX, Drugoi_otrezok.aY);
- double fourth = Distance_between_2_points(this->bX, this->bY, Drugoi_otrezok.bX, Drugoi_otrezok.bY);
- double tmp1, tmp2;
- /// вспомогательные переменные для того, чтобы сохранить читаемость кода (не делать всё во вложенных if)
- /// Это - все пары расстояний между отрезками, минимальное из них - наш ответ
- if(first < second)
- {
- tmp1 = first;
- }
- else
- {
- tmp1 = second;
- }
- if(third < fourth)
- {
- tmp2 = third;
- }
- else
- {
- tmp2 = fourth;
- }
- if(tmp1 < tmp2)
- {
- return tmp1;
- }
- else
- {
- return tmp2;
- }
- }
- double Distance_between_2_points(double x1, double y1, double x2, double y2)
- {
- return sqrt( (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) );
- }
- };
- int main()
- {
- otrezok a = otrezok(2,-1,2,1);
- otrezok b = otrezok(0,0,3,0);
- a.Intersect(b);
- //cout << "Hello world!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement