C++程序检查三个点是否共线
给定三个不同值的点,任务是检查这些点是否共线。
如果点位于同一条直线上,则称它们为共线;如果它们位于不同的直线上,则称它们为非共线。以下是共线和非共线点的图形。

输入
x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 4, y3 = 5
输出
no points are not collinear
输入
x1 = 1, y1 = 1, x2 = 1, y2 = 4, x3 = 1, y3 = 5
输出
points are collinear
下面程序中使用的方法如下
输入点为 (x1, y1), (x2, y2), (x3, y3)
应用三角形面积公式 x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)
检查以下条件:
如果三角形面积为 0,则打印点共线
如果三角形面积不为 0,则打印点不共线
打印最终结果
算法
Start Step 1→ declare function to check if points are collinear or not void check_collinear(int x1, int y1, int x2, int y2, int x3, int y3) declare int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) IF (a == 0) Print "yes points are collinear" End Else Print "no points are not collinear" Step 2→ In main() Declare int x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 4, y3 = 5 Call check_collinear(x1, y1, x2, y2, x3, y3) Stop
示例
#include <bits/stdc++.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
//check if points are collinear or not
void check_collinear(int x1, int y1, int x2, int y2, int x3, int y3){
int a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
if (a == 0)
cout << "yes points are collinear";
else
cout << "no points are not collinear";
}
int main(){
int x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 4, y3 = 5;
check_collinear(x1, y1, x2, y2, x3, y3);
return 0;
}输出
如果运行以上代码,它将生成以下输出:
no points are not collinear
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP