<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>RepeatFinding</title>
</head>

<body>



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Tracker</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>This is a small program which is used for my project. </b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <font FACE="TimesNewRomanPSMT">
  <p ALIGN="LEFT"><span lang="en-ca"><b>The problem is simple</b></span><b><span lang="en-ca">. 
  I have a series of data files which are the results of &quot;evolver&quot;. Evolver is </span></b></p>
  <p ALIGN="LEFT"><span lang="en-ca"><b>a soft package such that it can simulate 
  the intension movements of liquid. Now we want to know</b></span></p>
  <p ALIGN="LEFT"><span lang="en-ca"><b>how exactly a small chip moves which 
  floats on the top of liquid.</b></span></p>
  </font>
  <p ALIGN="LEFT">　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca">It is a small game and no one would bother to treat them seriously, I presume.</span></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left" style="width: 875; height: 2026">
  <pre><font size="3"><b><span lang="en-ca">1. main</span>.<span lang="en-ca">cpp(main)</span></b></font></pre>
  <pre><font size="3"><b><span lang="en-ca">2. tracker</span>.<span lang="en-ca">h</span></b></font></pre>
  <pre><font size="3"><b><span lang="en-ca">3. tracker</span>.<span lang="en-ca">cpp</span></b></font></pre>
  <pre>　</pre>
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: main</b></font></span><font size="3" color="#FF0000"><b>.<span lang="en-ca">cpp(main)</span></b></font></pre>
  <pre>#include &quot;tracker.h&quot;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;


int main()
{
	
	Tracker T;
	T.readFile();
	//T.display();
	T.tracking(-0.141400, -0.141400);
	T.displayTrack();

	return 0;
}</pre>
  <pre>　</pre>
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: tracker</b></font></span><font size="3" color="#FF0000"><b>.<span lang="en-ca">h</span></b></font></pre>
  <pre>//#include &lt;stdio.h&gt;
//#include &lt;stdlib.h&gt;

//#x0   x0/TW_x   y0   y0/TW_y     z0    phi     Fo   Fp       Fz     torque

const int RowCount=11;//this is the data row
const int ColCount=10;//this is the data col
const double StepX=0.014142;
const double StepY=0.014142;
const int Quadrant=4;
const int GridRow=11;//this means the number of row of grid
const int GridCol=11;//this means the number of col of grid
const int StartQuadrant=3;
const double StartX=-0.141421;
const double StartY=-0.141421;
const double PI=3.14159265358979323846;
const double TimeStep=0.001;//milisecond
const double Mass=0.4*0.4*2.8*0.01;//pad_width=0.4, thickness=0.01, density=2.8
const int MaxTrackStep=100;

struct Coord
{
	double x;
	double y;
};

struct GRID
{
	double x0;
	double y0;
	double fx;
	double fy;
	double torque;
};

class Tracker
{
private:
	double vx, vy, ax, ay;
	int t;
	double ABS(double num){return num&gt;=0?num:-num;}
	Coord tracks[MaxTrackStep];
	GRID grids[Quadrant][GridRow][GridCol];
	bool force(double x, double y, double&amp; f_x, double&amp; f_y);
public:
	void readFile(char* fileName=&quot;results&quot;);
	void tracking(double startx, double starty);
	void display();//for debug
	void displayTrack();
};



</pre>
  <pre>　</pre>
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: tracker</b></font></span><font size="3" color="#FF0000"><b>.<span lang="en-ca">cpp</span></b></font>
</pre>
  <pre></pre>
  <pre></pre>
  <pre></pre>
  <pre></pre>
  <pre>#include &lt;string.h&gt;
#include &lt;math.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;fstream.h&gt;
#include &quot;tracker.h&quot;

//#define ABS(x)  (x&gt;=0?x:-x)

void Tracker::readFile(char* fileName)
{
	const int EmptyLine=10;
	ifstream in;
	char buffer[512];
	int lines=0, quadrant;
	double x,y,fp,fo, tor, dump, theta;
	in.open(fileName);
	while (lines&lt;EmptyLine)
	{
		in.getline(buffer, 512);
		lines++;
	}
	while (!in.eof())
	{
		for (int q=0; q&lt;Quadrant; q++)
		{
			quadrant=(q+StartQuadrant)%Quadrant;
			for (int r=0; r&lt;GridRow; r++)
			{				
				for (int c=0; c&lt;GridCol; c++)
				{
					//Fo := Fx*cos(pi+theta) + Fy*sin(pi+theta);
					//Fp := Fy*cos(pi+theta) - Fx*sin(pi+theta);
					//there is problem of &quot;pi+theta&quot; as theta is already the 3rd quadrant
					//theta = atan2(yoff, xoff) ranging from -PI to PI
					in&gt;&gt;x&gt;&gt;dump&gt;&gt;y&gt;&gt;dump&gt;&gt;dump&gt;&gt;dump&gt;&gt;fo&gt;&gt;fp&gt;&gt;dump&gt;&gt;tor;
					theta=atan2(y, x);
					grids[quadrant][r][c].x0=x;
					grids[quadrant][r][c].y0=y;
					grids[quadrant][r][c].torque=tor;
					
					grids[quadrant][r][c].fx=fo*cos(PI+theta)-fp*sin(PI+theta);
					grids[quadrant][r][c].fy=fo*sin(PI+theta)+fp*cos(PI+theta);				
				}
			}
		
		}
		break;
	}
	/*
	while(!in.eof())
	{
		in.getline(buffer, 512);
	}
	*/
}

bool Tracker::force(double x, double y, double&amp; f_x, double&amp; f_y)
{
	int r=0,c=0, quadrant=0;
	double distanceX, distanceY, prop_x, prop_y, fx_hi, fx_lo, fy_hi, fy_lo;
	bool found=false;

	f_x=f_y=0;
	
	if (y&lt;0)
	{
		quadrant+=2;
	}
	if (x&lt;0)
	{
		quadrant+=1;
	}
	for(r=0; r&lt;GridRow-1; r++)
	{
		if (found)
		{
			break;
		}
		for (c=0; c&lt;GridCol-1; c++)
		{
			if (ABS(x) &lt; ABS(grids[quadrant][r+1][c].x0)
				&amp;&amp;ABS(x) &gt;= ABS(grids[quadrant][r][c].x0)
				&amp;&amp;ABS(y) &lt; ABS(grids[quadrant][r][c+1].y0)
				&amp;&amp;ABS(y) &gt;= ABS(grids[quadrant][r][c].y0))
			{
				found=true;
				//because (x,y) is in-between four points
				distanceX=ABS(grids[quadrant][r][c].x0-grids[quadrant][r+1][c].x0);
				distanceY=ABS(grids[quadrant][r][c].y0-grids[quadrant][r][c+1].y0);
				//proportion
				prop_x=ABS(x-grids[quadrant][r][c].x0);
				prop_x/=distanceX;
				prop_y=ABS(y-grids[quadrant][r][c].y0);
				prop_y/=distanceY;
				//for fx, it is prop_x
				fx_hi=grids[quadrant][r+1][c+1].fx*(1-prop_x)+
					grids[quadrant][r][c+1].fx*prop_x;
				fx_lo=grids[quadrant][r+1][c].fx*(1-prop_x)+
					grids[quadrant][r][c].fx*prop_x;
				//but the total depends on prop_y
				f_x =fx_hi*prop_y+fx_lo*(1-prop_y);

				fy_hi=grids[quadrant][r+1][c+1].fy*(1-prop_y)+
					grids[quadrant][r+1][c].fy*prop_y;
				fy_lo=grids[quadrant][r][c+1].fy*(1-prop_y)+
					grids[quadrant][r][c].fy*prop_y;
				//but the total depends on prop_y
				f_y =fy_hi*prop_x+fy_lo*(1-prop_x);

				break;
			}
		}
	}
	return found;
}
		
		
void Tracker::tracking(double startx, double starty)
{
	double fx, fy, x, y;
	vx=vy=ax=ay=0;
	x=startx;
	y=starty;
	t=0;
	tracks[t].x=x;
	tracks[t].y=y;
	do
	{
		if (force(tracks[t].x, tracks[t].y, fx, fy))
		{
			ax=fx/Mass;
			ay=fy/Mass;
			vx+=ax*TimeStep;
			vy+=ay*TimeStep;
			x+=ax*TimeStep*TimeStep/2+vx*TimeStep;
			y+=ay*TimeStep*TimeStep/2+vy*TimeStep;
			tracks[t+1].x=x;
			tracks[t+1].y=y;
		}
		else
		{
			break;
		}
		t++;
	}while (t&lt;MaxTrackStep-1);
}

void Tracker::displayTrack()
{
	for (int i=0; i&lt;t; i++)
	{
		printf(&quot;x=%f, y=%f\n&quot;, tracks[i].x, tracks[i].y);
	}
}




void Tracker::display()
{
	for (int q=0; q&lt;Quadrant; q++)
	{
		for (int r=0; r&lt;GridRow; r++)
		{
			for (int c=0; c&lt;GridCol; c++)
			{
				printf(&quot;x=%f,y=%f,fx=%f,fy=%f,torque=%f\n&quot;, grids[q][r][c].x0, 
					grids[q][r][c].y0, grids[q][r][c].fx, grids[q][r][c].fy, 
					grids[q][r][c].torque);
			}
			printf(&quot;\n\n&quot;);
		}
	}
}


/*
void Tracker::readFile(char* fileName)
{
	const int EmptyLine=10;
	FILE* stream;
	char buffer[512];
	int lines=0;
	double x,y,fp,fo, tor, dump;
	stream=fopen(fileName, &quot;r&quot;);
	while (!feof(stream))
	{
		//omit empty data line
		while (lines&lt;EmptyLine)
		{
			fgets(buffer, 512, stream);
			lines++;
		}
		//now start reading
		fgets(buffer, 512, stream);
		if (strlen(buffer)!=0)
		{
			//#x0  x0/TW_x   y0   y0/TW_y  z0   phi Fo  Fp   Fz  torque

		//	sscanf(buffer, &quot;%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n&quot;, x,dump, y,dump, dump,
		//		dump, fo, fp, dump, tor);
			sscanf(buffer, &quot;%f&quot;, x);

		}
	}
}
*/


</pre>
  <pre></pre>
</div>
<pre></pre>
<pre></pre>
<pre></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>The result is like following: </b></font></span></pre>

<p>x=-0.141400, y=-0.141400<br>
x=-0.129308, y=-0.129279<br>
x=-0.108501, y=-0.108465<br>
x=-0.078684, y=-0.078633<br>
x=-0.039354, y=-0.039295<br>
x=0.006118, y=0.006196<br>
x=0.044441, y=0.051592<br>
x=0.073316, y=0.100191<br>
Press any key to continue</p>

<pre>　</pre>

<pre>　</pre>

<pre><span lang="en-ca">				</span><a href="game24.htm"><img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp;</p>

</body>

</html>