ID:2070943
 
(See the best response by Popisfizzy.)
Code:
var/global/list/function/functions

/function/standard
var/domain_start = -INFINITY
var/domain_end = INFINITY
var/fx //as in f(x)

/function/reverse //e.g. x=2 or f(x) = x^(1/2) //I am basically pretending the map is sideways so the process will be basically the same
var/domain_start = -INFINITY
var/domain_end = INFINITY
var/fy //as in f(y)

/proc/populate_function_list()
for(var/turf/simulated/wall/W in world)
var/function/standard/F = new/function/standard
F.domain_start = W.x * 32 //These are for the pixel locations. It might be smart to be using px and py
F.domain_end = (W.x + 1) * 32
F.fx = W.y * 32 //Do bottom edge
functions += F
F.fx = (W.y + 1) * 32 //Now the top
functions += F
for(var/turf/simulated/wall/W in world)
var/function/reverse/F = new/function/reverse
F.domain_start = W.x * 32
F.domain_end = (W.x + 1) * 32
F.fy = W.x * 32 //Do left edge
functions += F
F.fy = (W.x + 1) * 32 //Now the right
functions += F


Problem description:
I am trying to define the edges of every single wall (yes, only walls) as 4 functions, all of which are put into that global list at the top. Then I need to find the closest intersections to a certain point. This will ultimately lead into choked flow calculations. Currently what I have going is only for vertical or horizontal lines. How do I expand this to diagonals, or even curves? How should I try and find the closest intersection on either side?
Best response
As an aside, a function f-1 such that f∘f-1 = f-1∘f = id for all inputs is usually called the inverse rather than the reverse. (If only one of the above two holds, then it's called a right-inverse and left-inverse respectively).

How should I try and find the closest intersection on either side?

Assuming that you treat functions f, g as polynomial functions, then if you want to find where they two intersect you simply have to solve for all x such that f(x) - g(x) = 0, where f(x) - g(x) will be another polynomial. If your polynomial functions are degree four or less, then there are formulas for the roots that you can use. Unfortunately, there are no general algebraic solutions for polynomials of degree five or greater, and you would have to take a numerical approach instead.

If you want more general functions than polynomial functions, it's basically guaranteed that finding roots without numerical methods will be a nightmare if not impossible, so the same general idea will apply.

Currently what I have going is only for vertical or horizontal lines. How do I expand this to diagonals, or even curves?

I'm not exactly sure of what approach you're taking, but what seems most natural to me for something like this is to use parametric plane curves and simply specific the domain of your parameter (as you are already doing) as well as the step size you want (e.g., if you step size is Δt and your initial and final values are t0 and t1, then you would evaluate f(t0), f(t0 + Δt), f(t0 + 2Δt), f(t0 + 3Δt), ..., f(t1).

The bonus of this approach is that you can, for example, entirely specify things like circular or elliptical walls with a single function, while with a non-parametric function would require multiple equations. You can, generally speaking, get much more complicated shapes that are much more useful than if you use a non-parametric function. Furthermore, every non-parametric function can be (fairly trivially) represented as a parametric function while the reverse is not necessarily true.

It's essentially going to be a much more helpful and general approach, I'd imagine.

[Edit]

There appears to be a site bug, and that first link won't work. Here is the URL for the curious: https://en.wikipedia.org/wiki/Abel%E2%80%93Ruffini_theorem
I have very little input except a warning:

You are grasping the tail of an O(N2) dragon. That's not an enviable position to be in.
Thanks Popisfizzy