csansoon

Consolidation 3.01 P39799 Circles (2)

Dec 24th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5.  
  6. // Precondition:
  7. struct Point {
  8.     double x, y;};
  9. struct Circle {
  10.     Point center;
  11.     double radius;};
  12. double dist(const Point& a, const Point& b) {
  13.     return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));}
  14. void move(Point& p1, const Point& p2) {
  15.     p1.x = p1.x + p2.x;
  16.     p1.y = p1.y + p2.y;}
  17. void move(Circle& c, const Point& p) {
  18.     c.center.x = c.center.x + p.x;
  19.     c.center.y = c.center.y + p.y;}
  20. void scale(Circle& c, double sca) {
  21.     c.radius = c.radius * sca;}
  22. bool is_inside(const Point& p, const Circle& c) {
  23.     return dist(c.center,p)<=c.radius;}
  24.    
  25.  
  26.  
  27.  
  28. void read(Point& p) {
  29.     cin>>p.x>>p.y;}
  30.    
  31. void read(Circle& c) {
  32.     cin>>c.center.x>>c.center.y>>c.radius;}
  33.  
  34. int main() {
  35.     Point p,pnext;
  36.     Circle c;
  37.     read(c);
  38.     read(p);
  39.     if (is_inside(p,c)) cout<<"initially inside"<<endl;
  40.     else cout<<"initially outside"<<endl;
  41.     int t;
  42.     cin>>t;
  43.     for (int i=1; i<=t; ++i) {
  44.         bool was_inside = is_inside(p,c);
  45.         cin>>pnext.x>>pnext.y;
  46.         move(p,pnext);
  47.         if (is_inside(p,c) != was_inside) {
  48.             if (is_inside(p,c)) cout<<"in the step "<<i<<" has gone in"<<endl;
  49.             else cout<<"in the step "<<i<<" has gone out"<<endl;
  50.         }
  51.     }
  52. }
  53.  
  54. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment