Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ ตรวจสอบว่าจุดในระนาบ 3 มิติอยู่บนระนาบเดียวกันหรือไม่

กำหนดจุด (x1, y1, z1), (x2, y2, z2), (x3, y3, z3) และ (x4, y4, z4) โปรแกรมต้องตรวจสอบว่าจุดที่กำหนดเป็น coplanar (อยู่บนระนาบเดียวกัน) หรือไม่ จุดถูกกล่าวว่าเป็น coplanar หากอยู่ภายใต้ระนาบเดียวกัน และหากอยู่ภายใต้ระนาบที่ต่างกัน จุดจะไม่เป็น coplanar

ภาพด้านล่างแสดงจุด 4 จุดทั้งหมดอยู่ภายใต้ระนาบเดียวกันคือระนาบ xy หมายถึงจุดเป็น coplanar

โปรแกรม C++ ตรวจสอบว่าจุดในระนาบ 3 มิติอยู่บนระนาบเดียวกันหรือไม่

ภาพด้านล่างแสดงจุด 4 จุดอยู่ภายใต้ระนาบที่ต่างกัน แสดงว่าจุดไม่ใช่ coplanar

โปรแกรม C++ ตรวจสอบว่าจุดในระนาบ 3 มิติอยู่บนระนาบเดียวกันหรือไม่

ตัวอย่าง

Input: x1 = 2, y1 = 3, z1 = 1, x2 = 1, y2 = 9, z2 = 3, x3 = 3, y3 = 1, z3 = 5, x4 = 23, y4 = 21, z4 = 9 Output: they are not coplanar
Input: x1 = 3, y1 = 2, z1 = -5, x2 = -1, y2 = 4, z2 = -3, x3 = -3, y3 = 8, z3 = -5, x4 = -3, y4 = 2, z4 = 1 Output: they are coplanar

วิธีการที่ใช้ในโปรแกรมด้านล่าง −

  • รับจุดในตัวแปร (x1, y1, z1), (x2, y2, z2), (x3, y3, z3) และ (x4, y4, z4)
  • หาสมการของระนาบและตรวจสอบว่าเป็นไปตามเงื่อนไขหรือไม่
  • แสดงว่าจุดเป็น coplanar หรือไม่

อัลกอริทึม

START
Step 1 → ประกาศฟังก์ชันตรวจสอบจุดใน 3 มิติว่าเป็น coplanar หรือไม่
  void check_coplanar(int x1,int y1,int z1,int x2,int y2,int z2, int x3, int y3, int z3, int x, int y, int z)
  ประกาศตัวแปร int a1 = x2 - x1
  ประกาศตัวแปร int b1 = y2 - y1
  ประกาศตัวแปร int c1 = z2 - z1
  ประกาศตัวแปร int a2 = x3 - x1
  ประกาศตัวแปร int b2 = y3 - y1
  ประกาศตัวแปร int c2 = z3 - z1
  ประกาศตัวแปร int a = b1 * c2 - b2 * c1
  ประกาศตัวแปร int b = a2 * c1 - a1 * c2
  ประกาศตัวแปร int c = a1 * b2 - b1 * a2
  ประกาศตัวแปร int d = (- a * x1 - b * y1 - c * z1)
  ตรวจสอบ
    IF(a * x + b * y + c * z + d == 0)
      พิมพ์ coplanar
    End
    Else
      พิมพ์ not coplanar
    End
Step 2 → ใน main()
  ประกาศและกำหนดตัวแปร int x1 = 2, y1 = 3, z1 = 1, x2 = 1, y2 = 9, z2 = 3, x3 = 3, y3 = 1, z3 = 5, x4 = 23, y4 = 21, z4 = 9
  เรียก check_coplanar(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4)
STOP

ตัวอย่างโค้ด

#include <bits/stdc++.h> 
using namespace std; 

// คำนวณจุดในระนาบว่าเป็น coplanar หรือไม่
void check_coplanar(int x1,int y1,int z1,int x2,int y2,int z2, int x3, int y3, int z3, int x, int y, int z) { 
    int a1 = x2 - x1; 
    int b1 = y2 - y1; 
    int c1 = z2 - z1; 
    int a2 = x3 - x1; 
    int b2 = y3 - y1; 
    int c2 = z3 - z1; 
    int a = b1 * c2 - b2 * c1; 
    int b = a2 * c1 - a1 * c2; 
    int c = a1 * b2 - b1 * a2; 
    int d = (- a * x1 - b * y1 - c * z1); 
    if(a * x + b * y + c * z + d == 0) 
        cout << "they are coplanar" << endl; 
    else 
        cout << "they are not coplanar" << endl; 
} 

int main() { 
    int x1 = 2; 
    int y1 = 3; 
    int z1 = 1; 
    int x2 = 1; 
    int y2 = 9; 
    int z2 = 3; 
    int x3 = 3; 
    int y3 = 1; 
    int z3 = 5; 
    int x4 = 23; 
    int y4 = 21; 
    int z4 = 9; 
    check_coplanar(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4); 
    return 0; 
}

ผลลัพธ์

หากรันโค้ดด้านบนจะได้ผลลัพธ์ดังนี้ −

they are not coplanar