C++ 实现点与圆的位置关系

C++ 实现点与圆的位置关系

//maincircle

#include
#include"circle.h"
#include"point.h"
using namespace std;
int main()
{
    circle c1;
    point p1;
    c1.getcircle( 0, 0, 4 );
    p1.getpoint( 2, 2 ) ;
    cout<return 0;
}
//point.h
#pragma once
class point
{
public:
    void getpoint(double x,double y);
    double getx();
    double gety();
public:
    double x1;
    double y1;
};

//point.cpp
#include "point.h"
void point::getpoint(double x,double y)
{
    x1 = x;
    y1 = y;
}

double point::getx()
{
    return x1;
}

double point::gety()
{
    return y1;
}
//circle.h
#pragma once
#include"point.h"
class circle
{
public:
    void getcircle(double x,double y, double r);
    char* judge1( double x,double y);
    /*char* judge2( point &p);*/
private :
    double x0;
    double y0;
    double r1;
};
//circle.cpp
#include "circle.h"
void circle:: getcircle(double x,double y, double r)
{
    x0=x;
    y0=y;
    r1=r;
}
char* circle::judge1( double x,double y)
{
    double s =(x0 - x)*(x0 - x) + (y0 - y)*(y0 - y) - r1*r1;
    if( s > 0 )
    {
        return "outside the circle"
    }
    else
    {
        if( s == 0)
        {
            return " on the circle"
        }
        else return " in the circle"
    }
}

char* circle::judge2( point & p)
{
    double s =(x0 - p.getx())*(x0 - p.getx()) + (y0 - p.gety())*(y0 - p.gety()) - r1*r1;
    if( s > 0 )
    {
        return "outside the circle ";
    }
    else
    {
        if( s == 0)
        {
            return " on the circle";
        }
        else return " in the circle ";
    }
}

圆心坐标为(0,0),半径为4
(1)点的坐标为(2,2)
C++ 实现点与圆的位置关系_第1张图片
*(2)点的坐标为(0,4)
C++ 实现点与圆的位置关系_第2张图片
(3)点的坐标为(8,4)
C++ 实现点与圆的位置关系_第3张图片
judge1()和judge2()同时放开后
C++ 实现点与圆的位置关系_第4张图片

你可能感兴趣的:(C++语言)