C++ 判断两个圆是否有交集

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <math.h> 
#include <easyx.h>
#include <graphics.h>
typedef struct T_circle
{
    int x; //x轴坐标
    int y;//Y轴坐标
    int r; //圆的半径
}yuan;
void panduan(yuan test1, yuan test2)
{
    double d;//定义(圆心的距离)
    d = sqrt((test1.x - test2.x)*(test1.x - test2.x) + (test1.y - test2.y)*(test1.y - test2.y));
    if (d > test1.r + test2.r)
    {
        printf("没有交集\n");        
    }
    else if (d <= test1.r + test2.r)
    {
        printf("相切/交\n");
    }
}
void draw(yuan test1,yuan test2)
{
    initgraph(500, 500,SHOWCONSOLE);
    BeginBatchDraw();
    cleardevice();
    setlinecolor(RED);
    circle(test1.x, test1.y, test1.r);
    circle(test2.x, test2.y, test2.r);
    EndBatchDraw();
    system("pause");
    closegraph();
}
//首先要求出两个圆心的距离,公式为:d=√[(x2-x1)²+(y2-y1)²]  这里要用到math.h头文件sqrt
int main()
{
    yuan circle1 = { 100,100,60 };   //定义一个圆 
    yuan circle2 = { 100,150,50 };   //定义一个圆    
    panduan(circle1, circle2);
    draw(circle1, circle2);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shenji/p/12503106.html