Jump to content

Punctul De Intersecţie A Două Segmente Într-Un Sistem De Coordonate


Courage

Recommended Posts

  • Moderators

Să considerăm 4 puncte în planul Ox şi Oy: A(x1,y1), B(x2,y2), C(x3,y3), D(x4,y4). Să se găsească punctul de intersecţie al celor 4 puncte (mai exact: punctul de intersecţie dintre dreptele AB şi CD). În caz contrar, să se afişeze un mesaj corespunzător.

(L-am făcut transoformând un pseudocod dintr-un manual).

#include <iostream>
using namespace std;
int main()
{
   float x1,y1,x2,y2,x3,y3,x4,y4,x,y,m1,n1,m2,n2,min1,max1,min2,max2,min3,max3,min4,max4;
   cout<<"x1= "; cin>>x1;
   cout<<"y1= "; cin>>y1;
   cout<<"x2= "; cin>>x2;
   cout<<"y2= "; cin>>y2;
   cout<<"x3= "; cin>>x3;
   cout<<"y3= "; cin>>y3;
   cout<<"x4= "; cin>>x4;
   cout<<"y4= "; cin>>y4;
   if (x1==x2 or x3==x4)
      cout<<"Cel putin o dreapta este paralela cu Oy";
   else
   {
       m1=(y2-y1)/(x2-x1);
       m2=(y4-y3)/(x4-x3);
       n1=y1-m1*x1;
       n2=y3-m2*x3;
       if (m1==m2)
       {
           if (y1==m2*x1+n2) cout<<"Segmentele sunt pe aceeasi dreapta";
           else cout<<"Segmentele sunt paralele";
       }
       else
       {
           x=(n2-n1)/(m1-m2);
           y=m1*x+n1;
           if (x1<x2){ min1=x1; max1=x2; }
           else{ min1=x2; max1=x1; }
           if (y1<y2){ min2=y1; max2=y2; }
           else{ min2=y2; max2=y1; }
           if (x3<x4){ min3=x3; max3=x4; }
           else{ min3=x3; max3=x3; }
           if (y3<y4){ min4=y3; max4=y4; }
           else{ min4=y4; max4=y3; }
           if (x<min1 or y<min2 or x<min3 or y<min4 or x>max1 or y>max2 or x>max3 or y>max4)  cout<<"Segmentele se intersecteaza pe prelungirile ";
           cout<<"x= "<<x;
           cout<<", y= "<<y;
       }
   }
   return 0;
}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.