Anonymous user
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;
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(
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); // if edge is mutable
return raySegI(p, Edge(b, a));
if (p.y == a.y || p.y == b.y)
//p.y +=
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;
((p.y - a.y) / (p.x - a.x)) :
double.max;
((b.y - a.y) / (b.x - a.x)) :
double.max;
return blue >= red;
}
}
// cast() workaround
return count!((e){
}
|