❶ 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語言編寫函數實現求兩個線段的交點(定義一個函數而不是直接寫求解的程序)
欲求二函數交點,必須有二函數的代數表達式,或者必須有兩條線段的端點坐標,否則,不可能得到線段的交點。