Ray-casting algorithm: Difference between revisions

Content added Content deleted
(Updated D entry)
Line 491: Line 491:
if (p.x < min(a.x, b.x))
if (p.x < min(a.x, b.x))
return true;
return true;
immutable blue = (abs(a.x - p.x) > double.min) ?
immutable blue = (abs(a.x - p.x) > double.min_normal) ?
((p.y - a.y) / (p.x - a.x)) :
((p.y - a.y) / (p.x - a.x)) :
double.max;
double.max;
immutable red = (abs(a.x - b.x) > double.min) ?
immutable red = (abs(a.x - b.x) > double.min_normal) ?
((b.y - a.y) / (b.x - a.x)) :
((b.y - a.y) / (b.x - a.x)) :
double.max;
double.max;
Line 505: Line 505:


void main() {
void main() {
enum Figure[] polys = [
immutable Figure[] polys = [
{"Square", [
{"Square", [
{{ 0.0, 0.0}, {10.0, 0.0}}, {{10.0, 0.0}, {10.0, 10.0}},
{{ 0.0, 0.0}, {10.0, 0.0}}, {{10.0, 0.0}, {10.0, 10.0}},
Line 524: Line 524:
{{ 3.0, 10.0}, { 0.0, 5.0}}, {{ 0.0, 5.0}, { 3.0, 0.0}}]}
{{ 3.0, 10.0}, { 0.0, 5.0}}, {{ 0.0, 5.0}, { 3.0, 0.0}}]}
];
];
enum Point[] testPoints = [{ 5, 5}, {5, 8}, {-10, 5}, {0, 5},
immutable Point[] testPoints = [{ 5, 5}, {5, 8}, {-10, 5}, {0, 5},
{10, 5}, {8, 5}, { 10, 10}];
{10, 5}, {8, 5}, { 10, 10}];


foreach (poly; polys) {
foreach (immutable poly; polys) {
writefln(`Is point inside figure "%s"?`, poly.name);
writefln(`Is point inside figure "%s"?`, poly.name);
foreach (p; testPoints)
foreach (immutable p; testPoints)
writefln(" (%3s, %2s): %s", p.x, p.y, contains(poly, p));
writefln(" (%3s, %2s): %s", p.x, p.y, contains(poly, p));
writeln();
writeln;
}
}
}</lang>
}</lang>
{{out}}
Output:<pre>Is point inside figure "Square"?
<pre>Is point inside figure "Square"?
( 5, 5): true
( 5, 5): true
( 5, 8): true
( 5, 8): true