#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define RED "\033[1;31m"
#define GREEN "\033[1;32m"
#define YELLOW "\033[1;33m"
#define BLUE "\033[1;34m"
#define PURPLE "\033[1;35m"
#define CYAN "\033[1;36m"
#define RESET "\033[0m"
// Variáveis globais (artifício básico, para facilitar...)
int ENTRADA = -1;
float menor_distancia=10000000000000000;
float maior_distancia=-1;
int flag = 0;
// FIM - Variáveis globais
long int fatorial(long int n) {
if (n==0)
return 1;
else
return (n * fatorial(n-1));
}
float distancia_2_pontos(float *p1_x, float *p1_y, float *p1_z, float *p2_x, float *p2_y, float *p2_z) {
//printf("\nP1: %f - %f - %f <> P2: %f - %f = %f\n", *p1_x, *p1_y, *p1_z, *p2_x, *p2_y, *p2_z);
float dist
= sqrt(((*p2_x
- *p1_x
)*(*p2_x
- *p1_x
)) + ((*p2_y
- *p1_y
)*(*p2_y
- *p1_y
)) + (*p2_z
- *p1_z
)*(*p2_z
- *p1_z
)); return dist;
}
void identificacao_comparacoes(int r, int n, int k, float *px, float *py, float *pz, float *pdist) {
int i;
float dAB;
for(i=r; i<n; i++) {
printf("\n<%d>-<%d> (%d)", r
, i
+1, k
); dAB = distancia_2_pontos(px+(r-1), py+(r-1), pz+(r-1), px+i, py+i, pz+i);
printf("\nDistância %d-%d: %4.5f\n", r
, i
+1, dAB
); if(ENTRADA == -1) {
ENTRADA = 1;
menor_distancia = dAB;
maior_distancia = dAB;
}
else {
if(dAB < menor_distancia) {
menor_distancia = dAB;
}
else {
if(dAB > maior_distancia) {
maior_distancia = dAB;
//printf("\n>> OK\n");
}
}
}
k = k+1;
}
if(r<(n-1)) {
identificacao_comparacoes(r+1, n, k, px, py, pz, pdist);
}
else {
printf("\nTotal de comparações: "); }
}
int main(void) {
long int n;
int i, j, x;
float *p_x, *p_y, *p_z, *p_dist;
float dAB;
printf("Digite o valor de n >= 2 (total de pontos 3D):\n");
printf("\nAnálise Combinatória => ((n!)/(2!(n-2)!)");
// Análise combinatória facilita... para saber o total de comparações (x).
x = fatorial(n)/(fatorial(2)*fatorial(n-2));
p_x
= malloc(sizeof(float) * n
); p_y
= malloc(sizeof(float) * n
); p_z
= malloc(sizeof(float) * n
); p_dist
= malloc(sizeof(float) * x
);
for(i=0; i<n; i++)
*(p_dist+i) = -1; // -1 significa sem valor calculado... ainda.
printf("\nDigite as coordenadas de cada ponto!\n"); printf("Coordenadas geradas aleatoriamente...\n");
for(i=0; i<n; i++) {
//scanf("%f", p_x+i); // &(p_x[i])
*(p_x
+i
) = rand()%1000/100.0;
//scanf("%f", p_y+i); // &(p_y[i])
*(p_y
+i
) = rand()%1000/100.0;
//scanf("%f", p_z+i); // &(p_z[i])
*(p_z
+i
) = rand()%1000/100.0; }
if (flag == 1) {
printf("\nApresentação (dados):\n"); for(i=0; i<n; i++) {
printf("End. Px[%d]: %p / Valor Px[%d]: %f\n", i
+1, p_x
+i
, i
+1, p_x
[i
]); // *(p_x+i) printf("End. Py[%d]: %p / Valor Py[%d]: %f\n", i
+1, p_y
+i
, i
+1, p_y
[i
]); // *(p_y+i) printf("End. Pz[%d]: %p / Valor Pz[%d]: %f\n", i
+1, p_z
+i
, i
+1, p_z
[i
]); // *(p_z+i) }
}
identificacao_comparacoes(1, n, 1, p_x+0, p_y+0, p_z+0, p_dist+0);
printf("\nMenor distância: "); printf("%5f", menor_distancia
); printf("\nMaior distância: "); printf("%5f", maior_distancia
); printf(". -> Cálculo final!\n");
printf(" Memória Liberada!!!\n"); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2RlZmluZSBSRUQgICAgICJcMDMzWzE7MzFtIgojZGVmaW5lIEdSRUVOICAgIlwwMzNbMTszMm0iCiNkZWZpbmUgWUVMTE9XICAiXDAzM1sxOzMzbSIKI2RlZmluZSBCTFVFICAgICJcMDMzWzE7MzRtIgojZGVmaW5lIFBVUlBMRSAgIlwwMzNbMTszNW0iCiNkZWZ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