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

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

คำนวณระยะทางยุคลิดระหว่างจุด (x1,y1) และ (x2,y2) ด้วยสูตร √[(x2-x1)² + (y2-y1)²]

ตัวอย่าง

(4,9) และ (5,10) → Distance = 1.414214

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

#include <stdio.h>
#include <math.h>

void two_dis(float x1, float y1, float x2, float y2) {
    float dis = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
    printf("Distance between 2 points are: %f", dis);
}

int main() {
    float x1=4, y1=9, x2=5, y2=10;
    two_dis(x1, y1, x2, y2);
    return 0;
}

ผลลัพธ์

Distance between 2 points are: 1.414214