Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've lifted the maths from <a href="http://community.topcoder.com/tc?module=Static&amp;d1=tutorials&amp;d2=geometry2" rel="nofollow">here</a>,</p> <p>Okay so each point has 4 "cardinal rays", a ray being a infinite line that passes between two points.</p> <pre><code>// A line in the form Ax+By=C from 2 points public struct Ray { public readonly float A; public readonly float B; public readonly float C; public Ray(PointF one, PointF two) { this.A = two.y - one.y; this.B = one.x - two.x; this.C = (this.A * one.x) + (this.B * two.x); } } </code></pre> <p>To get the cardinals we can extend <code>PointF</code></p> <pre><code>private readonly SizeF NS = new SizeF(0.0F, 1.0F); private readonly SizeF EW = new SizeF(1.0F, 0.0F); private readonly SizeF NESW = new SizeF(1.0F, 1.0F); private readonly SizeF NWSE = new SizeF(-1.0F, 1.0F); public static IEnumerable&lt;Ray&gt; GetCardinals(this PointF point) { yield return new Ray(point + NS, point - NS); yield return new Ray(point + EW, point - EW); yield return new Ray(point + NESW, point - NESW); yield return new Ray(point + NWSE, point - NWSE); } </code></pre> <p>To find the inersection of two rays we can do</p> <pre><code>static PointF Intersection(Ray one, Ray two) { var delta = (one.A * two.B) - (two.A * one.B); if (delta == 0.0F) { //Lines are parallel return PointF.Empty; } else { var x = ((two.B * one.C) - (one.B * two.C)) / delta; var y = ((one.A * two.C) - (two.A * one.C)) / delta; return new PointF(x, y); } } </code></pre> <p>So, to get the intersections for the cardinals of two points,</p> <pre><code>public static IEnumerable&lt;PointF&gt; GetCardinalIntersections( this PointF point, PointF other); { return point.GetCardianls().SelectMany(other.GetCardinals(), Intersection) .Where(i =&gt; !i.IsEmpty()); } </code></pre> <p>Which then enables,</p> <pre><code>public static IEnumerable&lt;PointF&gt; GetCardinalIntersections( this PointF point, IEnumerable&lt;PointF&gt; others); { return others.SelectMany((o) =&gt; point.GetCardinalIntersections(o)); } </code></pre> <p>We can then use this functionality like this.</p> <pre><code>var point = new PointF(1.0F, 1.0F); var others = new [] { new PointF(2.0F, 5.0F), new PointF(-13.0F, 32.0F) }; var intersections = point.GetCardinalIntersections(others); </code></pre> <p>Obviously there is lots of iteration here, I haven't compiled or tested this but since, at its nub, the maths seems fairly efficient I'm optimistic about performance.</p>
    singulars
    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.
 

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