/* 
 * Thomas Pantzer: t_deq_c.cc, v0.4.7  8.6.99 16:32 
 * 
 * 
 * Copyright 1999 by Thomas Pantzer
 * 
 * Permission to use, copy, modify, and distribute this software for noncommercial 
 * use and without fee is hereby granted, provided that the above copyright notice 
 * appear in all copies and that both that copyright notice and this permission 
 * notice appear in supporting documentation, and that the name of the Author not be 
 * used in advertising or publicity pertaining to distribution of the software 
 * without specific, written prior permission. The Author makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is" 
 * without expressed or implied warranty. 
 * 
 * 
 * pantec@isys2.informatik.uni-leipzig.de   (Thomas Pantzer) 
 * 
 */


#include <stdio.h>
#include <math.h>

#include "copyright.h"

#include "t_deq_c.h"
#include "trigonom.h"
#include "graphix.h"



inline double v_len(double dx, double dy)
{
	double d = SQR(dx) + SQR(dy);
        double r = sqrt(d);
        return r;  
}

#define R param[0]
#define dxo param[1]
#define dyo param[2]


void t_deq_c::Init(int i, double a, t_deq *N)
{
   t_deq::Init(i,a,N);

   type = e_circle;	

   R = a;
}


double max_err = 0.0;
double min_err = 2.0;


#define KreisAbstand 1.5

double t_deq_c::Error(t_vector &Df, double ds, double da_avg, double ds_avg)
{
/*	Ralfs Fehlerfunktion
 */

//        double r = R * da_avg;
//        r -= ds_avg;
//        err = fabs(SQR(r));
//        err *= (1.0/10.0);   // 5.25

	double fe = (R*da_avg) - ds_avg;
	double s2 = SQR(KreisAbstand);
	
	err = 1.0;
        err -= (s2 / (SQR(fe) + s2));

        W += ((err - (2.0*W)) * (1.0/Tw));

        if (fe < min_err)
	   {
            min_err = fe;
//	    fprintf(stderr,"min_err: %g\n",min_err);
	   }	

        if (max_err < fe)
	   {
            max_err = fe;
//	    fprintf(stderr,"max_err: %g\n",max_err);
	   }	

 /*
	if (err > 1.0)
	   err = 1.0;
 */
	return err;
}



void t_deq_c::Update(double factor, t_vector &Df, double ds, double da_avg, double ds_avg)
{
//    double dR = (R*da_avg);
//    dR -= ds_avg;
//    dR *= da_avg; 


    double fe = (R*da_avg) - ds_avg;
    double s2 = SQR(KreisAbstand);

    double dR = s2*2.0*fe / SQR(SQR(fe) + s2);
 	
    dR *= factor * 200;  // * 10000;

    R += dR;	

//    fprintf(stderr,"radius %g, UF: %g\n",ARG,UF);
}




void t_deq_c::Show(t_vertex_2i *vcenter, double size_, t_deq *BP, t_deq *BF, t_deq *SE)
{
	SetColor(FL_RED);

	char st[256];
//	sprintf(st,"radius: %g",ARG);
//	SetColor(FL_CYAN);
	sprintf(st,"%d. p:%1.3f; e:%1.3f; f:%1.3f; r:%g",id,prob,err,fit,R);
	SetColor(FL_CYAN);
//	OutTextXY((int)EP.x,(int)EP.y,st);
	OutTextXY((int)(vcenter->y-(R/10.0)),(int)(vcenter->y-(R/10.0)),st);

        if (this == BF)
	   {
	    SetColor(FL_DOGERBLUE);
	    FillCircle((int)(vcenter->y-(R/10.0)),(int)(vcenter->y-(R/10.0)),7);
           }

        if (this == BP)
	    SetColor(FL_YELLOW);

	DrawCircle((int)(vcenter->y-(R/10.0)),(int)(vcenter->y-(R/10.0)),(int)fabs(R));
}















/**/
/**/
/**/
/**/
/**/
/**/
/**/
/**/
/*****  End Of File  ****************************/
