title: Detecting Singularities in a Graph
body: <p>I am creating a graphing calculator in Java as a project for my programming class. There are two main components to this calculator: the graph itself, which draws the line(s), and the equation evaluator, which takes in an equation as a <code>String</code> and... well, evaluates it.</p>
  
  <p>To create the line, I create a <code>Path2D.Double</code> instance, and loop through the points on the line. To do this, I calculate as many points as the graph is wide (e.g. if the graph itself is 500px wide, I calculate 500 points), and then scale it to the window of the graph.</p>
  
  <p>Now, this works perfectly for most any line. However, it does not when dealing with <a href="https://en.wikipedia.org/wiki/Mathematical_singularity" rel="nofollow noreferrer">singularities</a>.</p>
  
  <p>If, when calculating points, the graph encounters a domain error (such as 1/0), the graph closes the shape in the <code>Path2D.Double</code> instance and starts a new line, so that the line looks mathematically correct. Example:</p>
  
  <p><a href="https://i.stack.imgur.com/GiCbG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GiCbG.png" alt="Good Asymptote"></a><br>
  <sub>(source: <a href="http://content.imagesocket.com/images/good_line7cd.png" rel="nofollow noreferrer">imagesocket.com</a>)</sub>  </p>
  
  <p>However, because of the way it scales, sometimes it is rendered correctly, sometimes it isn't. When it isn't, the actual asymptotic line is shown, because within those 500 points, it skipped over <code>x = 2.0</code> in the equation <code>1 / (x-2)</code>, and only did <code>x = 1.98</code> and <code>x = 2.04</code>, which are perfectly valid in that equation. Example:</p>
  
  <p><a href="https://i.stack.imgur.com/ANvaS.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ANvaS.gif" alt="Bag Asymptote"></a><br>
  <sub>(source: <a href="http://content.imagesocket.com/images/bad_linef6e.png" rel="nofollow noreferrer">imagesocket.com</a>)</sub>  </p>
  
  <p>In that case, I increased the window on the left and right one unit each.</p>
  
  <p>My question is: <strong>Is there a way to deal with singularities using this method of scaling so that the resulting line looks mathematically correct?</strong></p>
  
  <p>I myself have thought of implementing a binary search-esque method, where, if it finds that it calculates one point, and then the next point is wildly far away from the last point, it searches in between those points for a domain error. I had trouble figuring out how to make it work in practice, however.</p>
  
  <p>Thank you for any help you may give!</p>