Note that there are some explanatory texts on larger screens.

plurals
  1. POa good-design approach for making a geometry library (regarding using union or not)?
    text
    copied!<p>i am making a geometry library and i am confused, what should be the return type of a function which calculates the intersection of a segment with another segment. The returned value would sometimes be a point and sometimes be a segment (overlap case) and sometimes be an empty set. As per my thinking there could be 3 ways to tackle this as per follows: 1. return a union (segment, null, point) 2. return a segment with first point == second point when intersection is a single point and both points as NAN when intersection is an empty set 3. return a vector (with 0 elements for empty set, 1 element for pnt and 2 elements for segment)</p> <p>Please let me know if there are any alternates possible and what are the pros and cons of each of the design. Moreover which design is supposed to be a good design and why. I am interested in making a robust architecture which lets single pipe-lining and thus almost no rewriting of code as well as scalable (in terms of adding functionality and handling all the edge cases)</p> <p>Following is my code for reference (whose return type is vector)</p> <pre><code>vector&lt;pnt&gt; seg::inter(seg z){ vector&lt;pnt&gt; ans; if(p1==p2){if(z.doesinter(p1)){ans.pb(p1);}} else if(z.p1==z.p2){ if(doesinter(z.p1)) ans.pb(z.p1);} else{ pnt p1p2=(p2-p1); pnt q1=p1p2*pnt(0,1); long double h1,h2; if(abs((z.p2-z.p1).dot(q1))&lt;=eps){ pnt r1((z.p1-p1)/(p2-p1)),r2((z.p2-p1)/(p2-p1)); if(abs(r1.y)&lt;=eps){//colinear case h1=r1.x; h2=r2.x; if(h1&gt;h2)swap(h1,h2); if(h2&gt;=0&amp;&amp;h1&lt;=1){//add eps h1=max(0.0L,h1);h2=min(1.0L,h2); ans.pb(p1+p1p2*h1); if(doublecompare(h1,h2)==-1)ans.pb(p1+p1p2*h2);}}} else{ h1 = ((p1-z.p1).dot(q1))/((z.p2-z.p1).dot(q1)); pnt q2 = (z.p2-z.p1)*pnt(0,1); h2 = ((z.p1-p1).dot(q2))/((p2-p1).dot(q2)); if(h1+eps&gt;=0&amp;&amp;h1-eps&lt;=1&amp;&amp;h2+eps&gt;=0&amp;&amp;h2-eps&lt;=1) ans.pb(z.p1+(z.p2-z.p1)*h1);}} return ans;} </code></pre>
 

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