#include <stdio.h>
#include <math.h>
struct Point3D
{
float x;
float y;
float z;
};
float compute_distance3d(struct Point3D p1, struct Point3D p2);
int main(void)
{
struct Point3D p1;
struct Point3D p2;
// Initialize the structures with test values
p1.x = 1.0;
p1.y = 2.0;
p1.z = 3.0;
p2.x = 4.0;
p2.y = 5.0;
p2.z = 6.0;
// Calculate the distance between the test values
float distance = compute_distance3d(p1, p2);
// Query user for input coordinates
printf("Enter coordinates for Point1 (x y z): ");
scanf("%f %f %f", &p1.x, &p1.y, &p1.z);
printf("Enter coordinates for Point2 (x y z): ");
scanf("%f %f %f", &p2.x, &p2.y, &p2.z);
// Calculate and print the distance based on user input
distance = compute_distance3d(p1, p2);
printf("Distance between the two points is %f.\n", distance);
return 0;
}
float compute_distance3d(struct Point3D p1, struct Point3D p2)
{
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2) + pow(p2.z - p1.z, 2));
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter