// file: $USFS/class/image_analysis/v1.0/imaga_fract_07.cc
//

// isip include files
//
#include "image_analysis.h"

#include <integral.h>
#include <integral_constants.h>

// method fract_tri3_cc
//
// arguments: none
//
// return: a logical_1 value indicating status
//
// this method computes the sides of the traingle
//
logical_1 Image_analysis::fract_tri3_cc(int_4 x_dim, int_4 y_dim,
					int_4 width_a, int_4 height_a,
					float_4* b_red_a, float_4* b_green_a,
					float_4* b_blue_a, int_4 n) {
  
  // declare local variables
  //
  float_4 side_red_a = 0.0, side_red_b = 0.0, side_red_c = 0.0;
  float_4 side_grn_a = 0.0, side_grn_b = 0.0, side_grn_c = 0.0;
  float_4 side_blu_a = 0.0, side_blu_b = 0.0, side_blu_c = 0.0;
  float_4 len = 0.0, len_r = 0.0, len_g = 0.0, len_b = 0.0;
  float_4 dist_a = 0.0, dist_b = 0.0;
  float_4 dist_red = 0.0, dist_grn = 0.0, dist_blu = 0.0;
  
  //checking the boundary conditions
  //
  if (( (x_dim >= n) && (x_dim < (height_a - n))) &&
      ( (y_dim >= 0) && (y_dim < (width_a - n)))) {

    // computing the first side
    //
    dist_a = x_dim - (x_dim - n);
    dist_b = y_dim - (y_dim + n);
    dist_red = b_red_a[x_dim*width_a+y_dim] -
      b_red_a[(x_dim-n)*width_a+(y_dim+n)];
    dist_grn = b_green_a[x_dim*width_a+y_dim] -
      b_green_a[(x_dim-n)*width_a+(y_dim+n)];
    dist_blu = b_blue_a[x_dim*width_a+y_dim] -
      b_blue_a[(x_dim-n)*width_a+(y_dim+n)];

    // computing the length of the side
    //
    len = dist_a*dist_a + dist_b*dist_b;
    
    len_r = len + dist_red*dist_red;
    len_g = len + dist_grn*dist_grn;
    len_b = len + dist_blu*dist_blu;

    // checking for the zero condition
    //
    if (len_r >= 0) {
      side_red_a = sqrt(len_r);
    }

    if (len_g >= 0) {
      side_grn_a = sqrt(len_g);
    }
    
    if (len_b >= 0) {
      side_blu_a = sqrt(len_b);
    }

    // computing the second side
    //
    dist_a = x_dim - (x_dim + n);
    dist_red = b_red_a[x_dim*width_a+y_dim] -
      b_red_a[(x_dim+n)*width_a+(y_dim+n)];
    dist_grn = b_green_a[x_dim*width_a+y_dim] -
      b_green_a[(x_dim+n)*width_a+(y_dim+n)];
    dist_blu = b_blue_a[x_dim*width_a+y_dim] -
      b_blue_a[(x_dim+n)*width_a+(y_dim+n)];

    // computing the length of the second side
    //
    len = dist_a*dist_a + dist_b*dist_b;
    
    len_r = len + dist_red*dist_red;
    len_g = len + dist_grn*dist_grn;
    len_b = len + dist_blu*dist_blu;
    
    // checking for the zero condition
    //
    if (len_r >= 0) {
      side_red_b = sqrt(len_r);
    }

    if (len_g >= 0) {
      side_grn_b = sqrt(len_g);
    }
    
    if (len_b >= 0) {
      side_blu_b = sqrt(len_b);
    }

    // computing the third side
    //
    dist_a = (x_dim - n) - (x_dim + n);
    dist_red = b_red_a[(x_dim-n)*width_a+(y_dim+n)] -
      b_red_a[(x_dim+n)*width_a+(y_dim+n)];
    dist_grn = b_green_a[(x_dim-n)*width_a+(y_dim+n)] -
      b_green_a[(x_dim+n)*width_a+(y_dim+n)];
    dist_blu = b_blue_a[(x_dim-n)*width_a+(y_dim+n)] -
      b_blue_a[(x_dim+n)*width_a+(y_dim+n)];
    
    // computing the length of the second side
    //
    len = dist_a*dist_a;
    
    len_r = len + dist_red*dist_red;
    len_g = len + dist_grn*dist_grn;
    len_b = len + dist_blu*dist_blu;
    
    // checking for the zero condition
    //
    if (len_r >= 0) {
      side_red_c = sqrt(len_r);
    }

    if (len_g >= 0) {
      side_grn_c = sqrt(len_g);
    }
    
    if (len_b >= 0) {
      side_blu_c = sqrt(len_b);
    }

    if (fract_area_cc(side_red_a, side_red_b, side_red_c,
		      side_grn_a, side_grn_b, side_grn_c,
		      side_blu_a, side_blu_b, side_blu_c, n) == ISIP_FALSE) {
      error_handler_cc((char_1*)"fract_dist_cc",
		       (char_1*)"error computing area of the traingle");
      return ISIP_FALSE;
    }
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}




