Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรม C คำนวณระยะทางระหว่างสามจุดใน 3D


ให้ด้วยระนาบสามมิติและด้วยเหตุนี้สามพิกัดและภารกิจคือการค้นหาระยะห่างระหว่างจุดที่กำหนดและแสดงผล

ในระนาบสามมิติ มีสามแกนที่เป็นแกน x โดยมีพิกัดเป็น (x1, y1, z1), แกน y ที่มีพิกัดเป็น (x2, y2, z2) และแกน z ที่มีพิกัดเป็น (x3 , y3, z). ในการคำนวณระยะห่างระหว่างกันนั้นมีสูตรโดยตรงที่ให้ไว้ด้านล่าง

$$\sqrt{\lgroup x2-x1\rgroup^{2}+\lgroup y2-y1\rgroup^{2}+\lgroup z2-z1\rgroup^{2}}$$

ด้านล่างคือแผนภาพแสดงแกนและพิกัดที่แตกต่างกันสามแกน

โปรแกรม C คำนวณระยะทางระหว่างสามจุดใน 3D

แนวทางที่ใช้ด้านล่างมีดังนี้

  • ป้อนพิกัดเป็น (x1, y1, z1), (x2, y2, z2) และ (x3, y3, z3)
  • ใช้สูตรเพื่อคำนวณความแตกต่างระหว่างจุดเหล่านี้
  • พิมพ์ระยะทาง

อัลกอริทึม

Start
Step 1-> declare function to calculate distance between three point
   void three_dis(float x1, float y1, float z1, float x2, float y2, float z2)
      set float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2) * 1.0)
      print dis
step 2-> In main()
   Set float x1 = 4
   Set float y1 = 9
   Set float z1 = -3
   Set float x2 = 5
   Set float y2 = 10
   Set float z2 = 9
   Call three_dis(x1, y1, z1, x2, y2, z2)
Stop

ตัวอย่าง

#include <stdio.h>
#include<math.h>
//function to find distance bewteen 3 point
void three_dis(float x1, float y1, float z1, float x2, float y2, float z2) {
   float dis = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2) * 1.0);
   printf("Distance between 3 points are : %f", dis);
   return;
}
int main() {
   float x1 = 4;
   float y1 = 9;
   float z1 = -3;
   float x2 = 5;
   float y2 = 10;
   float z2 = 9;
   three_dis(x1, y1, z1, x2, y2, z2);
   return 0;
}

ผลลัพธ์

หากเราเรียกใช้โค้ดข้างต้น จะเกิดผลลัพธ์ดังต่อไปนี้

Distance between 3 points are : 12.083046