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

c语言直线的交点个数

发布时间: 2023-01-29 07:57:35

‘壹’ c语言求两直线的交点

1、首先在打开的C语言软件中,先用for循环输入两个集合放到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++程序求两条直线的交点

比较粗略的答案
#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(10,2)=45个。

‘伍’ 同一平面内的五条直线最多有几个交点,请画图说明

一个平面内五条直线两两相交时交点最多,按照排列组合计算交点个数为C(5,2)=10 。用图表示如下:

该表达式不适用于和任意坐标轴垂直的直线

‘陆’ c++直线交点数

假设f(n)是n条直线的最大交点数,那么n+1条直线就是f(n+1)=f(n)+n,因为第n+1条直线最多 增加n个交点
程序很简单
#include <iostream>
using namespace std;
long f(long n);
int main(int argc, char* argv[]){
long n;
cin >> n;
cout << f(n) <<endl;
return 0;
}
long f(long n) {
if(n>1000||n<2) return -1; //异常处理
long values[1001];
values[2] = 1;
for(long i=3; i< n; i ++) {
values[i] = values[i-1] +i;
}
return values[n];
}

‘柒’ 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++

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

//.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语言编写函数实现求两个线段的交点(定义一个函数而不是直接写求解的程序)

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

‘拾’ 用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;
}