<?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> Computing a Straight Line in Integers </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>
Computing a Straight Line in Integers 
<a href="../../../../index.html">
<img alt="home" src="../../../comsci/images/Frame.gif" /> </a>
</h1>

<p>
<a href="#Intro">Intro</a><br/>
<a href="#Problem_Description">Problem Description</a><br/>
<a href="#Algorithm_Discussion">Algorithm Discussion</a><br/>
<a href="#Order_Driven_Algebra_Analysis">
  Order Driven Algebra Analysis</a><br/>
<a href="#Solving_for_t">Solving for t</a><br/>

</p>

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

<p>
"Computer Graphics Principles and Practice" by Foley, van Dam, Feirner 
 and Hughes, second edition
 discusses several raster graphics algorithms for drawing 2D primitives.
 The midpoint line algorithm, the midpoint circle algorithm, midpoint 
 ellipse scan-conversion algorithm and finally the scan-line algorithm.
</p>

<p> I say finally because all these algorithms share the same
 way of computing.  The algorithms were introduced in that
 order, but by far the simplest is the 
 method used to compute the straight line.
 Its buried in the scan-line algorithm.
 See page 97 LeftEdgeScan procedure.
</p>

<p>
Call the algorithm to compute the straight line in integers the 
 <b>Line Algorithm</b>.
</p>

<a id="Problem_Description"></a>
<h2> Problem Description </h2>

<p>
On a integer grid points are placed to approximate a straight
 line, through the origin to simplify the maths.
 Let both end points lie on the grid. 
 <br/> ie   &nbsp; 

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

 &nbsp; and &nbsp; 

<math xmlns="&mathml;">
  <mo>(</mo>
  <mi>a</mi>
  <mo>,</mo>
  <mi>b</mi>
  <mo>)</mo>
</math>

 be the end points of the straight line.

Let 

<math xmlns="&mathml;">
  <mi>b</mi>
  <mo>&gt;</mo>
  <mi>a</mi>
</math>
.

 
</p>



<p>
Derive the following algorithm as given in Computer Graphics
 page 97 LeftEdgeScan procedure. It has been adapted - the 
 notation changed for simplicity.
</p>

<p class="equ">

<math xmlns="&mathml;">
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>1</mi>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mo>(</mo>
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>b</mi>
  <mo>)</mo>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>a</mi>
  <mo>+</mo>
  <mo>(</mo>
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>b</mi>
  <mo>)</mo>
  <mo>.</mo>
  <mo>-</mo>
  <mi>b</mi>
</math>

</p>

</div>

<div class="float25">
<a id="Algorithm_Discussion"></a>
<h2> Algorithm Discussion </h2>

<p>
The restriction that 
<math xmlns="&mathml;">
  <mi>b</mi>
  <mo>&gt;</mo>
  <mi>a</mi>
</math>
 is to guarantee the change in y to be constant. A point is generated
 from a previous point and its state(the f variable holds the state).
</p>


<p>
 For other curves the curve is divided into regions where one of the
 variables has a constant change of unity.
</p>

<p>
The x variable has either no change or changes by 1.
</p>

<p>
The issue here is how the algorithm works. I am happy that its
 simple to implement, and can see the same technique being
 used in similar algorithms, but whats driving it?
 Can it be shown indeed to approximate the straight line.
 Its like a limit but it never really converges to an exact
 solution because the arithmetic is in integers - which 
 makes it really interesting.  Here is something fundamentally
 different to the standard "maths" view of reality - its like
 a discrete limit but there isn't any language in common use
 to describe this.
</p>

<p>
I hope that I have shown that there is something deeper here,
 and that its a problem worthy of attention.
 So faced with this situation the question remains how do you
 analyze it?
</p>

</div>

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

<div class="float25">

<a id="Order_Driven_Algebra_Analysis"></a>
<h2> Order Driven Algebra Analysis </h2>

<p> The following uses an <a href="../calc031.xml"> Order Driven Algebra </a> .
 Essentially you can not interchange the sides of the equation. For example 
 the f variable will be defined on the lhs and needs to stay on the 
 lhs throughout the calculation. I am growing algebra.
</p>



<p class="equ">
<math xmlns="&mathml;">
  <mi>y</mi>
  <mo>=</mo>
  <mfrac>
    <mrow><mi>b</mi></mrow>
    <mrow><mi>a</mi></mrow>
  </mfrac>
  <mi>x</mi>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>a</mi>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&approx;</mo>
  <mi>b</mi>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
</math>

</p>

<p>
Although the approximate symbol is being employed it does 
 not say why. In a sense the magnitude of the difference 
 from the exact solution diminishes compared with the magnitude
 of x and y, which is what happens in a limit. In short its
 a discrete form of a limit.
</p>


<p> Increment the system. </p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>a</mi>
  <mo>(</mo>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
  <mo>&approx;</mo>
  <mi>b</mi>
  <mo>(</mo>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>)</mo>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>a</mi>
  <mo>(</mo>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>1</mi>
  <mo>)</mo>
  <mo>&approx;</mo>
  <mi>b</mi>
  <mo>(</mo>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>t</mi>
  <mo>)</mo>
</math>

 &nbsp; where &nbsp;  

<math xmlns="&mathml;">
  <mi>t</mi>
  <mo>=</mo>
  <mo>{</mo>
  <mi>0</mi>
  <mo>,</mo>
  <mi>1</mi>
  <mo>}</mo>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>a</mi>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>a</mi>
  <mo>&approx;</mo>
  <mi>b</mi>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>b</mi>
  <mi>t</mi>
</math>


</p>

<p>
Now the change in y was known so y was incremented by one. The change in 
 x is unknown and called t which is either 0 or 1.
</p>

<p> Express the equation on one side. f is essentially the straight
 line equation. </p>


<p class="equ">
  Let <br/>

<math xmlns="&mathml;">
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>a</mi>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>b</mi>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
</math>
 on the lhs.

<br/>

<math xmlns="&mathml;">
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&approx;</mo>
  <mi>0</mi>
</math>

</p>

<p class="equ">
Similarly we would expect <br/>

<math xmlns="&mathml;">
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>&approx;</mo>
  <mi>0</mi>
</math>

</p>

<p> Consider the difference and substitute the result from the incremented equation. </p>

<p class="equ">

<math xmlns="&mathml;">
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>0</mi>
</math>

 &nbsp; 
 relative to   &nbsp; 
<math xmlns="&mathml;">
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
</math>

<br/>




<math xmlns="&mathml;">
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>a</mi>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>b</mi>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>a</mi>
  <mi>y</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>b</mi>
  <mi>x</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
</math>

<br/>
  



<math xmlns="&mathml;">
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mi>a</mi>
  <mo>-</mo>
  <mi>b</mi>
  <mi>t</mi>
</math>

</p> 

<p>
Now as you may have noticed there is almost a one-one correlation
 between the given algorithm and the derived algorithm.
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>t</mi>
  <mo>=</mo>
  <mo>(</mo>
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>b</mi>
  <mo>)</mo>
</math>

</p>

</div>


<div class="float25"> 

<a id="Solving_for_t"></a>
<h2> Solving for t </h2>

<p> Divide f by b and consider the change along the x-axis. </p>

<p class="equ">
Let <br/>

<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>/</mo>
  <mi>b</mi>
</math>

<br/>


<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>=</mo>
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mfrac>
    <mrow><mi>a</mi></mrow>
    <mrow><mi>b</mi></mrow>
  </mfrac>
  <mo>-</mo>
  <mi>t</mi>
  <mo>&approx;</mo>
  <mi>0</mi>
</math>

</p>

<p class="equ">
Let &nbsp; 
<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>0</mi>
</math>
</p>

<p>
This restricts the change to one side of the line.
It also turns out to be the condition to solve the system.
</p>


<p class="equ">
<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi><mo>+</mo><mi>1</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>0</mi>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>+</mo>
  <mfrac>
    <mrow><mi>a</mi></mrow>
    <mrow><mi>b</mi></mrow>
  </mfrac>
  <mo>-</mo>
  <mi>t</mi>
  <mo>&gt;</mo>
  <mi>0</mi>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>t</mi>
  <mo>&gt;</mo>
  <mi>0</mi>
</math>

<br/>

If this equation is true, so is the previous equation.

</p>

<p class="equ">
Solve <br/>

<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>-</mo>
  <mi>t</mi>
  <mo>&gt;</mo>
  <mi>0</mi>
</math>

<br/>

Case 

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

<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>0</mi>
</math>
 &nbsp; is always true.

<br/>

Case 

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

<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>1</mi>
</math>

<br/>

Both conditions satisfied when &nbsp; 

<math xmlns="&mathml;">
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>1</mi>
</math>
.
<br/>

Let &nbsp; 

<math xmlns="&mathml;">
  <mi>t</mi>
  <mo>=</mo>
  <mo>(</mo>
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>1</mi>
  <mo>)</mo>
</math>
</p>

<p class="equ">
<math xmlns="&mathml;">
  <mi>t</mi>
  <mo>=</mo>
  <mo>(</mo>
  <mi>g</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>1</mi>
  <mo>)</mo>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>t</mi>
  <mo>=</mo>
  <mo>(</mo>
  <mfrac>
  <mrow>

  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>

  </mrow>
  <mrow> <mi>b</mi></mrow>
  </mfrac>

  <mo>&gt;</mo>
  <mi>1</mi>
  <mo>)</mo>
</math>

<br/>

<math xmlns="&mathml;">
  <mi>t</mi>
  <mo>=</mo>
  <mo>(</mo>
  <mi>f</mi>
  <msub>
    <mi></mi>
    <mrow><mi>k</mi></mrow>
  </msub>
  <mo>&gt;</mo>
  <mi>b</mi>
  <mo>)</mo>
</math>


</p>

</div>

</div>


</body>
</html>



