How to resolve the algorithm Ray-casting algorithm step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ray-casting algorithm step by step in the Wren programming language

Table of Contents

Problem Statement

Given a point and a polygon, check if the point is inside or outside the polygon using the ray-casting algorithm. A pseudocode can be simply: Where the function ray_intersects_segment return true if the horizontal ray starting from the point P intersects the side (segment), false otherwise. An intuitive explanation of why it works is that every time we cross a border, we change "country" (inside-outside, or outside-inside), but the last "country" we land on is surely outside (since the inside of the polygon is finite, while the ray continues towards infinity). So, if we crossed an odd number of borders we were surely inside, otherwise we were outside; we can follow the ray backward to see it better: starting from outside, only an odd number of crossing can give an inside: outside-inside, outside-inside-outside-inside, and so on (the - represents the crossing of a border). So the main part of the algorithm is how we determine if a ray intersects a segment. The following text explain one of the possible ways. Looking at the image on the right, we can easily be convinced of the fact that rays starting from points in the hatched area (like P1 and P2) surely do not intersect the segment AB. We also can easily see that rays starting from points in the greenish area surely intersect the segment AB (like point P3). So the problematic points are those inside the white area (the box delimited by the points A and B), like P4. Let us take into account a segment AB (the point A having y coordinate always smaller than B's y coordinate, i.e. point A is always below point B) and a point P. Let us use the cumbersome notation PAX to denote the angle between segment AP and AX, where X is always a point on the horizontal line passing by A with x coordinate bigger than the maximum between the x coordinate of A and the x coordinate of B. As explained graphically by the figures on the right, if PAX is greater than the angle BAX, then the ray starting from P intersects the segment AB. (In the images, the ray starting from PA does not intersect the segment, while the ray starting from PB in the second picture, intersects the segment). Points on the boundary or "on" a vertex are someway special and through this approach we do not obtain coherent results. They could be treated apart, but it is not necessary to do so. An algorithm for the previous speech could be (if P is a point, Px is its x coordinate): (To avoid the "ray on vertex" problem, the point is moved upward of a small quantity   ε.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ray-casting algorithm step by step in the Wren programming language

Source code in the wren programming language

import "/fmt" for Fmt

class RayCasting {
    static intersects(a, b, p) {
        if (a[1] > b[1]) return intersects(b, a, p)
        if (p[1] == a[1] || p[1] == b[1]) p[1] = p[1] + 0.0001
        if (p[1] > b[1] || p[1] < a[1] || p[0] >= a[0].max(b[0])) return false
        if (p[0] < a[0].min(b[0])) return true        
        var red  = (p[1] - a[1]) / (p[0] - a[0])
        var blue = (b[1] - a[1]) / (b[0] - a[0])
        return red >= blue
    }

    static contains(shape, pnt) {
        var inside = false
        var len = shape.count
        for (i in 0...len) {
            if (intersects(shape[i], shape[(i + 1) % len], pnt)) inside = !inside
        }
        return inside
    }

    static square { [[0, 0], [20, 0], [20, 20], [0, 20]] }

    static squareHole { [[0, 0], [20, 0], [20, 20], [0, 20], [5, 5], [15, 5], [15, 15], [5, 15]] }

    static strange { [[0, 0], [5, 5], [0, 20], [5, 15], [15, 15], [20, 20], [20, 0]] }

    static hexagon { [[6, 0], [14, 0], [20, 10], [14, 20], [6, 20], [0, 10]] }

    static shapes { [square, squareHole, strange, hexagon] }
}

var testPoints = [[10, 10], [10, 16], [-20, 10], [0, 10], [20, 10], [16, 10], [20, 20]]
for (shape in RayCasting.shapes) {
    for (pnt in testPoints) Fmt.write("$7s ", RayCasting.contains(shape, pnt))
    System.print()
}

  

You may also check:How to resolve the algorithm Diversity prediction theorem step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Fusc sequence step by step in the Delphi programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the OCaml programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the POV-Ray programming language
You may also check:How to resolve the algorithm Create a file step by step in the Groovy programming language