首页 > 编程知识 正文

用c语言判定两个圆是否相交,C语言判断点和圆的关系

时间:2024-03-25 09:50:00 阅读:332645 作者:GIHO

本文目录一览:

c语言作业不会啊

没怎么按要求,但是差不多完成了前2个任务

/* Note:Your choice is C IDE */

#include "stdio.h"

#include "math.h"

typedef struct

{

int x;

int y;

}

point;

typedef struct

{

float a;

float b;

float c;

}

line;

typedef struct

{

point x;

point y;

point z;

point t;

}

rect;

typedef struct

{

point x;

int radius;

}

cycle;

double testpoint(void)

{

point x1,y1;

double l,temp;

printf("请输入第一个点的 x坐标n");

scanf("%d",x1.x);

printf("请输入第一个点的 y坐标n");

scanf("%d",x1.y);

printf("请输入第二个点的 x坐标n");

scanf("%d",y1.x);

printf("请输入第二个点的 y坐标n");

scanf("%d",y1.y);

temp=(double)(((x1.x)-(y1.x))*((x1.x)-(y1.x))+((x1.y)-(y1.y))*((x1.y)-(y1.y)));

l=sqrt(temp);

return l;

}

double testline(void)

{

point x1;

line g;

double temp1,temp2,d;

printf("请输入要求点的 x坐标n");

scanf("%d",x1.x);

printf("请输入要求点的 y坐标n");

scanf("%d",x1.y);

printf("请输入直线一般表达式aX+bY+c=0的三个系数n");

printf("请输入an");

scanf("%f",g.a);

printf("请输入bn");

scanf("%f",g.b);

printf("请输入cn");

scanf("%f",g.c);

temp1=sqrt((g.a)*(g.a)+(g.b)*(g.b));

temp2=g.a*(double)(x1.x)+g.b*(double)(x1.y)+g.c;

if(temp2=0)

temp2=temp2;

else

temp2=-temp2;

d=temp2/temp1;

return d;

}

void main()

{

double l,d;

l=testpoint();

printf("2点之间的距离是%9.9fn",l);

d=testline();

printf("点到线的距离是%fn",d);

}

用c语言判定两个圆是否相交,

判断圆心距离和半径的和 差之间的关系就行了;

bool xiangjiao(int x1,int y1,int r1,int x2,int y2,int r2)

{

doule s;

s = sqrt(double((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1)));

if(int(s)r2+r1int(s)abs(r2-r1))

return true;

else

return false;

}

这是一个方法,写代码时候调用该方法就可以了。

c语言程序求二维坐标中两圆的面积及是否相交

经典题。网上答案很多,给你抄一个:

假设半径小的圆为c1,半径大的圆为c2。

c1的半径r1,圆心坐标(x1,y1)。c2的半径r2,圆心坐标(x2,y2)。

d为两圆圆心连线的长度。

相交面积为S

d=sqrt((x1-x2)^2+(y1-y2)^2)

(1)如果r1+r2=d

那么两圆相离,相交面积S=0

(2)如果r2-r1=d

那么半径小的圆内含半径大的圆,那么相交面积为小圆的面积S=pi*r1*r1

(3)既非(1)也非(2)

在图上画两个相交圆,结合图像看。

那么两圆相交,连接小圆的圆心与两个圆的交点,连接大圆的圆心和两个圆的交点。

可以发现形成的图形被两个圆心的连线平分成2个全等三角形。

由小圆圆心和交点所连两条线(长度为半径)以及在大圆之内的弧所形成的扇形为S1

由大圆圆心和交点所连两条线(长度为半径)以及在小圆之内的弧所形成的扇形为S2

由小圆圆心和交点所连两条线以及由大圆圆心和交点所连两条线所形成的四边形的面积为S3

可见相交面积S=S1+S2-S3

要求出扇形的面积,要知道扇形的圆心角。

小圆包含的扇形的圆心角为2*a1(考虑一个三角形)

a1=acos((r1^2+d^2-r2^2)/(2.0*r1*d)) 余弦定理

a2=acos((r2^2+d^2-r1^2)/(2.0*r2*d))

S1=pi*r1*r1*2*a1/(2*pi)=a1*r1*r1

同理

S2=a2*r2*r2

S3为一个三角形面积的2倍

S3=2*r1*d*sin(a1)/2=r1*d*sin(a1)

则S=a1*r1*r1+a2*r2*r2-r1*d*sin(a1)

代码:

#define pi acos(-1.0)

#define maxn 10

struct node{

double x;

double y;

double r;

} c[maxn];

double area(int i,double r1,int j,double r2){

double d=

sqrt((c[i].x-c[j].x)*(c[i].x-c[j].x)+(c[i].y- c[j].y)*(c[i].y-c[j].y));//圆心距

if(r1r2){

double temp=r1;

r1=r2;

r2=temp;

}//r1取小

if(r1+r2=d)

return 0;//相离

else if(r2-r1=d)

return pi*r1*r1;//内含

else {

double a1=acos((r1*r1+d*d-r2*r2)/(2.0*r1*d));

double a2=acos((r2*r2+d*d-r1*r1)/(2.0*r2*d));

return (a1*r1*r1+a2*r2*r2-r1*d*sin(a1));

}//相交

}

=======

如果只有2个圆,前面可用:

#define maxn 2

main()

{

输入或赋值 c[0].x, c[0].y,c[0].r, c[1].x, c[1].y,c[1].r,

调用 参数:i=0;r1=c[0].r; j=1;r2=c[1].r;

调用:

printf("Area=%lf", area(0,c[0].r,1,c[1].r) );

}

判断两圆是否相交 用c 语言

#include stdio.h

#include math.h

int main(void)

{

float x1, y1, x2, y2, r1, r2;

scanf("%f %f %f", x1, y1, r1);

scanf("%f %f %f", x2, y2, r2);

float len = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

if (len (r1+r2))

{

printf("yesn");

}

else

printf("non");

return 0;

}

C语言编程求解

homework16.h里面的内容:

struct Pointxy /*点*/

{

int x;

int y;

}

typedef struct Pointxy Point;

struct Cirlexy /*圆*/

{

Point center;

int r;

}

typedef struct Cirlexy Circle;

struct Rectanglexy /*长方形结构*/

{

Point leftp;

int a;

int b;

}

typedef struct Rectanglexy Rect;

struct LineAb /* y=k*x+b */

{

float k;

float b;

}

typedef stuct LineAb line;

homework16.c里面的内容:还需要完善,先占位,再完善了发过来

#include"homework16.h"

#include"math.h"

float PointToPoint(Point a,Point b) /*点到点的距离*/

{

return(sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

}

float PointToLine(Point a,Line l) /*点到直线的距离*/

{

}

int PointInCircle(Point a,Circle c) /*点是否在圆内*/

{

float dis=0;

dis=PointToPoint(Point a,c.center);

if(disc.r)

return 0;

else

return 1;

}

int PointInRect(Point a,Rect r) /*点是否在Rect内*/

{

}

int CircleInCircle(Circle a,Circle b) /*圆是否相交*/

{

float dis;

dis=PointToPoint(a.center,b.center);

if(disa.r+b.r)

return 0;

else

return 1;

}

用C语言编写“求两个圆之间的距离,相离,相切或相交”

#include stdio.h

#include math.h

int main()

{ double x1,x2,y1,y2,r1,r2,d;

  printf("请输入圆1的圆心坐标和半径:");

  scanf("%lf%lf%lf",x1,y1,r1);

  printf("请输入圆2的圆心坐标和半径:");

  scanf("%lf%lf%lf",x2,y2,r2);

  d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

  if(dr1+r2)printf("两圆相离n");

  else if(dr1+r2)printf("两圆相交n");

  else printf("两圆相切n");

  return 0;

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。