Ray-casting algorithm: Difference between revisions
Content added Content deleted
m (added whitespace to the task's preamble.) |
(javascript) |
||
Line 1,675: | Line 1,675: | ||
true false false false true true false |
true false false false true true false |
||
true true false false true true false </pre> |
true true false false true true false </pre> |
||
=={{header|Java}}== |
|||
<lang javascript> |
|||
/** |
|||
* @return {boolean} true if (lng, lat) is in bounds |
|||
*/ |
|||
function contains(bounds, lat, lng) { |
|||
//https://rosettacode.org/wiki/Ray-casting_algorithm |
|||
var count = 0; |
|||
for (var b = 0; b < bounds.length; b++) { |
|||
var vertex1 = bounds[b]; |
|||
var vertex2 = bounds[(b + 1) % bounds.length]; |
|||
if (west(vertex1, vertex2, lng, lat)) |
|||
++count; |
|||
} |
|||
return count % 2; |
|||
/** |
|||
* @return {boolean} true if (x,y) is west of the line segment connecting A and B |
|||
*/ |
|||
function west(A, B, x, y) { |
|||
if (A.y < B.y) { |
|||
if (y < A.y || y > B.y || |
|||
x >= A.x && x >= B.x) { |
|||
return false; |
|||
} else if (x < A.x && x < B.x) { |
|||
return true; |
|||
} else { |
|||
return (y - A.y) / (x - A.x) > (B.y - A.y) / (B.x - A.x); |
|||
} |
|||
} else { |
|||
return west(B, A, x, y); |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |