Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sounds like you have your solution, which is great. I have some suggestions for improving it. </p> <p>The method has a major usability problem, in that it is very confusing to understand (1) what the parameters going in mean, and (2) what the results coming out mean. Both are little puzzles that you have to figure out if you want to use the method.</p> <p>I would be more inclined to use the type system to make it much more clear what this method does.</p> <p>I'd start by defining a type -- perhaps a struct, particularly if it was going to be immutable -- called LineSegment. A LineSegment consists of two PointF structs representing the end point. </p> <p>Second, I would define an abstract base type "Locus" and derived types EmptyLocus, PointLocus, LineSegmentLocus and perhaps UnionLocus if you need to represent the locus that is the union of two or more loci. An empty locus is just a singleton, a point locus is just a single point, and so on.</p> <p>Now your method signature becomes much more clear:</p> <pre><code>static Locus Intersect(LineSegment l1, LineSegment l2) </code></pre> <p>This method takes two line segments and computes the locus of points that is their intersection -- either empty, a single point, or a line segment.</p> <p>Note that you can then generalize this method. Computing the intersection of a line segment with a line segment is tricky, but computing the intersection of a line segment with a point, or a point with a point, or anything with the empty locus is <em>easy</em>. And it's not hard to extend intersection to arbitrary unions of loci. Therefore, you could actually write:</p> <pre><code>static Locus Intersect(Locus l1, Locus l2) </code></pre> <p>And hey, now it becomes clear that Intersect could be an extension method on locus:</p> <pre><code>static Locus Intersect(this Locus l1, Locus l2) </code></pre> <p>Add an implicit conversion from PointF to PointLocus and LineSegment to LineSegmentLocus, and you can say things like</p> <pre><code>var point = new PointF(whatever); var lineseg = new LineSegment(somepoint, someotherpoint); var intersection = lineseg.Intersect(point); if (intersection is EmptyLocus) ... </code></pre> <p>Using the type system well can massively improve the readability of a program.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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