<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd" [ <!ENTITY mathml "http://www.w3.org/1998/Math/MathML"> ]>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>C++ Arcs made from Control Points</title>
<meta http-equiv="Content-Type" content="text/xml; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="stylesheet" type="text/css" href="../documentation/s011.css" />
<link rel='SHORTCUT ICON' href='../documentation/images/FrameHome.ico' />
</head>

<body>

<div class="message_right">
  <a href="http://validator.w3.org/check/referer">
    <img src="http://www.w3.org/Icons/valid-xhtml11" alt="Valid
	XHTML 1.1!" height="31" width="88" />
  </a>
  <a href="http://jigsaw.w3.org/css-validator/">
    <img style="width:88px;height:31px"
       src="http://jigsaw.w3.org/css-validator/images/vcss" 
       alt="Valid CSS!" />
  </a>
  <br />
  Created 2006-11-06 Modified 
<!--UPDATE_DATE_BEGIN-->
2009-04-11<br />
<a class="reflocal" href="../../../../doc02.html">Chelton Evans</a>

<!--UPDATE_DATE_END-->
</div>



<h1> 
<a href="../doc.html">
<img alt="proj" src="../../../comsci/images/code.png" /> </a>
arcs
<a href="../../../../index.html">
<img alt="home" src="../../../comsci/images/Frame.gif" /> </a>
</h1>

<p>
<a class="reflocal2" href="#Intro">Intro</a><br />
<a class="reflocal2" href="#Solution_Strategies">
  Solution Strategies</a><br />
<a class="reflocal2" href="#Source">Source</a><br />
<a class="reflocal2" href="#Model_Connected_Arcs_in_Cartesian_Coordinates">
  Model Connected Arcs in Cartesian Coordinates</a><br />
<a class="reflocal2" href="#Continuity_Condition">
  Continuity Condition</a><br />
<a class="reflocal2" href="#Constructing_an_Arc_through_Two_Points_given_an_Angle">
  Constructing an Arc through Two Points given an Angle </a><br />
  &nbsp; &nbsp; 
<a class="reflocal2" href="#Distance_from_a_Point_to_the_Arc">
  Distance from a Point to the Arc</a><br />
<a class="reflocal2" href="#Handeling_a_large_number_of_Arcs">
  Handling a large number of Arcs</a><br />
<a class="reflocal2" href="#Minimization_Algorithm">
  Minimization Algorithm</a><br />
<a class="reflocal2" href="#Arc_Formats">Arc Formats</a><br />
  &nbsp; &nbsp; 
  <a class="reflocal2" href="#Arc_Drawing_Format">Arc Drawing Format</a><br />
  &nbsp; &nbsp; 
  <a class="reflocal2" href="#Arc_Solver_Format">Arc Solver Format</a><br />
<a class="reflocal2" href="#TODO">TODO</a><br />
<a class="reflocal2" href="#Algorithm_Complexity">
  Algorithm Complexity</a><br />
<a class="reflocal2" href="#Handeling_a_large_number_of_Arcs">
 Handeling a large number of Arcs</a>. <br />

<a class="reflocal2" href="#Line_Segments">Line Segments</a><br />


<a class="reflocal2" href="#Algorithm1">
Algorithm1</a><br />



<a class="reflocal2" href="#References">References</a>
</p>


<div class="float25">
<a id="Intro"></a>
<h2>Intro</h2>

<p>
Status: incomplete. To be implemented, see
<a class="reflocal2" href="#Algorithm1">
 Algorithm1</a>.
</p>

<p>The problem is to have N connected arcs approximated or best fitted
 to W control points where W is larger than N.
</p>

<p>
The arcs are restricted to being convex and first derivative continuous at joins
 with other arcs.
</p>

<p>This is a minimization problem. </p>

<p>Thanks to Mike, Serge and David of 
<a href="http://www.arbsys.com/"> Arbiter Modeling Systems Pty Ltd
</a> for the problem.  
</p>

</div>

<div class="float25">
<a id="Solution_Strategies"></a>
<h2>Solution Strategies</h2>

<p>
A related problem is minimizing  
<a class="reflocal2" href="#Line_Segments">
 Line Segments</a>.
</p>



<p>
Make the problem easier.
 Let N<sub>2</sub> = W-1.
 So a path is created through
 the control points. 
 Then start adding control points.
</p>


<h3>Problem</h3>
<p>The problem simplifies with the use of convex arcs.</p>

<p>An initial condition must first be solved for.</p>

<p>
<a class="reflocal2" href="#Model_Connected_Arcs_in_Cartesian_Coordinates">
  Model Connected Arcs in Cartesian Coordinates</a>.
</p> 

<h3>Incremental Algorithm</h3>

<p>
Find initial solution of N arcs with
 N+1 control points.
</p>

<p>
Add remaining control points q<sub>i</sub> 
 incrementally. No idea how to implement 
 this. Mesh of arcs, log(N) to find 
 associated arc. One
 minimization on each insertion. Complicated
 and O(N^2) minimum with f as O(N).
</p>

</div>
<div class="float25">

<a id="Continuity_Condition"></a>
<h2>Continuity Condition</h2>

<p>
Having two arcs connected together then their first derivatives should be
equal. Geometrically we can say that the next arcs initial angle is equal
 to the previous arcs second angle.
</p>

<p>
Alternatively the new arcs center is on the same line as the 
 previous arcs radius. 
</p>

<p> A straight line is interpreted as an arc with infinite radius.
  If C<sup>1</sup> is needed a component could be added to the 
 minimizer to exclude straight lines by making them more costly.
 Inflection points are where the curve winding changes
 and can not be avoided.
</p>

<p>The consequence of the continuity condition is that it connects
 the arcs so O(f)=N. ie when one arc is updated the subsequent arcs
 in the chain need to be changed.  For large N this is a problem.
</p>

<p>The following can be used to connect two arcs at their boundaries.
</p>



<p>
Let the previous arc go through the points 
<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
</math>
 and 
<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
</math>. <br />
 Let the new arc be constructed through the points 

<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
</math>
 and 
<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msub>
</math>. <br />
If 
<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msub>
</math> is below the circles tangent at point
 <math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
</math>

 then let the previous end angle of the last arc
 be the starting angle of the next arc, else add
 180 degrees to the previous arc to give the starting
 angle of the new arc.
</p>

</div>

<div class="float25">

<img src="img04.png" alt="img04.png" />

<p>Expressing this using the halfspace
 test for the tangent condition.
</p>

<p class="equ">
if 
<math xmlns="&mathml;">
  <mo>(</mo>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>c</mi>
  <mo>)</mo>
  <mo>&CenterDot;</mo>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <mo>&gt;</mo>
  <mi>0</mi>
</math>




<br />

 &nbsp; &nbsp; 


<math xmlns="&mathml;">
  <mi>arc</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mi>.&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>arc</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>
  <mi>.&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
</math> 

<br />

else  <br />

 &nbsp; &nbsp; 


<math xmlns="&mathml;">
  <mi>arc</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mi>.&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>arc</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>
  <mi>.&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>&pi;</mi>
</math> 

</p>

<p>
When the point is on the halfspace a straight line is needed.
 Which could be an arc with infinite radius.
</p>

<p>Here are some screenshots of the two possible situations
 (excluding the straight line case) where the next point is
 below and above the tangent.
</p>

</div>

<div class="spacer" />

<div class="float25">
<a href="img02.png">
<img src="img02.png" alt="img02.png" height="350" /></a>

</div>
<div class="float25">

<a href="img03.png">
<img src="img03.png" alt="img03.png" height="350" /></a>

</div>
<div class="float25">

<a id="Distance_from_a_Point_to_the_Arc"></a>
<h2>Distance from a Point to the Arc</h2>

<p>Use halfspace tests about the arc
 arms to handle cases where the minimum
 distance is to an end point.
</p>

<p>The case where the point is in the arcs
 angle range is then handled.
</p>

<p>For computation purposes the square root
 is avoided and the distance measure is the
 standard distance squared.
</p>

<p>The points q<sub>i</sub> are the control
 points. To associate a control point uniquely
 with an arc only the minimum distance is
 considered between a control point and all
 the arcs.
</p>

</div>
<div class="float25">

<img src="img05.png" alt="img05.png" />


</div>
<div class="float25">

<a id="Handeling_a_large_number_of_Arcs"></a>
<h2>Handling a large number of Arcs</h2>

<p>Lets say that the program can easily solve for a small number of
 arcs say 10.  How can this be applied to solve for 1000.
</p>

<p>Maybe use line segment to find a path
 and order the control points.
</p>

<p>
Pre order the control points along the
 path so they divide the path into K
 control points with I intervals.
 Let their be N2 arcs for each interval.
 and that N2 be an integer.
</p>

<p>Use the solver to minimized for K 
 control points. Sequentually
 solve the path.
</p>

<p>The client could choose K and N2.</p>


<p>I have suggested this rather than solving
 for the full N arcs to keep the complexity
 linear as the number of arcs is increased.
</p>

</div>
<div class="float25">

<a id="Model_Connected_Arcs_in_Cartesian_Coordinates"></a>
<h2>Model Connected Arcs in Cartesian Coordinates</h2>

<p>Here the points connecting the arcs are allowed to vary in x and y.
 The arcs end points are continuous.</p>

</div>
<div class="float25">

<a id="Minimization_Algorithm"></a>
<h2>Minimization Algorithm</h2>

<p>
<a class="reflocal2" href="#Algorithm1">
Algorithm1</a> uses a derivative free
 minimizer.
</p>

<p>Mike suggested the Levenberg-Marquadt method (also called marquardt method) see
 [1] has become the standard of nonlinear least squares problems.
</p>


</div>


<div class="float25">
<a id="Arc_Formats"></a>
<h2>Arc Formats</h2>

<p>There are two formats, one for drawing the arcs and the other for
 solving for k arcs given n control points.
</p>

<p>The idea is to feed the problem to the solver through the arc
 solver format. It then produces a file in the arc drawing format.
 To get the solution the application reads the output of the solver
 through the arc drawing format.
</p>

<p>The arcs are convex.  The sign of the radius corresponds to 
 anticlockwise winding for a positive radius and clockwise winding
 for a negative radius.
</p>

<a id="Arc_Drawing_Format"></a>
<h3>Arc Drawing Format</h3>

<ul>
<li>Number of points</li>
<li>List of 2D points</li>
<li>Number of arcs</li>
<li>List of arcs</li>
</ul>

<p>Here is an example of the arc format, with three arcs.
</p>

<pre>
4
0.0 0.0
1.0 1.1
1.5 2.8
2.8 4.2
3
0 1 2.5
1 2 -2.5 
2 3 -2.5
</pre>

<p>An arc is defined by indexing into the points vector and then
 the radius.  A positive radius has anti clockwise winding and
 a negative radius has clockwise winding.
</p>

<p>
 The order of the points is important.  0 1 2.5 and 1 0 2.5 have
 mirrored arcs.  
 Note that changing the point order is equivalent to multiplying
 the radius by -1.  
</p>

<a id="Arc_Solver_Format"></a>
<h3>Arc Solver Format</h3>

<ul>
<li>Initial angle</li>
<li>Number of arcs</li>
<li>Number of control points</li>
<li>List of control points</li>
</ul>


</div>

<!--UPDATE_SOURCE_TABLE-->
<!--UPDATE_SOURCE_TABLE_BEGIN-->
<div class="float25">
<a id="Source"> </a>
<h2> <a class="reflocal" href="../../download.html">Source</a> </h2>
<h3> Files </h3>
<a class="reflocal" href="Makefile">Makefile</a> <br/>
<a class="reflocal" href="arc.cpp">arc.cpp</a> <br/>
<a class="reflocal" href="arc.h">arc.h</a> <br/>
<a class="reflocal" href="arcdraw.cpp">arcdraw.cpp</a> <br/>
<a class="reflocal" href="arcdraw.h">arcdraw.h</a> <br/>
<a class="reflocal" href="arcprob.cpp">arcprob.cpp</a> <br/>
<a class="reflocal" href="arcprob.h">arcprob.h</a> <br/>
<a class="reflocal" href="arcsconnected.cpp">arcsconnected.cpp</a> <br/>
<a class="reflocal" href="arcsconnected.h">arcsconnected.h</a> <br/>
<a class="reflocal" href="arcsdef.h">arcsdef.h</a> <br/>
<a class="reflocal" href="farcmin01.cpp">farcmin01.cpp</a> <br/>
<a class="reflocal" href="farcmin01.h">farcmin01.h</a> <br/>
<a class="reflocal" href="main.cpp">main.cpp</a> <br/>
<a class="reflocal" href="openwindowresource.cpp">openwindowresource.cpp</a> <br/>
<a class="reflocal" href="openwindowresource.h">openwindowresource.h</a> <br/>
<a class="reflocal" href="pathlineseg.cpp">pathlineseg.cpp</a> <br/>
<a class="reflocal" href="pathlineseg.h">pathlineseg.h</a> <br/>
<a class="reflocal" href="pathlinesegdraw.cpp">pathlinesegdraw.cpp</a> <br/>
<a class="reflocal" href="pathlinesegdraw.h">pathlinesegdraw.h</a> <br/>
<a class="reflocal" href="pathlinesegtest.cpp">pathlinesegtest.cpp</a> <br/>
<a class="reflocal" href="pathlinesegtest.h">pathlinesegtest.h</a> <br/>
<br/>
<a class="reflocal" href="p01.txt">p01.txt</a> <br/>
<a class="reflocal" href="pa01.txt">pa01.txt</a> <br/>
<a class="reflocal" href="pa02.txt">pa02.txt</a> <br/>
<a class="reflocal" href="pa03.txt">pa03.txt</a> <br/>
<a class="reflocal" href="pa04.txt">pa04.txt</a> <br/>
<a class="reflocal" href="path001.txt">path001.txt</a> <br/>
<a class="reflocal" href="path002.txt">path002.txt</a> <br/>
<a class="reflocal" href="pb01.txt">pb01.txt</a> <br/>
<a class="reflocal" href="projcompile.txt">projcompile.txt</a> <br/>
<a class="reflocal" href="temp01.txt">temp01.txt</a> <br/>
<a class="reflocal" href="temp02.txt">temp02.txt</a> <br/>
<a class="reflocal" href="temp03.txt">temp03.txt</a> <br/>
<a class="reflocal" href="testscript01.txt">testscript01.txt</a> <br/>
<a class="reflocal" href="unittestsreport.txt">unittestsreport.txt</a> <br/>
<br/>
</div>
<div class="float25">
<h3> Doxygen </h3>
<a class="reflocal" href="../html/arcs_2main_8cpp.html">main.cpp</a> <br/>
<a class="reflocal" href="Makefile.html"> Makefile </a> <br/>
<a class="reflocal" href="../html/classarc.html">arc</a> <br/>
<a class="reflocal" href="../html/classarcdraw.html">arcdraw</a> <br/>
<a class="reflocal" href="../html/classarcprob.html">arcprob</a> <br/>
<a class="reflocal" href="../html/classarcsconnected.html">arcsconnected</a> <br/>
<a class="reflocal" href="../html/classfarcmin01.html">farcmin01</a> <br/>
<a class="reflocal" href="../html/classopenwindowresource.html">openwindowresource</a> <br/>
<a class="reflocal" href="../html/classpathlineseg.html">pathlineseg</a> <br/>
<a class="reflocal" href="../html/classpathlinesegdraw.html">pathlinesegdraw</a> <br/>
<a class="reflocal" href="../html/classpathlinesegtest.html">pathlinesegtest</a> <br/>
<a class="reflocal" href="../html/classpathlinesegvec.html">pathlinesegvec</a> <br/>
</div>
<!--UPDATE_SOURCE_TABLE_END-->

<div class="float25">
<a id="TODO"></a>
<h2>TODO</h2>

<ul>

<li>Develope a format for the problem.<br />
Initial arc angle, Number of arcs, Number of control points, control points. 
<br />
Have a test function to read in and display the control points in purple.
 The arc points are displayed in green.
</li>

<li>draw arcs correctly<br />
  arc format defined and documented.
</li>


<li>
Initial condition <br />
  - implement
</li>

<li>
 Write an algorithm to 
<a class="reflocal2" href="#Handeling_a_large_number_of_Arcs">
 Handeling a large number of Arcs</a>.
</li>


</ul>


</div>

<div class="spacer" />


<div class="float25">
<a id="Constructing_an_Arc_through_Two_Points_given_an_Angle"></a>
<h2>Constructing an Arc through Two Points given an Angle</h2>

<img src="img01.png" alt="img01.png" />

<p>
The problem is that given an initia angle and two points construct a new arc.
</p>

<p class="equ">
Given &nbsp; 

<math xmlns="&mathml;">
  <mi>&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
</math>
,  
<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
</math>
,  
<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
</math>
 <br />
find <br />
 radius and center c and 
<math xmlns="&mathml;">
  <mi>&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
</math>

</p>

<p class="equ">
Let r be the positive valued radius and is a scalar variable. <br />
<math xmlns="&mathml;">
  <mi>a</mi>
  <mo>=</mo>
  <mo>(</mo>
  <mi>cos</mi>
  <mi>&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>,</mo>
  <mi>sin</mi>
  <mi>&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>)</mo>
</math>

<br />

<math xmlns="&mathml;">
  <mo>-</mo>
  <mi>c</mi>
  <mo>+</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>a</mi>
  <mi>r</mi>
</math>

</p>

<p>Since the arc is on a circle the distance between the center
 and the points is equal.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>d</mi>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>,</mo>
  <mi>c</mi>
  <mo>)</mo>
  <mo>=</mo>
  <mi>d</mi>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>,</mo>
  <mi>c</mi>
  <mo>)</mo>

</math>

<br />

<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>c</mi>
  <mo>)</mo>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>
  <mo>=</mo>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>c</mi>
  <mo>)</mo>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>
</math>

<br />

<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>

  <mo>-</mo>

  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>

  <mo>=</mo>

  <mi>2</mi>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <mi>c</mi>
</math>
</p>

<p>Rearrange c and substitute it into the above expression.</p>

</div>

<div class="float25">

<p class="equ">

<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>

  <mo>-</mo>

  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>

  <mo>=</mo>

  <mi>2</mi>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>a</mi>
  <mi>r</mi>
  <mo>)</mo>
</math>

</p>

<p>r is the only scalar, solve for it.</p>

<p class="equ">

<math xmlns="&mathml;">
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>

  <mo>-</mo>

  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>

  <mo>=</mo>

  <mi>2</mi>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>)</mo>
  <mo>-</mo>
  <mi>2</mi>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <mi>a</mi>
  <mi>r</mi>
</math>

<br />

<math xmlns="&mathml;">
  <mo>(</mo>
  <mo>&PlusMinus;</mo>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <mo>)</mo>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>
  <mo>=</mo>
  <mi>2</mi>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <mi>a</mi>
  <mi>r</mi>
</math>

<br />

<math xmlns="&mathml;">
  <mi>r</mi>
  <mo>=</mo>

  <mfrac>
    <mrow>

  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>

    </mrow>
    <mrow>

<mi>2</mi>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <mi>a</mi>

    </mrow>
  </mfrac>




</math>


</p>

<p>
Use this to calculate the center c.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>c</mi>
  <mo>=</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>0</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>a</mi>
  <mi>r</mi>
</math>



</p>

<p>
 Calculate the second angle by shifting the point about the center and
 using the atan2 routine to get the second angle. 
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>&varphi;</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>tan</mi>
  <msup>
    <mi></mi>
    <mrow><mo>-</mo><mi>1</mi></mrow>
  </msup>
  <mo>(</mo>

  <mfrac>
    <mrow><mi>1</mi></mrow>
    <mrow><mi>r</mi></mrow>
  </mfrac>
  <mo>(</mo>
  <mi>p</mi>
  <msub>
    <mi></mi>
    <mrow><mi>1</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>c</mi>
  <mo>)</mo>

  <mo>)</mo>
</math>


</p>

<p>The arc is interpreted as being convex.  However the sign
 is used to indicate the anticlockwise (positive) winding or
 clockwise(negative) winding. A test based on the values
 of the angles was used.
</p>

<pre>
bool const arc::isAntiClockwise2() const
{
  double dphi = phi1-phi0;
  if (dphi&lt;0.0)
    dphi += 2.0*PI;

  if (dphi&lt;=PI)
    return true;

  return false;
}
</pre>

<p class="equ">
if arc.isAntiClockwise2() <br />
  &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mi>radius</mi>
  <mo>=</mo>
  <mi>r</mi>
</math>
<br />
else<br />
  &nbsp; &nbsp; 
<math xmlns="&mathml;">
  <mi>radius</mi>
  <mo>=</mo>
  <mo>-</mo>
  <mi>r</mi>
</math>

</p>

</div>
<div class="float25">

<a id="Algorithm_Complexity"></a>
<h2>Algorithm Complexity</h2>

<p>
This is to model and compare different 
 algorithms. I do not know the complexity
 of the minimizers on a particular problem.
</p>

<p>If the problem is divided into N
 then let this be the measure.
</p>

<p>For example I do not know if
1..N^2 or 1..N^3 iterations of the
 minimizer are needed, but can
 speculate.
</p>

<p>
Let PM be the problem minimizer where P
 is the order of the problem and M the
 order of the minimizer.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>O</mi>
  <mo>(</mo> 
  <mi>PM</mi>
  <mo>)</mo>
  <mo>=</mo>
  <mi>N</mi>
  <msup>
    <mi></mi>
    <mrow><mi>P</mi><mo>-</mo><mi>M</mi></mrow>
  </msup>
</math>

</p>

</div>
<div class="float25">


<p>
Adding the inner loop with the distance
 function used by the minimizer.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>O</mi>
  <mo>(</mo> 
  <mi>PM</mi>
  <mo>)</mo>
  <mo>=</mo>
  <mi>N</mi>
  <msup>
    <mi></mi>
    <mrow><mi>P</mi><mo>-</mo><mi>M</mi></mrow>
  </msup>
  <mo>(</mo> 
  <mi>inner loop</mi>
  <mo>)</mo>
</math>

</p>


<p>
For example using exploratory
 minimizer M=1, assume arcs minimizing
 arcs is P=2. O(N) control points.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>O</mi>
  <mo>(</mo> 
  <mi>N</mi>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi><mo>-</mo><mi>1</mi></mrow>
  </msup>
  <mo>)</mo>
  <mi>O</mi>
  <mo>(</mo> 
  <mi>N</mi>
  <msup>
    <mi></mi>
    <mrow><mi>2</mi></mrow>
  </msup>
  <mo>)</mo>
  <mo>=</mo>
  <mi>N</mi>
  <msup>
    <mi></mi>
    <mrow><mi>3</mi></mrow>
  </msup>
</math>
</p>

<p>
Local arcs would take the distance
 function from O(N^2) to O(N).
 A quadratic minimizer would reduce
 by another order too.
</p>

<p>
This line of reasoning is speculative.
 The inner loop is true but the outer look
 is a guess of what might be happening.
</p>

</div>
<div class="float25">

<a id="Line_Segments"></a>
<h2>Line Segments</h2>

<p>
 Find N connected line segments best fitted
 to W control points where W is larger 
 than N.
</p>

<p>
<code>
./main prog=30 file=path002.txt
</code>
</p>

<img src="img07.png" alt="img07.png" ></img>

<p>
Minimizing the distance of all the line
 segments to 
 all the control points produced a solution
 the I did not expect but is understandable.
 min=0.273
</p>

</div>
<div class="float25">


<p>
<code>
$./main prog=33 file2=temp3.txt distfn=1<br />
$./main prog=30 file=temp3.txt
</code>
</p>

<img src="img08.png" alt="img08.png" ></img>

<p>Adding the segment length to the
 distance function. min=2.540
</p>

<img src="img09.png" alt="img09.png" ></img>

<p>A convexity condition for successive
 line segments could have stopped
 chopping and may help other minimization
 algorithm implementations.
</p>


<p>Changing the step length
 from 0.3 to 0.1 showed the
 target solution was a local
 minimum and not a global minimum.
 min=3.239
</p>

</div>
<div class="float25">

<p>
<code>
$./main prog=33 file2=temppath2.txt h0=0.1 imax=15 distfn=2<br />
$./main prog=30 file=temppath2.txt
</code>
</p>

<img src="img10.png" alt="img10.png" ></img>

<p>
This is still not the full story.
 An initial curve was given which
 was already a path with convex angles
 greater than 90 degrees.
</p>


</div>
<div class="float25">
<a id="Algorithm1"></a>
<h2>Algorithm1</h2>

<p class="equ">
Let w<sub>i</sub> be N+1 points connecting
 the N arcs.
</p>

<p>
Initial approximation
 found by fitting N+1 control points
 which include the fixed ends of the
 curve and fit the arcs through satisfying
 the initial angle.
</p>
<br />

<p class="equ">
Solve arc<sub>k</sub> <br />
Given w<sub>k</sub> and w<sub>k+1</sub> and
 &theta; <br />
arc<sub>k</sub>.p0 = w<sub>k</sub> <br />
arc<sub>k</sub>.p1 = w<sub>k+1</sub> <br />
arc<sub>k</sub>.&varphi;<sub>0</sub> = 
 &theta; or &theta; + &pi;<br />
Solve arc<sub>k</sub> by finding
 arc<sub>k</sub>.&varphi;<sub>1</sub>. 
</p>




<p class="equ">
Updating connected arcs f. <br />

f(w<sub>1</sub>.x, 
  w<sub>1</sub>.y, 
 ... <br />
 w<sub>N-1</sub>.x,
 w<sub>N-1</sub>.y
)
<br />
k = 1..N <br />
 &nbsp; solve arc<sub>k</sub>
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>dist01</mi>
  <mo>=</mo>


  <munder>
    <mi>&Sum;</mi>
    <mrow><mi>i</mi></mrow>
  </munder>
  <munder>
    <mi>&Sum;</mi>
    <mrow><mi>k</mi></mrow>
  </munder>

  <mi>d</mi>
  <mo>(</mo>
  <mi>q</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>

  <mo>,</mo>
  <mi>arc</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>)</mo>
</math>
<br />
 
 &nbsp; 

<math xmlns="&mathml;">
  <mo>+</mo>
  <munder>
    <mi>&Sum;</mi>
    <mrow><mi>k</mi></mrow>
  </munder>
  <mi>arc</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>.</mo>
  <mi>length</mi>
  <mo>(</mo>
  <mo>)</mo>


</math>

</p>

<p class="equ">
Minimize f with dist01.
</p>



</div>
<div class="spacer" />

<div class="float25">
<a id="References"></a>
<h2>References</h2>

<ol>
<li> Press Teukolsky Vetterling Flannery (1992) second edition.
  Numerical Recipes in C. ISBN 0-521-431088. Page 683 </li>

</ol>


</div>



</body>
</html>



