S
S
Silence2017-05-09 09:39:57
C++ / C#
Silence, 2017-05-09 09:39:57

Search for groups of similar triangles among an array of triangles by sides. How to implement?

SEARCH FOR GROUPS OF SIMILAR TRIANGLES.
It is necessary that in the end it turns out for example: Group 1- 1,2,3. (Triangles). Group 2-4.5.
I tried to write, but it turns out complete nonsense.
How to implement?

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct
{
 double n;
 double m;
 double o;
 double s;
}Triangle;
int array (int n)
{
        while(1) 
        { 
                 printf("Enter array size: \n "); 
                 scanf("%d",&n); 
                 fflush(stdin); 
                 if (n>1 && n<=20)
                 break; 
                 printf("Error \n"); 
        } 
        printf("\n"); 
        return n; 
} 

void entercoord(Triangle *p)
{  
     printf("Enter first line range: \n");
     while (1)
    {
           fflush(stdin);
           if(scanf("%lg", &p->n)==1)
             break;
           printf("Eror! Enter 'n' again:"); 
     } 
   printf("Enter second line range: \n");
     while (1)
    {
           fflush(stdin);
           if(scanf("%lg", &p->m)==1)
             break;
           printf("Eror! Enter 'm' again:"); 
     } 
     printf("Enter third line range: \n");
     while (1)
    {
           fflush(stdin);
           if(scanf("%lg", &p->o)==1)
             break;
           printf("Eror! Enter 'o' again:"); 
     } 
}
void enter(Triangle z[],int n)
{
     int i;
     for(i=0;i<n;i++)
     {
     printf("Enter lines for triangle [%d]\n",i+1);
     entercoord(&z[i]);
     }
}

void SumArea(Triangle z[20],int n)
{
       int j;
       for (j=0;j<n;j++)
       {
         z[j].s=sqrt (((z[j].n+z[j].m+z[j].o)/2)*(((z[j].n+z[j].m+z[j].o)/2)- z[j].n)*(((z[j].n+z[j].m+z[j].o)/2)-z[j].m)*(((z[j].n+z[j].m+z[j].o)/2)-z[j].o));
       }
}

void CompareTriangle( Triangle z[20],int n)
{
     int i,j;
     for(i = 0 ; i <n ; i++) 
     { 
       for(j = 0 ; j <n - i ; j++) 
       {  
           if(z[j].s < z[j+1].s) 
           {           
              Triangle tmp = z[j];
              z[j] = z[j+1];
              z[j+1] = tmp; 
           }
        }
    } 
}
void Output( Triangle z[20],int n)
{
     int i;
     for (i=0;i<n;i++)     
     {    
     printf("Line size of Triangle z[%d] : %lg,%lg,%lg \n",i+1,z[i].n,z[i].m,z[i].o);   
     printf("Triangle area z[%d]: %lg%\n",i+1,z[i].s);
     }
}


int main()
{
    int n;
    Triangle z[20];
    n=array(n);
    enter(z,n);
    SumArea(z,n);
    printf("Before sort:\n");
    Output(z,n);
    printf("Triangle compare:\n");
    CompareTriangle(z,n);
    printf("Sortirovka po ploshadeam:\n");
    Output(z,n);
    
    
    system("pause");
    return (0);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2017-05-09
@Adrikk

To begin with, to make it easier to search, it is better to keep the sides of the triangles in ascending order, that is, z[j].n <= z[j].m <= z[j].o
Then, we recall the rule for the similarity of triangles, taking into account the sorting of the lengths of the sides:
Let's rewrite these conditions for the computer:

(z[i].n / z[j].n) = (z[i].m / z[j].m) => ((z[i].n * z[j].m) / (z[i].m * z[j].n)) = 1
(z[i].n / z[j].n) = (z[i].o / z[j].o) => ((z[i].n * z[j].o) / (z[i].o * z[j].n)) = 1

Since real calculations on a computer give an error, the conditions must be converted (epsilon is a constant that specifies the accuracy of calculations):
abs(((z[i].n * z[j].m) / (z[i].m * z[j].n)) - 1) < epsilon && 
abs((z[i].n * z[j].o) / (z[i].o * z[j].n)) - 1) < epsilon

Now, starting from the first triangle, we assign it to class 1, check it for similarity to the rest and, where the triangles are similar, write them to class 1.
Then we look for a triangle for which the class is not set, assign it to the next class, check its similarity to the rest of the classless triangles , for similar ones we write the same class.
We continue until all triangles are assigned to one of the classes.

A
Alexander, 2017-05-09
@NeiroNx

Compare one with the rest. To find out similar triangles or not - you need to sort the lengths of the sides in descending order and compare their ratio (the ratio of each pair of sides) - it must be the same or in a certain interval (since there is a calculation error). Add indexes of found to groups. Well, how to mark already processed triangles in the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question