And determining whether the ray intersects the circle

Given the coordinates of two points A and B, there is a ray Representative AB.
To give a set point and a radius coordinate point O R, representative R is O as a center to a radius of the circle.
Ray asked whether there is a common point of the circle that exist and at some point both in the circle ray.
Total number of input 7, are non-negative integers not more than 100.
Output line, there is this point on output Y, otherwise N. output
Test Example:
Test Input: 232,100,211
Expected Output: N
Test Inputs: 101100211
expected output: Y
vector multiplication:
A * B = |a| |b| * * * B = cosxa | a | * | B | cosx *
a * B = coordinate |a| * |b| * cosx vector multiplication:
(X1, Y1) * (X2, Y2) = X1 * Y1 + X2 * Y2 (X1, y1) * (x2, y2) = x1 * x2 + y1 * y2 (x1, y1) * (x2, y2) = x1 * x2 + y1 * y2 Tip:
Here Insert Picture Description
Comparative ∠OAB ∠OAP and size, if not ∠OAB greater than ∠OAP, the ray intersects the circle.
You can determine the size by comparing the value of sin.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
 int x1,y1,x2,y2,xo,yo;
 double r,l,k=0;
 cin>>x1>>y1>>x2>>y2>>xo>>yo>>r;
 k=(y2-y1)/(x2-x1);
 l=abs(k*xo-yo+y1-k*x1)/(sqrt(k*k+1));
 if(l==r)
 {
  cout<<'Y';
 }
 else
 {
  cout<<'N';
 }
}

or

#include <iostream>
#include <cmath>
using namespace std;
float len(int x1,int y1,int x2,int y2)
{
      int t = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
      return sqrt(t);
}
int main()
{
    int xa,ya,xb,yb,xo,yo,R;
    cin>>xa>>ya>>xb>>yb>>xo>>yo>>R;
    if(xa==xb && ya==yb) 
 {
  cout<<"N";return 0;
 }
    float OA = len(xa,ya,xo,yo);
    float OB = len(xb,yb,xo,yo);
    if(OA<=(float)R || OB<=(float)R) 
 {
  cout<<"Y";return 0;
 }
    else
 {
     int son_cos = (xo-xa)*(xb-xa)+(yo-ya)*(yb-ya);
     float mother_cos = len(xa,ya,xo,yo)*len(xa,ya,xb,yb);
     float cos = (float)son_cos/mother_cos;
     float my_sin = sqrt(1-cos*cos);
     float sin = (float)R/len(xa,ya,xo,yo);
     if(cos<=0)
  {
   cout<<"N";
   return 0;
  }
     else if(my_sin<=sin) 
  { 
   cout<<"Y";
   return 0;
  }
      else 
  {
   cout<<"N";
   return 0;
  }
}  
}
Published 102 original articles · won praise 93 · views 4981

Guess you like

Origin blog.csdn.net/huangziguang/article/details/104575752