• + 0 comments

    To solve the problem of finding the reflection (inversion) of point P across point Q, we can use simple geometry. The reflection R of point P across point Q can be found using the following formulas for the coordinates of R:

    [ R_x = 2Q_x - P_x ] [ R_y = 2Q_y - P_y ]

    Here's how you can implement the solution in C:

    1. Define the findPoint function which takes the coordinates of points P and Q and calculates the coordinates of the reflected point R.
    2. Return the calculated coordinates.
    3. In the main function, handle the input and output according to the specified format.

    Here's the complete implementation:

    #include <assert.h>
    #include <ctype.h>
    #include <limits.h>
    #include <math.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char* readline();
    char* ltrim(char*);
    char* rtrim(char*);
    char** split_string(char*);
    
    int parse_int(char*);
    
    int* findPoint(int px, int py, int qx, int qy, int* result_count) {
        // Allocate memory for the result array
        int* result = (int*)malloc(2 * sizeof(int));
        
        // Calculate the reflected point coordinates
        result[0] = 2 * qx - px;
        result[1] = 2 * qy - py;
        
        // Set the result count to 2 since we are returning 2 integers
        *result_count = 2;
        
        return result;
    }
    
    int main()
    {
        FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
    
        int n = parse_int(ltrim(rtrim(readline())));
    
        for (int n_itr = 0; n_itr < n; n_itr++) {
            char** first_multiple_input = split_string(rtrim(readline()));
    
            int px = parse_int(*(first_multiple_input + 0));
            int py = parse_int(*(first_multiple_input + 1));
            int qx = parse_int(*(first_multiple_input + 2));
            int qy = parse_int(*(first_multiple_input + 3));
    
            int result_count;
            int* result = findPoint(px, py, qx, qy, &result_count);
    
            for (int i = 0; i < result_count; i++) {
                fprintf(fptr, "%d", *(result + i));
    
                if (i != result_count - 1) {
                    fprintf(fptr, " ");
                }
            }
    
            fprintf(fptr, "\n");
            free(result);
        }
    
        fclose(fptr);
    
        return 0;
    }
    
    char* readline() {
        size_t alloc_length = 1024;
        size_t data_length = 0;
    
        char* data = (char*)malloc(alloc_length);
    
        while (true) {
            char* cursor = data + data_length;
            char* line = fgets(cursor, alloc_length - data_length, stdin);
    
            if (!line) {
                break;
            }
    
            data_length += strlen(cursor);
    
            if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
                break;
            }
    
            alloc_length <<= 1;
    
            data = (char*)realloc(data, alloc_length);
    
            if (!data) {
                data = '\0';
    
                break;
            }
        }
    
        if (data[data_length - 1] == '\n') {
            data[data_length - 1] = '\0';
    
            data = (char*)realloc(data, data_length);
    
            if (!data) {
                data = '\0';
            }
        } else {
            data = (char*)realloc(data, data_length + 1);
    
            if (!data) {
                data = '\0';
            } else {
                data[data_length] = '\0';
            }
        }
    
        return data;
    }
    
    char* ltrim(char* str) {
        if (!str) {
            return '\0';
        }
    
        if (!*str) {
            return str;
        }
    
        while (*str != '\0' && isspace(*str)) {
            str++;
        }
    
        return str;
    }
    
    char* rtrim(char* str) {
        if (!str) {
            return '\0';
        }
    
        if (!*str) {
            return str;
        }
    
        char* end = str + strlen(str) - 1;
    
        while (end >= str && isspace(*end)) {
            end--;
        }
    
        *(end + 1) = '\0';
    
        return str;
    }
    
    char** split_string(char* str) {
        char** splits = NULL;
        char* token = strtok(str, " ");
    
        int spaces = 0;
    
        while (token) {
            splits = (char**)realloc(splits, sizeof(char*) * ++spaces);
    
            if (!splits) {
                return splits;
            }
    
            splits[spaces - 1] = token;
    
            token = strtok(NULL, " ");
        }
    
        return splits;
    }
    
    int parse_int(char* str) {
        char* endptr;
        int value = strtol(str, &endptr, 10);
    
        if (endptr == str || *endptr != '\0') {
            exit(EXIT_FAILURE);
        }
    
        return value;
    }
    

    This program reads the input, calculates the reflected point for each pair of points provided, and outputs the results. The function findPoint performs the necessary calculations and returns the result as an array of integers.