ให้ด้วยคะแนนที่มีค่าต่างกันสามคะแนนและภารกิจคือตรวจสอบว่าคะแนนนั้นใกล้เคียงกันหรือไม่
คะแนนจะเรียกว่า collinear ถ้าอยู่บนเส้นเดียวกัน และไม่ colinear ถ้าอยู่บนเส้นที่ต่างกัน ด้านล่างนี้คือตัวเลขของจุด collinear และ non-collinear

ป้อนข้อมูล
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