We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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:
Define the findPoint function which takes the coordinates of points P and Q and calculates the coordinates of the reflected point R.
Return the calculated coordinates.
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*);intparse_int(char*);int*findPoint(intpx,intpy,intqx,intqy,int*result_count){// Allocate memory for the result arrayint*result=(int*)malloc(2*sizeof(int));// Calculate the reflected point coordinatesresult[0]=2*qx-px;result[1]=2*qy-py;// Set the result count to 2 since we are returning 2 integers*result_count=2;returnresult;}intmain(){FILE*fptr=fopen(getenv("OUTPUT_PATH"),"w");intn=parse_int(ltrim(rtrim(readline())));for(intn_itr=0;n_itr<n;n_itr++){char**first_multiple_input=split_string(rtrim(readline()));intpx=parse_int(*(first_multiple_input+0));intpy=parse_int(*(first_multiple_input+1));intqx=parse_int(*(first_multiple_input+2));intqy=parse_int(*(first_multiple_input+3));intresult_count;int*result=findPoint(px,py,qx,qy,&result_count);for(inti=0;i<result_count;i++){fprintf(fptr,"%d",*(result+i));if(i!=result_count-1){fprintf(fptr," ");}}fprintf(fptr,"\n");free(result);}fclose(fptr);return0;}char*readline(){size_talloc_length=1024;size_tdata_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';}}returndata;}char*ltrim(char*str){if(!str){return'\0';}if(!*str){returnstr;}while(*str!='\0'&&isspace(*str)){str++;}returnstr;}char*rtrim(char*str){if(!str){return'\0';}if(!*str){returnstr;}char*end=str+strlen(str)-1;while(end>=str&&isspace(*end)){end--;}*(end+1)='\0';returnstr;}char**split_string(char*str){char**splits=NULL;char*token=strtok(str," ");intspaces=0;while(token){splits=(char**)realloc(splits,sizeof(char*)*++spaces);if(!splits){returnsplits;}splits[spaces-1]=token;token=strtok(NULL," ");}returnsplits;}intparse_int(char*str){char*endptr;intvalue=strtol(str,&endptr,10);if(endptr==str||*endptr!='\0'){exit(EXIT_FAILURE);}returnvalue;}
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find the Point
You are viewing a single comment's thread. Return to all 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:
findPoint
function which takes the coordinates of points P and Q and calculates the coordinates of the reflected point R.main
function, handle the input and output according to the specified format.Here's the complete implementation:
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.