当前位置:首页 » 编程语言 » 两条直线的交点c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

两条直线的交点c语言

发布时间: 2022-05-07 10:08:47

c语言求两直线的交点

1、首先在打开的C语言软件中,先用for循环输入两个集合放到a、b两个数组中,如下图所示。

❷ c编程求出两条直线的交点

//以下为我的编码结果,此结果没有考虑竖直方向直线,以及两条直线完全平行的问题
//因为如果加入这两个条件,程序的复杂程度会上升。
//问题2与问题1内容完全一致,将问题1做两次就是问题2的结果,所以不做解答
#include"stdio.h"

intmain()
{
//四个点的坐标存在这里面,我随便编几个数据哈。反正不影响算法
doublebuffer[4][2]={{3,0},{0,1},{1,0},{2,1}};

//假设直线AB的公式为y=ax+b
//a=(y1-y2)/(x1-x2)
//b=y1-(y1-y2)/(x1-x2)*x1
//如果x1-x2==0,说明这是一条平行于x轴的直线,a=0,b=y1;
doublea,b;
if((buffer[0][0]-buffer[1][0])==0)
{
a=0;
b=buffer[0][1];
}
else
{
a=(buffer[0][1]-buffer[1][1])/(buffer[0][0]-buffer[1][0]);
b=buffer[0][1]-(buffer[0][1]-buffer[1][1])/(buffer[0][0]-buffer[1][0])*buffer[0][0];
}

//同理可证直线CD的描述
doublec,d;
if((buffer[2][0]-buffer[3][0])==0)
{
c=0;
d=buffer[2][1];
}
else
{
c=(buffer[2][1]-buffer[3][1])/(buffer[2][0]-buffer[3][0]);
d=buffer[2][1]-(buffer[2][1]-buffer[3][1])/(buffer[2][0]-buffer[3][0])*buffer[2][0];
}

//现在问题转变为y=ax+b和y=cx+d两条直线的交点
//x=(d-b)/(a-c)
//y=a(d-b)/(a-c)+b;
doubleResultX,ResultY;
ResultX=(d-b)/(a-c);
ResultY=a*(d-b)/(a-c)+b;

printf("x=%f,y=%f",ResultX,ResultY);

return0;
}

❸ 求两条直线的交点!! C++

代码和注释给你, 自己整理吧

//.h文件
#ifndef _LINE_H_
#define _LINE_H_

#include <stdio.h>
#include <iostream.h>

class Point
{
public:
/* 点的坐标 */
int x1;
int y1;

public:
void SetXY(int x, int y);
void GetXY(int &x, int &y);

/* 2个构造函数 */
Point();
Point(int x, int y);
};

class Line : public Point /* line继承point */
{
public:
/* 另一个点的坐标 */
int x2;
int y2;

public:
void SetPoint(Point* point1, Point* point2);

/* 3个构造函数 */
Line();
Line(int x1, int y1, int x2, int y2);
Line(Point* point1, Point* point2);

/* 是否相交
** 相交返回0, 交点为intersect_point
** 不相交返回-1, intersect_point为空
** 两直线相同返回1, intersect_point为空
*/
int Intersect(Line* another_line, Point* intersect_point);
};

#endif

//.c文件
#include "1.h"

Point::Point()
{
x1 = 0;
y1 = 0;
}

Point::Point(int x, int y)
{
x1 = x;
y1 = y;
}

void Point::SetXY(int x, int y)
{
x1 = x;
y1 = y;
}

void Point::GetXY(int &x, int &y)
{
x = x1;
y = y1;
}

Line::Line()
{
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
}

Line::Line(int x1, int y1, int x2, int y2)
{
this->x1 = x1;
this->x2 = x2;
this->y1 = y1;
this->y2 = y2;
}

void Line::SetPoint(Point* point1, Point* point2)
{
x1 = point1->x1;
y1 = point1->y1;
x2 = point2->x1;
y2 = point2->y1;
}

Line::Line(Point* point1, Point* point2)
{
x1 = point1->x1;
y1 = point1->y1;
x2 = point2->x1;
y2 = point2->y1;
}

int Line::Intersect(Line* another_line, Point* intersect_point)
{
/* y = ax + b */
int a_my, b_my;
b_my = (x1 * y2 - y1 * x2) / (x1 - x2);
a_my = (y1 - y2) / (x1 - x2);

/* check if point */
Point* point = (Point*)another_line;
if(a_my * point->x1 + b_my == point->y1)
{
intersect_point = point;
return 0;
}

int a_other, b_other;
a_other = (another_line->x1 * another_line->y2 - another_line->y1 * another_line->x2) / (another_line->x1 - another_line->x2);
b_other = (another_line->y1 - another_line->y2) / (another_line->x1 - another_line->x2);

if(a_other == a_my)
{
if(b_my == b_other)
{
intersect_point = NULL;
return -1; //not intersect
}
else
{
return 1; //the same
}
}
else
{
intersect_point->x1 = ((b_other - b_my) / (a_my - a_other));
intersect_point->y1 = (a_my * intersect_point->x1 + b_my);
return 0; //intersect
}

}

/* test */
int main()
{
Point point1(0, 1);
Point point2(1, 2);

Line line1(0, 1, 1, 2);
Line line2(&point1, &point1);

Line po;
int a = line1.Intersect((Line*)&line2, &po);
if(a == 0)
{
cout<<"yes"<<endl;
cout<<"x = "<<po.x1<<", y = "<<po.y1<<endl;
}
else if(a == -1)
{
cout<<"no"<<endl;
}
else
{
cout<<"same"<<endl;
}

return 0;
}

❹ 急! 用c++程序求两条直线的交点

比较粗略的答案
#include <iostream.h>
class line
{ double a,b,c;
public:
line ( double a1, double b1, double c1)
{a=a1;b=b1;c=c1;}
friend void setpoint(line &A,line &B);
};
void setpoint(line &A,line &B)
{ double x;
if(A.a/B.a!=A.b/B.b)
{x=-100;
do
{
if(( (-A.c-A.a*x)/A.b - (-B.c-B.a*x)/B.b ) < 0.00001)
break;

x=x+0.00001;
}while(1);
cout<<x<<" "<<(-A.c-A.a*x)/A.b;}
else cout<<"error"<<endl;}

void main ()
{line A(-2,-1,4),B(1,-10,-3);
setpoint(A,B);
}

❺ 求两直线交点并将交点排序的C语言源代码

void jiaodian(int a,int b) //这是个功能函数,a和b的值通过形参传过来
{int i,y,k,A[100];
k=tana;
for(y=0;y<=100;y++)
A[y]=(y-b)/k; //tana(a=0~09)斜率为正,不用排序

for(i=0;i<=100;i++)
printf("%4d",A[i]);
}

void main() //主函数
{int a,b;
{
scanf("%d",a); //通过键盘输入a
scanf("%d",b); //通过键盘输入b ,这样你的a,b就可以是任意的了。
jiaodian(a,b); //调用上面的功能函数;
}

试试吧,建议初学者还是从基础一些的学起,这道题目涉及了函数和数组,没学直接上手会比较困难~~加油吧

❻ 用C语言制作一个求两条线段交点的程序

//先判断两条线段是否不平行(最好同时判断是否有交点并且不平行,因为浮//点运算不精确),然后计算两条线段的交点。以下是C语言代码:
#include<stdio.h>
#include<math.h>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct point{double x,y;};

//计算交叉乘积(P1-P0)x(P2-P0)
double xmult(point p1,point p2,point p0){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

//判点是否在线段上,包括端点
int dot_online_in(point p,point l1,point l2){
return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
}

//判两点在线段同侧,点在线段上返回0
int same_side(point p1,point p2,point l1,point l2){
return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}

//判两直线平行
int parallel(point u1,point u2,point v1,point v2){
return zero((u1.x-u2.x)*(v1.y-v2.y)-(v1.x-v2.x)*(u1.y-u2.y));
}

//判三点共线
int dots_inline(point p1,point p2,point p3){
return zero(xmult(p1,p2,p3));
}

//判两线段相交,包括端点和部分重合
int intersect_in(point u1,point u2,point v1,point v2){
if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}

//计算两线段交点,请判线段是否相交(同时还是要判断是否平行!)
point intersection(point u1,point u2,point v1,point v2){
point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
return ret;
}

int main(void)
{
point u1,u2,v1,v2,ans;
printf("请输入线段1的两个端点:\n");
scanf("%lf%lf%lf%lf",&u1.x,&u1.y,&u2.x,&u2.y);
printf("请输入线段2的两个端点:\n");
scanf("%lf%lf%lf%lf",&v1.x,&v1.y,&v2.x,&v2.y);
if (parallel(u1,u2,v1,v2)||!intersect_in(u1,u2,v1,v2)){
printf("无交点!\n");
}
else{
ans=intersection(u1,u2,v1,v2);
printf("交点为:(%lf,%lf)",ans.x,ans.y);
}
return 0;
}

❼ 用C语言,已知一条直线的一点和斜率,令一条直线的两点,如何求两条直线的交点x,y

首先判断第2条直线是否是垂直于x轴的,如果是,单独处理。
再求出第二条直线斜率k,并判断k是否和第一条直线斜率相同,若相同,则不存在焦点,或者有无穷多个交点。
以上条件都不满足则直接解方程求出交点
#include <stdio.h>
#include <math.h>
#define EQUAL(x, y) (fabs((x) - (y)) <= 1e-6)

int main()
{
double k1, b1, k2, b2;
double x1, y1, x2, y2;
double resultx, resulty;

printf("Input the k and b: ");
scanf("%llf %llf", &k1, &b1);
printf("Input the first point's x and y cand: ");
scanf("%llf %llf", &x1, &y1);
printf("Input the first point's x and y cand: ");
scanf("%llf %llf", &x2, &y2);

if (EQUAL(x1, x2))
{
printf("The point is (%.2llf, %.2llf).\n", x1, x1 * k1 + b1);
}
else
{
k2 = (y1 - y2) / (x1 - x2);
b2 = (y1 * x2 - x1 * y2) / (x2 - x1);

if (EQUAL(k2, k1))
{
printf("None point!\n");
}
else
{
resultx = (b1 - b2) / (k2 - k1);
resulty = k1 * resultx + b1;
printf("The point is (%.2llf, %.2llf).\n", resultx, resulty);
}
}

return 0;
}

❽ 已知两条直线(两点式),求交点坐标,C语言实现

不喜欢只给程序,给出链接网页链接,主要讲述一下两点确认直线,点到直线距离,两条直线的交点等问题的解决方法,并给出python程序。之前的回答太复杂,方法选的好,求交点不需要判断太多内容。网页链接

```python
defGeneralEquation(first_x,first_y,second_x,second_y):
#一般式Ax+By+C=0
#fromhttp://www.cnblogs.com/DHUtoBUAA/
A=second_y-first_y
B=first_x-second_x
C=second_x*first_y-first_x*second_y
returnA,B,C
```
```python
defGetIntersectPointofLines(x1,y1,x2,y2,x3,y3,x4,y4):
#fromhttp://www.cnblogs.com/DHUtoBUAA/
A1,B1,C1=GeneralEquation(x1,y1,x2,y2)
A2,B2,C2=GeneralEquation(x3,y3,x4,y4)
m=A1*B2-A2*B1
ifm==0:
print("平行,无交点")
else:
x=(C2*B1-C1*B2)/m
y=(C1*A2-C2*A1)/m
returnx,y
```

❾ c语言如何实现两直线(一般式)求交点,或者说如何解2*3增广矩阵

二元一次方程组

a1x+b1y=c1

a2x+b2y=c2

当a1b2<>b1a2时,方程组有唯一的一组解。

若a1b2==b1a2,且c1*b2<>b1*c2或a1*c2<>c1*a2时,方程组无解;

若a1b2==b1a2,且c1*b2==b1*c2且a1*c2==c1*a2时,方程组有无数组解:

#include <stdio.h>

int main()

{ double a1,b1,c1,a2,b2,c2,x,y;

scanf("%lf%lf%lf",&a1,&b1,&c1);

scanf("%lf%lf%lf",&a2,&b2,&c2);

if(a1*b2-b1*a2)

{ printf("方程组有唯一的一组解: ");

printf("x=%f ",(c1*b2-b1*c2)/(a1*b2-b1*a2));

printf("y=%f ",(a1*c2-c1*a2)/(a1*b2-b1*a2));

}

else if((c1*b2-b1*c2)||(a1*c2-c1*a2))

printf("方程组无解 ");

else

printf("方程组有无数组解 ");

return 0;

}

❿ C语言编写函数实现求两个线段的交点(定义一个函数而不是直接写求解的程序)

欲求二函数交点,必须有二函数的代数表达式,或者必须有两条线段的端点坐标,否则,不可能得到线段的交点。