Ray-casting algorithm: Difference between revisions

Updated D code
(Adding coffeescript version - will add testing code tomorrow(?) when I modify it to use the same as other examples)
(Updated D code)
Line 474:
<lang d>import std.stdio, std.math, std.algorithm, std.conv;
 
immutable struct Figure { string name; Edge[] edges; }
string name;
struct Edge { Point a, b; } // Figure edge from a to b
Edge[] edges;
struct Point { double x, y; }
}
immutable struct Edge { Point a, b; } // Figure edge from a to b
immutable struct Point { double x, y; }
 
bool contains(/*in*/ Figure poly, in Point p) pure nothrow {
static bool raySegI(in Point p, in Edge edge) pure nothrow {
enum double epsilon = 0.00001;
with (edge) {
if (a.y > b.y) swap(a, b);
//swap(a, b); // if edge is mutable
return raySegI(p, Edge(b, a));
if (p.y == a.y || p.y == b.y)
//p.y += 0.00001epsilon; // epsilonif p is mutable
if return raySegI(Point(p.y > b.y ||x, p.y <+ a.yepsilon) || (p.x > max(a.x, b.x))edge);
if (p.y > b.y || p.y < a.y || p.x > max(a.x, b.x))
return false;
if (p.x < min(a.x, b.x))
return true;
constimmutable blue = (abs(a.x - p.x) > double.min) ?
((p.y - a.y) / (p.x - a.x)) :
double.max;
constimmutable red = (abs(a.x - b.x) > double.min) ?
((b.y - a.y) / (b.x - a.x)) :
double.max;
return blue >= red;
}
}
 
// cast() workaround
return count!((e){ return raySegI(p,e); })(cast()poly.edges) % 2 == 1;
}
 
Anonymous user