<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" 
               "http://www.w3.org/TR/MathML2/dtd/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>
<link rel="stylesheet" type="text/css" href="../../../comsci/css/s011.css"/>
<link rel='SHORTCUT ICON' href='../../../comsci/images/FrameHome.ico' />
<style type="text/css">
</style>
<title> Midpoint Line Algorithm </title>
</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 2004-06-01 &nbsp; Modified 
<!--UPDATE_DATE_MODIFIED-->
<!--UPDATE_DATE_BEGIN-->
2007-01-01
<br/>
<a href="mailto:chelton.evans@yahoo.com">Chelton Evans</a>
<!--UPDATE_DATE_END-->
</div>


<div>


<h1> 
<a href="geom.html"> <img alt="proj" src="../../../comsci/images/compgeom.png" /></a>
Midpoint Line Algorithm 
</h1>

<p>
<a href="#Intro">Intro</a><br/>
<a href="#Algorithm">Algorithm</a><br/>
<a href="#Algorithm_Derivation">Algorithm Derivation</a><br/>
<a href="#Hand_Trace">Hand Trace</a><br/>

</p>

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


<p> I had a read of "Computer Graphics: 
 Principles and Practice"  2nd edition
 around pages 78, which gives the
 Midpoint Line Algorithm, and I can
 throughly say I didn't understand it.
</p>

<p> The use of Implicit functions to
 derive the algorithm - well where
 did the implicit form come from? 
 Its at best a proof without meaning!
</p>

<p> Long ago I came to the conclusion
 that such maths was bullshit and so
 I derive my own. I have pointed the
 superficialness of the proof so that
 you may recognize fluff when you
 see it - underlining their fluff is
 a really good idea and as
 far as implicit functions go they
 are general but how was the equation
 formed in the first place?
</p>

</div>

<div class="float25">

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

<p class="equ">
  Line translated to origin for simplicity<br/>
 Line from  &nbsp; 

<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>0</mi>
  <mo>,</mo>
  <mi>0</mi>
  <mo>)</mo>
</math> 
 &nbsp; to  &nbsp; 

<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>x1</mi>
  <mo>,</mo>
  <mi>y1</mi>
  <mo>)</mo>
</math> 

</p>

<p class="equ">
Initial Conditions <br/>

<math xmlns="&mathml;">
  <mi>dx</mi>
  <mo>=</mo>
  <mi>x1</mi>
</math> 
<br/>

<math xmlns="&mathml;">
  <mi>dy</mi>
  <mo>=</mo>
  <mi>y1</mi>
</math> 
<br/>

<math xmlns="&mathml;">
  <mi>d</mi>
  <mo>=</mo>
  <mi>2</mi>
  <mi>dy</mi>
  <mo>-</mo>
  <mi>dx</mi>
</math> 
<br/>

<math xmlns="&mathml;">
  <mi>x</mi>
  <mo>=</mo>
  <mi>0</mi>
</math> 
<br/>

<math xmlns="&mathml;">
  <mi>y</mi>
  <mo>=</mo>
  <mi>0</mi>
</math> 
<br/>

</p>

 
<p class="equ">
while &nbsp; 
<math xmlns="&mathml;">
  <mi>x</mi>
  <mo>&leq;</mo>
  <mi>dx</mi>
</math>
<br/>
{
<br/>
 &nbsp; &nbsp;
 write point 
<br/>
<br/>
 &nbsp; &nbsp;
 if &nbsp; 
<math xmlns="&mathml;">
  <mi>d</mi>
  <mo>&gt;</mo>
  <mi>0</mi>
</math>
<br/>
 &nbsp; &nbsp; &nbsp; &nbsp; 

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

<br/>


 &nbsp; &nbsp; &nbsp; &nbsp; 

<math xmlns="&mathml;">
  <mi>d</mi>
  <mo>-=</mo>
  <mi>2</mi>
  <mi>dx</mi>
</math>

<br/>


<br/>

 &nbsp; &nbsp; 

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

<br/>

&nbsp; &nbsp; 

<math xmlns="&mathml;">
  <mi>d</mi>
  <mo>+=</mo>
  <mi>2</mi>
  <mi>dy</mi>
</math>

<br/>

}

</p>


</div>

<div class="spacer" />

<div class="float25">

<a id="Algorithm_Derivation"></a> 
<h2> Algorithm Derivation </h2>

<p class="equ">

<math xmlns="&mathml;">
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>
  <mo>=</mo>
  <mo>&lfloor;</mo>
  <mfrac>
    <mrow><mi>dy</mi></mrow>
    <mrow><mi>dx</mi></mrow>
  </mfrac>
  <mi>i</mi>
  <mo>&rfloor;</mo>
</math>


</p>


<p class="equ">
<math xmlns="&mathml;">
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>
  <mo>=</mo>
  <mfrac>
    <mrow><mi>dy</mi></mrow>
    <mrow><mi>dx</mi></mrow>
  </mfrac>
  <mi>i</mi>
  <mo>-</mo>
  <mfrac>
    <mrow><mi>1</mi></mrow>
    <mrow><mi>2</mi></mrow>
  </mfrac>
  <mi>t</mi>
</math>
</p>

<p>
Lets discuss this. The negative sign
 is rounding down. 1/2 is the point
 at which the rounding function balances
 and hence makes a decision at.
</p>

<p>
The equation itself is in integer arithmetic with rounding down. In the next step
 the equation will be brought fully
 into the integer domain by removing
 division. At present its in a gray
 area but I like it here.
</p>

<p> Increment the equation </p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>=</mo>
  <mfrac>
    <mrow><mi>dy</mi></mrow>
    <mrow><mi>dx</mi></mrow>
  </mfrac>
  <mo>(</mo>
  <mi>i</mi>
  <mo>+</mo>
  <mi>1</mi>
  <mo>)</mo>
  <mo>-</mo>
  <mfrac>
    <mrow><mi>1</mi></mrow>
    <mrow><mi>2</mi></mrow>
  </mfrac>
  <mi>t</mi>
</math>

<br/>
<math xmlns="&mathml;">
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>w</mi>
  <mo>=</mo>
  <mfrac>
    <mrow><mi>dy</mi></mrow>
    <mrow><mi>dx</mi></mrow>
  </mfrac>
  <mi>i</mi>
  <mo>+</mo>
  <mfrac>
    <mrow><mi>dy</mi></mrow>
    <mrow><mi>dx</mi></mrow>
  </mfrac>
  <mo>-</mo>
  <mfrac>
    <mrow><mi>1</mi></mrow>
    <mrow><mi>2</mi></mrow>
  </mfrac>
  <mi>t</mi>
</math>


</p>

<p>
Now as discussed in
<a href="g004.xml"> Computing a Straight
 Line in Integers </a> 

 the change of one incremented equation
 relative to another in

 &nbsp; 
<math xmlns="&mathml;">
  <mi>t</mi>
</math>
 &nbsp; 

is one way, so redefine &nbsp; 


<math xmlns="&mathml;">
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>
</math>

.

</p>


<p class="equ">
<math xmlns="&mathml;">
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>
  <mo>=</mo>
  <mfrac>
    <mrow><mi>dy</mi></mrow>
    <mrow><mi>dx</mi></mrow>
  </mfrac>
  <mi>i</mi>
</math>
 <br/>
 relative to &nbsp; 
<math xmlns="&mathml;">
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
</math>

</p>

</div>

<div class="float25">
<p> Subtract the equations for 
 y.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>w</mi>
  <mo>=</mo>
  <mfrac>
    <mrow><mi>dy</mi></mrow>
    <mrow><mi>dx</mi></mrow>
  </mfrac>
  <mo>-</mo>
  <mfrac>
    <mrow><mi>1</mi></mrow>
    <mrow><mi>2</mi></mrow>
  </mfrac>
  <mi>t</mi>
</math>


<br/>
<math xmlns="&mathml;">
  <mi>2</mi>
  <mi>w</mi>
  <mi>dx</mi>
  <mo>=</mo>
  <mi>2</mi>
  <mi>dy</mi>
  <mo>-</mo>
  <mi>t</mi>
  <mi>dx</mi>
</math>

<br/>
<math xmlns="&mathml;">
  <mi>t</mi>
  <mi>dx</mi>
  <mo>=</mo>
  <mi>2</mi>
  <mi>dy</mi>
  <mo>-</mo>
  <mi>2</mi>
  <mi>w</mi>
  <mi>dx</mi>
</math>

</p>


<p>
Note that &nbsp; 
<math xmlns="&mathml;">
  <mi>t</mi>
  <mi>dx</mi>
</math> &nbsp; corresponds
 with 


 &nbsp; 
<math xmlns="&mathml;">
  <mi>d</mi>
</math> &nbsp; 

 in the algorithm.
 After some reflection this is to
 be expected as a consequence of
 considering the change about the
 floor operator. Relabel.
</p>

<p class="equ">

<math xmlns="&mathml;">
  <mi>w</mi>
  <mo>=</mo>
  <mo>(</mo>
  <mi>d</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi></mrow>
  </msub>
  <mo>&leq;</mo>
  <mi>0</mi>
  <mo>)</mo>
</math>

<br/>
  


<math xmlns="&mathml;">
  <mi>d</mi>
  <msub>
    <mi></mi>
    <mrow><mi>i</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>2</mi>
  <mi>dy</mi>
  <mo>-</mo>
  <mi>2</mi>
  <mi>w</mi>
  <mi>dx</mi>
</math>

</p>


<p>
There is still a bit of work to
 be done in the algorithm details,
 eg the correct w condition needs
 to be solved for in detail.
 Similarly I have not derived the
 initial algorithm condition which
 in itself is interesting, if someone
 can do it shoot me an email.
</p>

<p>
To conclude this method is generalizable.
 I have constructed two of the 
 integer arithmetic algorithms and
 they share similar assumptions which
 seem to work.  The best thing about
 these constructions is that they
 are direct eg from the line equation
 identify the change and perform
 analysis to get the algorithm,
 something which the authors of
 the algorithm could not do.
</p>

</div>

<div class="spacer" />

<div class="float25">

<a id="Hand_Trace"></a>
<h2> Hand Trace </h2>


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

</div>

</div>

</body>
</html>





