File Exchange Pick of the Week

February 19th, 2008

Practical example: Intersecting a circle with a line series

As with all the videos in this practical example series, the process of developing code is more important than the particular problem that is being solved. In this video, we will be covering topics that are covered in earlier videos:

  • Nested subfunctions
  • Run configurations
  • FIND
  • Custom plots with Handle Graphics
  • However now we will be covering them in the context of a larger project.

    The project came in from e-mail. This MATLAB user wanted to intersect a circle with a line series that extended to the right. There were some subtle algorithm decisions that had to be made so that you did not need to make an exhaustive search.

    Video Content

    iconFiles.jpgiconPod.jpglazy.jpg

    4 Responses to “Practical example: Intersecting a circle with a line series”

    1. Martin Cohen replied on :

      A good start, but ….

      1) What if the last point is inside the circle but some intermediate point is outside? Your code will give up. I think it would be better to look for the first point such that either (it is inside and next is outside) or (it is outside and next is inside). If I wanted to optimize this, I would start at the last point found and search forward and back, assuming that the next one, for a close initial point, would also be close.

      2) You chose a particular solution to the quadratic equation. How do you know it is the correct one? The line intersects the circle in two points – I have not worked through the math to see if you can prove that your choice is always the correct one.

    2. Doug replied on :

      Martin,

      After filming this, I found there were some edge cases like you mention. This is mostly an exercise in developing the algorithm. I am not sure much is gained from getting an absolutely complete solution. These cases can be dealt with, but it does get quite fiddly.

      Your second point. I did not know which one would be correct of the two solutions, so I ran both and plotted them out. It was always the same one that was correct, so I just used it. Not a strict mathematical explanation, but a practical one!

      Thanks for paying attention enough to note these details!
      Doug

    3. rasem palestine replied on :

      dear freind
      i would like to thank u for your hard efforts
      regard’s
      rasem kabi

    4. johnsmith replied on :

      People started to try to solve the classical Greek problem of squaring a circle by construction around 200 BC. It was finally proven impossible in 1882, when Lindemann proved that pi was trancendental. (I will omit the proof of this here.) Here are the basic ideas of the proof, following closely the discussion by Courant and Robbins in “What Is Mathematics.” Given an arbitrary circle, let us define our unit of measure to be the radius of the circle. That means that the circle has area pi. Therefore, in order to construct a square of equal area, we need to construct the side of the square, which must have length square root of pi. I show that this is impossible. First, I must explain what it actually means to say that pi is transendental. This means that pi does not satisfy any rational polynomial equation.
      ——————–
      johnsmith

      Wide Circles

    Leave a Reply

    Wrap code fragments inside <pre> tags, like this:

    <pre class="code">
    a = magic(3);
    sum(a)
    </pre>
    

    If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


    MathWorks

    Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

    These postings are the author's and don't necessarily represent the opinions of The MathWorks.