编程:读入三个点的坐标,确定他们是否在一条线上
发布网友
发布时间:2023-12-28 06:05
我来回答
共2个回答
热心网友
时间:2024-12-12 18:38
没指明用什么语言, 用C写了。
#include <stdio.h>
#include <math.h>
#define jing 0.000001
int main(){
double x1, y1, x2, y2, x3, y3;
double k, b, t;
int flag = 0;
//输入3个坐标:
printf("Input point A:x, y:");
scanf("%lf, %lf", &x1, &y1);
printf("Input point b:x, y:");
scanf("%lf, %lf", &x2, &y2);
printf("Input point C:x, y:");
scanf("%lf, %lf", &x3, &y3);
if (fabs(x2-x1)<jing) {//平行于x轴,斜率为0,只需判断x3
if (fabs(x2-x3) < jing)
flag = 1;
} else {
k = (y2-y1)/(x2-x1); //计算 斜率
b = y1-k*x1;
t = k*x3 + b;
if (fabs(y3-t) < jing)
flag = 1;
}
if (flag == 1)
printf("三点在一条直线上。\n");
else
printf("三点不在一条直线上。\n");
return 0;
}
热心网友
时间:2024-12-12 18:39
#include<stdio.h>
#include<math.h>
int main()
{
float x1, x2, x3, y1, y2, y3;
//输入3个坐标:
printf("Input point A:x, y:");
scanf("%lf, %lf", &x1, &y1);
printf("Input point b:x, y:");
scanf("%lf, %lf", &x2, &y2);
printf("Input point C:x, y:");
scanf("%lf, %lf", &x3, &y3);
if(x1=x3||x1=x2||x2=x3) {
if(x1=x2=x3) printf("在同一直线上。\n")
else printf("不在同一直线上。\n");
}
else{
if ((y1 - y2) / (x1 - x2) == (y2 - y3) / (x2 - x3))
printf("在同一直线上。\n");
else printf("不在同一直线上。\n");
}
return 0;
}