Struct: Two 3D points

PHOTO EMBED

Tue Jun 11 2024 04:02:57 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c #struct

#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));
}
content_copyCOPY

For example: Input Result 1 2 3 4 5 6 distance between two points is 5.196152. 0 -2 -3 7 5 6 distance between two points is 13.379088. Objective Create a C program to calculate the distance between two points in 3D space using a structure named Point3D. The program should allow users to input coordinates for the points and should output the calculated distance between them. Structure Definition Declare a structure named Point3D with the following members: x-coordinate (type: float) y-coordinate (type: float) z-coordinate (type: float) Main Function Tasks Inside the main() function, allocate two Point3D structures. Initialize the structures with test values: Point1 (p1): <1, 2, 3> Point2 (p2): <4, 5, 6> To assign these values, use the dot operator to access each member and assign the appropriate value. Function Declaration and Definition Declare and define a function named compute_distance3d that: Takes two Point3D structures by value as input. Calculates the distance between the two points (you may ask for the formula). Returns the calculated distance as a float. Note: Utilize the math.h library for the square root function. User Input and Output Modify the program to query the user for the coordinates of two 3D points. Pass these points to compute_distance3d. Print out the result returned from compute_distance3d.