geometry_8c-source.html

来自「仿真人工智能是指用人工的方法和技术」· HTML 代码 · 共 754 行 · 第 1/5 页

HTML
754
字号
01121 {01122     <font class="keywordtype">double</font> x0, y0, r0;01123     <font class="keywordtype">double</font> x1, y1, r1;01124 01125     x0 = <a class="code" href="classCircle.html#a7">getCenter</a>( ).<a class="code" href="classVecPosition.html#a26">getX</a>();01126     y0 = <a class="code" href="classCircle.html#a7">getCenter</a>( ).<a class="code" href="classVecPosition.html#a28">getY</a>();01127     r0 = <a class="code" href="classCircle.html#a5">getRadius</a>( );01128     x1 = c.<a class="code" href="classCircle.html#a7">getCenter</a>( ).<a class="code" href="classVecPosition.html#a26">getX</a>();01129     y1 = c.<a class="code" href="classCircle.html#a7">getCenter</a>( ).<a class="code" href="classVecPosition.html#a28">getY</a>();01130     r1 = c.<a class="code" href="classCircle.html#a5">getRadius</a>( );01131 01132     <font class="keywordtype">double</font>      d, dx, dy, h, a, x, y, p2_x, p2_y;01133 01134     <font class="comment">// first calculate distance between two centers circles P0 and P1.</font>01135     dx = x1 - x0;01136     dy = y1 - y0;01137     d = sqrt(dx*dx + dy*dy);01138 01139     <font class="comment">// normalize differences</font>01140     dx /= d; dy /= d;01141 01142     <font class="comment">// a is distance between p0 and point that is the intersection point P2</font>01143     <font class="comment">// that intersects P0-P1 and the line that crosses the two intersection</font>01144     <font class="comment">// points P3 and P4.</font>01145     <font class="comment">// Define two triangles: P0,P2,P3 and P1,P2,P3.</font>01146     <font class="comment">// with distances a, h, r0 and b, h, r1 with d = a + b</font>01147     <font class="comment">// We know a^2 + h^2 = r0^2 and b^2 + h^2 = r1^2 which then gives</font>01148     <font class="comment">// a^2 + r1^2 - b^2 = r0^2 with d = a + b ==&gt; a^2 + r1^2 - (d-a)^2 = r0^2</font>01149     <font class="comment">// ==&gt; r0^2 + d^2 - r1^2 / 2*d</font>01150     a = (r0*r0 + d*d - r1*r1) / (2.0 * d);01151 01152     <font class="comment">// h is then a^2 + h^2 = r0^2 ==&gt; h = sqrt( r0^2 - a^2 )</font>01153     <font class="keywordtype">double</font>      arg = r0*r0 - a*a;01154     h = (arg &gt; 0.0) ? sqrt(arg) : 0.0;01155 01156     <font class="comment">// First calculate P2</font>01157     p2_x = x0 + a * dx;01158     p2_y = y0 + a * dy;01159 01160     <font class="comment">// and finally the two intersection points</font>01161     x =  p2_x - h * dy;01162     y =  p2_y + h * dx;01163     p1-&gt;<a class="code" href="classVecPosition.html#a29">setVecPosition</a>( x, y );01164     x =  p2_x + h * dy;01165     y =  p2_y - h * dx;01166     p2-&gt;<a class="code" href="classVecPosition.html#a29">setVecPosition</a>( x, y );01167 01168     <font class="keywordflow">return</font> (arg &lt; 0.0) ? 0 : ((arg == 0.0 ) ? 1 :  2);01169 }01170 <a name="l01174"></a><a class="code" href="classCircle.html#a12">01174</a> <font class="keywordtype">double</font> <a class="code" href="classCircle.html#a12">Circle::getIntersectionArea</a>( <a class="code" href="classCircle.html">Circle</a> c )01175 {01176   <a class="code" href="classVecPosition.html">VecPosition</a> pos1, pos2, pos3;01177   <font class="keywordtype">double</font> d, h, dArea;01178   <a class="code" href="Geometry_8h.html#a2">AngDeg</a> ang;01179 01180   d = <a class="code" href="classCircle.html#a7">getCenter</a>().<a class="code" href="classVecPosition.html#a30">getDistanceTo</a>( c.<a class="code" href="classCircle.html#a7">getCenter</a>() ); <font class="comment">// dist between two centers</font>01181   <font class="keywordflow">if</font>( d &gt; c.<a class="code" href="classCircle.html#a5">getRadius</a>() + <a class="code" href="classCircle.html#a5">getRadius</a>() )           <font class="comment">// larger than sum radii</font>01182     <font class="keywordflow">return</font> 0.0;                                   <font class="comment">// circles do not intersect</font>01183   <font class="keywordflow">if</font>( d &lt;= fabs(c.<a class="code" href="classCircle.html#a5">getRadius</a>() - <a class="code" href="classCircle.html#a5">getRadius</a>() ) )   <font class="comment">// one totally in the other</font>01184   {01185     <font class="keywordtype">double</font> dR = <a class="code" href="Geometry_8h.html#a2">min</a>( c.<a class="code" href="classCircle.html#a5">getRadius</a>(), <a class="code" href="classCircle.html#a5">getRadius</a>() );<font class="comment">// return area smallest circle</font>01186     <font class="keywordflow">return</font> M_PI*dR*dR;01187   }01188 01189   <font class="keywordtype">int</font> iNrSol = <a class="code" href="classCircle.html#a11">getIntersectionPoints</a>( c, &amp;pos1, &amp;pos2 );01190   <font class="keywordflow">if</font>( iNrSol != 2 )01191     <font class="keywordflow">return</font> 0.0;01192 01193   <font class="comment">// the intersection area of two circles can be divided into two segments:</font>01194   <font class="comment">// left and right of the line between the two intersection points p1 and p2.</font>01195   <font class="comment">// The outside area of each segment can be calculated by taking the part</font>01196   <font class="comment">// of the circle pie excluding the triangle from the center to the</font>01197   <font class="comment">// two intersection points.</font>01198   <font class="comment">// The pie equals pi*r^2 * rad(2*ang) / 2*pi = 0.5*rad(2*ang)*r^2 with ang</font>01199   <font class="comment">// the angle between the center c of the circle and one of the two</font>01200   <font class="comment">// intersection points. Thus the angle between c and p1 and c and p3 where</font>01201   <font class="comment">// p3 is the point that lies halfway between p1 and p2.</font>01202   <font class="comment">// This can be calculated using ang = asin( d / r ) with d the distance</font>01203   <font class="comment">// between p1 and p3 and r the radius of the circle.</font>01204   <font class="comment">// The area of the triangle is 2*0.5*h*d.</font>01205 01206   pos3 = pos1.<a class="code" href="classVecPosition.html#a50">getVecPositionOnLineFraction</a>( pos2, 0.5 );01207   d = pos1.<a class="code" href="classVecPosition.html#a30">getDistanceTo</a>( pos3 );01208   h = pos3.<a class="code" href="classVecPosition.html#a30">getDistanceTo</a>( <a class="code" href="classCircle.html#a7">getCenter</a>() );01209   ang = asin( d / <a class="code" href="classCircle.html#a5">getRadius</a>() );01210 01211   dArea = ang*<a class="code" href="classCircle.html#a5">getRadius</a>()*<a class="code" href="classCircle.html#a5">getRadius</a>();01212   dArea = dArea - d*h;01213 01214   <font class="comment">// and now for the other segment the same story</font>01215   h = pos3.<a class="code" href="classVecPosition.html#a30">getDistanceTo</a>( c.<a class="code" href="classCircle.html#a7">getCenter</a>() );01216   ang = asin( d / c.<a class="code" href="classCircle.html#a5">getRadius</a>() );01217   dArea = dArea + ang*c.<a class="code" href="classCircle.html#a5">getRadius</a>()*c.<a class="code" href="classCircle.html#a5">getRadius</a>();01218   dArea = dArea - d*h;01219 01220   <font class="keywordflow">return</font> dArea;01221 }01222 01223 01224 <font class="comment">/******************************************************************************/</font>01225 <font class="comment">/***********************  CLASS LINE *******************************************/</font>01226 <font class="comment">/******************************************************************************/</font>01227 <a name="l01233"></a><a class="code" href="classLine.html#a0">01233</a> <a class="code" href="classLine.html#a0">Line::Line</a>( <font class="keywordtype">double</font> dA, <font class="keywordtype">double</font> dB, <font class="keywordtype">double</font> dC )01234 {01235   <a class="code" href="classLine.html#o0">m_a</a> = dA;01236   <a class="code" href="classLine.html#o1">m_b</a> = dB;01237   <a class="code" href="classLine.html#o2">m_c</a> = dC;01238 }01239 <a name="l01245"></a><a class="code" href="Geometry_8C.html#a15">01245</a> ostream&amp; <a class="code" href="SoccerTypes_8C.html#a1">operator &lt;&lt;</a>(ostream &amp; os, <a class="code" href="classLine.html">Line</a> l)01246 {01247   <font class="keywordtype">double</font> a = l.<a class="code" href="classLine.html#a10">getACoefficient</a>();01248   <font class="keywordtype">double</font> b = l.<a class="code" href="classLine.html#a11">getBCoefficient</a>();01249   <font class="keywordtype">double</font> c = l.<a class="code" href="classLine.html#a12">getCCoefficient</a>();01250 01251   <font class="comment">// ay + bx + c = 0 -&gt; y = -b/a x - c/a</font>01252   <font class="keywordflow">if</font>( a == 0 )01253     os &lt;&lt; <font class="stringliteral">"x = "</font> &lt;&lt; -c/b;01254   <font class="keywordflow">else</font>01255   {01256     os &lt;&lt; <font class="stringliteral">"y = "</font>;01257     <font class="keywordflow">if</font>( b != 0 )01258       os &lt;&lt; -b/a &lt;&lt; <font class="stringliteral">"x "</font>;01259     <font class="keywordflow">if</font>( c &gt; 0 )01260        os &lt;&lt; <font class="stringliteral">"- "</font> &lt;&lt;  fabs(c/a);01261     <font class="keywordflow">else</font> <font class="keywordflow">if</font>( c &lt; 0 )01262        os &lt;&lt; <font class="stringliteral">"+ "</font> &lt;&lt;  fabs(c/a);01263   }01264   <font class="keywordflow">return</font> os;01265 }01266 <a name="l01269"></a><a class="code" href="classLine.html#a1">01269</a> <font class="keywordtype">void</font> <a class="code" href="classLine.html#a1">Line::show</a>( ostream&amp; os)01270 {01271   os &lt;&lt; *<font class="keyword">this</font>;01272 }01273 <a name="l01278"></a><a class="code" href="classLine.html#a2">01278</a> <a class="code" href="classVecPosition.html">VecPosition</a> <a class="code" href="classLine.html#a2">Line::getIntersection</a>( <a class="code" href="classLine.html">Line</a> line )01279 {01280   <a class="code" href="classVecPosition.html">VecPosition</a> pos;01281   <font class="keywordtype">double</font> x, y;01282 01283   <font class="keywordflow">if</font>( <a class="code" href="classLine.html#o1">m_b</a> == line.<a class="code" href="classLine.html#a11">getBCoefficient</a>() ) <font class="comment">// lines are parallel, no intersection</font>01284   {01285     <font class="keywordflow">return</font> pos;01286   }01287   <font class="keywordflow">if</font>( <a class="code" href="classLine.html#o0">m_a</a> == 0 )               <font class="comment">// bx + c = 0 and a2*y + b2*x + c2 = 0 ==&gt; x = -c/b</font>01288   {                          <font class="comment">// calculate x using the current line</font>01289    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?