Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- struct Point{
- double x, y;
- };
- double getDistance (struct Point a, struct Point b) {
- double distance;
- distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)) ;
- return distance;
- }
- int main () {
- struct Point list_p [5] = {
- {2.0, 3.5},
- {1.2, 4.5},
- {1.5, 6.1},
- {6.9, 7.4},
- {3.7, 8.2}
- };
- int n = sizeof(list_p)/sizeof(list_p[0]);
- double dist_p [n][n];
- int points [n][n];
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- points[i][j] = -1;
- }
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- if (i == j){
- dist_p[i][j] = 0;
- }else{
- dist_p[i][j] = getDistance(list_p[i], list_p[j]);
- }
- }
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- printf("%lf\t", dist_p[i][j]);
- }
- printf("\n");
- }
- int k, aux, pivot;
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- k = 0;
- if (i == j){
- points[i][j] = j;
- aux = j;
- }else{
- if(points[i][k] != -1){
- pivot = j;
- while (k < n){
- if (points[i][k] != -1){
- if (dist_p[i][pivot] > dist_p[i][points[i][k]]){
- aux = pivot;
- pivot = points[i][k];
- points[i][k] = aux;
- k++;
- }
- else{
- k++;
- }
- }else{
- points[i][k] = pivot;
- break;
- }
- }
- } else{
- points[i][k] = j;
- }
- }
- }
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- printf("%d\t", points[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement