|
suny00059X and Y exhaustion may result in orders of magnitude of calculation time, I thought of a method of exhaustive X or Y only, the time complexity is reduced by an order of magnitude, see if it works. The preliminary test results seem to be fine.
#include <math.h>
#define min(a,b) (((a) <(b))? (a): (b))
double Zaxby(int Xmin, int Xmax, int Ymin, int Ymax, double a, double b)
{
int i;
double c, Zmin, dt;
The
if(b == 0) return min(fabs(a * Xmin), fabs(a * Xmax));
if(a == 0) return min(fabs(b * Ymin), fabs(b * Ymax));
The
c = a / b;
Zmin = fabs(c * Xmax-Ymax);
for(i = Xmin; i <= Xmax; i++){
dt = c * i;
if(dt> Ymin&&dt <Ymax){
dt = fabs(dt-(int)(dt));
if(dt> 0.5) dt = 1.0-dt;
}
else if(dt <= Ymin) dt = Ymin-dt;
else dt = dt-Ymax;
The
if(dt <Zmin) Zmin = dt;
}
return fabs(Zmin * b);
}
main()
{
double a = -1.3, b = 2.2;
int Xmin = 1, Xmax = 5, Ymin = 2, Ymax = 8;
double data;
data = Zaxby(Xmin, Xmax, Ymin, Ymax, a, b);
} |
|