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:
- A detailed explanation of the task and the solution procedure.
- Practical examples of input and output that illustrate specific cases.
- An analysis of the algorithm's efficiency and useful implementation tips.
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:
- One point:
3.0 4.0
Output0.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:
- 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.
- int n; scanf("%d", &n);
- Storing the coordinates of the points:
- Two arrays (x and y) are used to store the x and y coordinates of all the points.
- 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.
- 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
- 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
- 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:- Entering the number of points:
- First, the program reads the number of points n to be processed.
- 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.
- 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.
- 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.
- 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:
- Data input:
- The program reads the number of points n, and then their coordinates (x, y), and stores them in data structures.
- 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
- 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
- Formatted output:
- Formatting to two decimal places is used so that the output is clear and precise.
- 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.
- Space:
- It uses O(n) memory to store the x and y coordinates, which is optimal for the given conditions n≤100.
- 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.
Back: Arrays – examples
#include <iostream>
#include<stdio.h>
using namespace std;
/*
Date su koordinate N tačaka u ravni. Translirati tačke tako da im težište bude u koordinatnom početku.
Ulaz
U prvoj liniji standardnog ulaza nalazi se prirodan broj n (1≤n≤100). U sledećih n
linija nalaze se po dva realna broja, koji predstavljaju x i y koordinate tačaka.
Izlaz
Na standardnom izlazu prikazati koordinate tačaka posle translacije, za svaku tačku u jednoj liniji njenu
x pa y koordinatu, koordinate odvojiti jednom prazninom i prikazati ih na dve decimale.
Primer
Ulaz
3
0 0
1 0
2 3
Izlaz
-1.00 -1.00
0.00 -1.00
1.00 2.00
*/
int main()
{
int n;
cin>>n;
double x[100],y[100];
for(int i=0;i<n;i++){
cin>>x[i]>>y[i];
}
double Tx,Ty;
double zbX=0,zbY=0;
for(int i=0;i<n;i++){
zbX=zbX+x[i];
zbY=zbY+y[i];
}
Tx=zbX/n;
Ty=zbY/n;
for(int i=0;i<n;i++){
printf("%.2f %.2f\n",(x[i]-Tx),(y[i]-Ty));
}
return 0;
}
© 2019 by Slobodan izprogramiranja.weebly.com
All rights reserved
