Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, I have classical solution that work in any dimensions. </p> <p>First of all, you got sphere and a line, and you need to have good model of them. Sphere is easy you just have a Vector <code>.center</code> and <code>.diameter</code>.</p> <pre><code>class Sphere: def __init__( sphere, center, diameter ): sphere.center=Vector(center) sphere.diameter=float(diameter) </code></pre> <p>Line could be more problematic for beginners because it could be defined in many ways. The most useful comes from parametric equation, you have a direction in Vector <code>.direction</code> and some staring point <code>.center</code>. We assume that <code>.direction</code> is unit length, and <code>.center</code> is the nearest point on line from (0,0). In most cases we need to create a line, having to points Vectors:</p> <pre><code>def line_on_two_points( A, B ): return Line( direction= Vector(B)-A, center=A ) </code></pre> <p>So we have to fix the <code>direction</code> and <code>center</code> in the constructor. <code>.direction</code> is easy to fix wee need just to make it unit length. To find <code>.center</code>, we need <a href="http://en.wikipedia.org/wiki/Scalar_projection" rel="nofollow noreferrer">scalar projection</a>. Here is as vector to D: <img src="https://i.stack.imgur.com/vrA4j.png" alt="The nearest point on line from (0,0)"></p> <p>Having <code>.direction</code> as unit length A to B and <code>center</code> as from C to A, we could init our line as:</p> <pre><code>class Line: def __init__( line, direction, center ): line.direction= Vector(direction) / length(direction) line.center= center - line.direction*dot(center,line.direction) </code></pre> <p>If we don't have a line, just two points we could just do:</p> <pre><code>#class Sphere: def colide_line_on_two_points( sphere, A, B ): line=line_on_two_points( A-sphere.center, B-sphere.center) return length(line.center) &lt; sphere.diameter </code></pre> <p>But when we have a line we try to optimize it as:</p> <pre><code>#class Sphere: def colide_line( sphere, line ): return line.distance_to(sphere.center) &lt; sphere.diameter </code></pre> <p>The <code>.distance_to()</code> function is a bit tricky:</p> <p><img src="https://i.stack.imgur.com/CB2e7.png" alt="Vector from line to point"></p> <pre><code>#class Line: def vector_to( line, P ): return line.center + line.direction * dot(line.direction,P) - P def distance_to( line, P ): return length( line.center + line.direction * dot(line.direction,P) - P ) def move_to( line, P ): line.center += line.direction * dot(line.direction,P) - P </code></pre> <p>The last but not least is the <code>Vector</code> type, I try numpy, but it's rather slow for 2D,3D:</p> <pre><code>from numpy import array as Vector from numpy import dot from numpy.linalg import norm as length </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload