SOLUTION OF THE EXAMPLE "TRANSLATION OF POINTS"

Welcome to the page dedicated to solving the problem "Translation of points". This task is an interesting combination of geometry and programming, letting you apply basic mathematical operations and algorithmic thinking in practice. The goal is to translate a set of points in the plane so that their centroid ends up at the origin, which is a common step in data analysis and computational geometry.
On this page you will find: Whether you are a beginner or an advanced programmer, the goal is for this guide to help you better understand the concept of the centroid and how it is adjusted in the context of programming tasks.

Text:

/*
The coordinates of  N points in the plane are given. Translate the points so that their centroid is at the origin.

Input
The first line of the standard input contains a natural number n (1≤n≤100). The next n
 lines contain two real numbers each, which are the x and y coordinates of the points.

Output
On the standard output show the coordinates of the points after the translation, for each point in one line its
x and then y coordinate, separated by one space and shown with two decimal places.

Example
Input
3
0 0
1 0
2 3
Output
-1.00 -1.00
0.00 -1.00
1.00 2.00
*/
A complete input/output example:Adding more input examples helps users better understand how the algorithm works in different scenarios. For example:
  1. One point:
Input1  
3.0 4.0
​Output​0.00 0.00
2. Points on a straight line:Input
​3  
0.0 0.0  
1.0 1.0  
2.0 2.0
Output
​-1.00 -1.00  
0.00 0.00  
1.00 1.00

3. Symmetric points in the plane:Input
4  
-1.0 -1.0  
1.0 1.0  
-1.0 1.0  
1.0 -1.0
Output
-1.00 -1.00  
1.00 1.00  
-1.00 1.00  
1.00 -1.00


Solution in C

#include <stdio.h>

int main() {
int n; // Number of points
    scanf("%d", &n); // Reading the number of points

double x[100], y[100]; // Arrays for storing the x and y coordinates of the points
for (int i = 0; i < n; i++) {
scanf("%lf %lf", &x[i], &y[i]); // Reading the coordinates of each point
    }

double Tx, Ty;      // Coordinates of the centroid (center of mass)
    double zbX = 0, zbY = 0; // Variables for the sum of the x and y coordinates

// Calculating the sums of the x and y coordinates
for (int i = 0; i < n; i++) {
        zbX += x[i];
        zbY += y[i];
    }

// Calculating the coordinates of the centroid
    Tx = zbX / n; 
    Ty = zbY / n;

// Translating the points
for (int i = 0; i < n; i++) {
        printf("%.2f %.2f\n", x[i] - Tx, y[i] - Ty); // Printing the new coordinates with two decimal places
    }

return 0; // End of program
}

A detailed explanation of the code:

  1. Declaring and entering the number of points:
    • int n; scanf("%d", &n);
      This part of the code reads the number of points n, where 1≤n≤100.
  2. Storing the coordinates of the points:
    • Two arrays (x and y) are used to store the x and y coordinates of all the points.
  3. Calculating the sums of the x and y coordinates:
    • zbX and zbY are initially set to 0.
    • In the loop for (int i = 0; i < n; i++), each coordinate is added to the matching sum.
  4. Calculating the centroid:
    • The centroid (Tx, Ty) is calculated as the average of all the x and y coordinates: Tx=sum of all x coordinates/n, Ty=sum of all y coordinates/n​
  5. Translating the points:
    • Each point is moved by the centroid vector from its original position.
    • The new coordinates of point i are obtained as: xnew=xold−Tx, ynew=yold−Ty
  6. Printing the result:
    • The coordinates after the translation are printed with two decimal places using printf("%.2f %.2f\n", ...).

Explanation of the task

This program is written in C++ and translates points in the plane so that their centroid ends up at the origin. Here is how the program works:
  1. Entering the number of points:
    • First, the program reads the number of points n to be processed.
  2. Entering the coordinates:
    • In the next step, the program uses the arrays x and y to store the x and y coordinates of the points. It reads the coordinates of each point from the standard input.
  3. Calculating the centroid:
    • The program uses the variables zbX and zbY to add up the x and y coordinates of all the points. The centroid (center) is calculated as the average of the coordinates of all the points, i.e. Tx and Ty are the average x and y coordinates.
  4. Translating the points:
    • After the centroid is calculated, each point is translated by subtracting the coordinates of the centroid (Tx and Ty) from its original coordinates. The result is shown with two decimal places.
  5. Printing the result:
    • Finally, the program uses printf to format and print the coordinates of each point after the translation, with two decimal places.

Why does this solution work?


The centroid of a set of points in the plane is defined as the average of their coordinates. By translating all the points by the negative values of the centroid coordinates (Tx,Ty), the centroid moves to the origin (0,0).
Comment: The code is optimized for O(n) complexity, which means that each point is processed once in the two main loops. This solution is efficient for the given conditions n≤100.

Solution in C++

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

int main() {
int n;
cin >> n;

vector<double> x(n), y(n);
double zbX = 0, zbY = 0;

// Entering the coordinates and adding them up
    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i];
        zbX += x[i];
        zbY += y[i];
    }

// Calculating the centroid
double Tx = zbX / n, Ty = zbY / n;

// Translating the points and printing the result
for (int i = 0; i < n; i++) {
cout << fixed << setprecision(2) << (x[i] - Tx) << " " << (y[i] - Ty) << endl;
    }

return 0;
}
Explanation of the task:
  1. Data input:
    • The program reads the number of points n, and then their coordinates (x, y), and stores them in data structures.
  2. Calculating the centroid:
    • The centroid is the center of mass of all the points, calculated as the average of the x coordinates and the y coordinates: Tx=∑x/n, Ty=∑y/n​
  3. Translation:
    • Each point is moved so that the centroid ends up at the origin. The translation is done by subtracting the coordinates of the centroid from the coordinates of each point: x′=x−Tx, y′=y−Ty​
  4. Formatted output:
    • Formatting to two decimal places is used so that the output is clear and precise.
Efficiency: It is important to stress the efficiency of the algorithm. In this case:
  1. Complexity:
    The algorithm has a time complexity of O(n), because:
    • The first loop goes through all the points to add up their coordinates (O(n)).
    • The second loop goes through all the points to translate them (O(n)).
    • The constant operations inside the loops do not significantly affect the complexity.
  2. Space:
    • It uses O(n) memory to store the x and y coordinates, which is optimal for the given conditions n≤100.
  3. Practical limits:
    • Even for the largest allowed input (n=100), the algorithm is fast and efficient, because it is a linear scan of the coordinates.
Note: This level of efficiency is an example of optimal design for problems with a small number of inputs, while for larger data (e.g. n≫1000) it would be worth exploring optimizations for parallel computing or more space-efficient methods.

Back: Arrays – examples