Representing 2D line as two variables, all cases

1.9k Views Asked by At

Is it possible to represent a line in the general case using only two variables (namely floats or doubles)? By this I mean being able to convert a line into those two variables and successfully converting them back without loss of data or the algorithm failing for specific cases.

I know it can be done with three, since any line can be represented as the coefficients in Ax + By = C, but I can't represent a line using two using the coefficients in Ax + By = 1, since this representation produces infinite values for A or B if the line passes through the origin, and I can't use the coefficients m and b in y = mx+b since m becomes infinite for vertical lines.

5

There are 5 best solutions below

0
On BEST ANSWER

@MBo's answer is quite optimal I think, unless you really don't want to deal with costly trigonometry, or if you don't want the problems with angle periodicity.

However, there is one more thing you can do. You can always squeeze infinities into finite range. Any monotonous function with two horizontal asymptotes (like arctan, tanh, or something like that) can be used to do that. When I'm dealing with positive values only, I like

$$a=\frac{A}{A+1}$$ and its inverse $$A=\frac{a}{1-a}$$

This transformation maps all positive reals including infinity to $[0,1]$. For $a\geq 0$ and $b\geq 0$, cross-multiply to remove fractions and write $$a(1-b)x+b(1-a)y=(1-a)(1-b)$$ which handles both infinite cases (when $a=1$ or $b=1$).

It is a bit more clumsy when you want to handle signed numbers: $$a=\frac{A}{|A|+1}$$ and the inverse: $$A=\frac{a}{1-|a|}$$

It follows $$a(1-|b|)x+b(1-|a|)y=(1-|a|)(1-|b|)$$

This represents all your lines symmetrically with numbers $-1\leq a,b\leq 1$, and requires one division per dimension for conversion, and only multiplication to get the equation.

As a bonus, you can also plot your lines on a unit square and see how the boundaries of the square represent the lines through the center, coordinate axes are vertical and horizontal lines, and the coordinate center is the line at infinity.

You need a special case for $a=b=\pm 1$: you can still represent them, but the equation is zero unless you treat the case specially and just fall back to $ax+by=0$. One if should do it. Unfortunately, the mapping is still ill-conditioned around that point (very small values in the equation). You avoid that if you map to a circle instead of a square.

It's always useful for programming to squeeze infinities into a finite range. It's useful for plotting infinite maps because then you never miss anything that gets out of range, while the center is not squeezed too much.

For mathematical purposes, it's better to preserve symmetry and map everything to the unit circle instead of square:

$$R=\sqrt{A^2+B^2}$$ $$a=\frac{A}{R+1}$$ $$b=\frac{B}{R+1}$$ and the inverses: $$r=\sqrt{a^2+b^2}$$ $$A=\frac{a}{1-r}$$ $$B=\frac{b}{1-r}$$

This avoids the corner problems and treats all the directions equally.

0
On

Yes, it is possible to represent any line with only two variables:
p is the length of normal from coordinate origin to line
Theta is angle between OX-axis and direction of normal. Equation:

x*Cos(Θ) + y*Sin(Θ) - p = 0

enter image description here

To transform canonical equation of line to normal form:

A*x+B*y+C=0
D=Sqrt(A^2+B^2)
Sgn = Sign(C) (-1,0,+1)
p=Sign*C/D  //always positive
Cos(Theta)=Sgn*A/D 
Sin(Theta)=Sgn*B/D 

or

p=Abs(C)/D
Theta=atan2(B,A) (clamped to range [0..Pi))

another relation:

Fi = Theta - Pi/2

where tg(Fi)=k in equation y=kx+b

1
On

@MBo's answer - it's the Hessian Normal Form of a line.

In this form a point $\mathbf{r} = (x,y)$ on the line satisfies $\mathbf{r}.\mathbf{\hat{n}} - p = 0$, where $\mathbf{\hat{n}}$ is a unit normal to the line and $p$ is the perpendicular (closest) distance from the line to the origin.

The unit normal $\mathbf{\hat{n}} = (\cos{\phi}, \sin{\phi})$, where $\phi = \arctan{x/y}$. The distance $p$ is a scalar. So the line can be represented by two parameters, $\phi$ and $p$.

See this example

Incidentally, the HNF of a line is used in image processing to detect straight line segments at arbitrary orientations (the $y=mx+c$ form would fall over for vertical lines). This is an application of the Hough Transform

2
On

The general approach in computational geometry has been skepticism toward "minimal representation", and rather be looking for numerical representations that present the fewest exceptions needing to be handled (for more streamlined code).

In addition, from an object-oriented software standpoint, when we utilize vector-based objects in representations, the cognitive load is based on the number of objects in the representation, not the number of scalars buried in the objects.

Based on that thinking, a straightforward representation of a 2D line, stated as a vector predicate is:

p • o == L

Where p = [ x y] is any point on the line, o is the line's orientation (direction vector (unit vector) pointing perpendicular to run direction of line:

o <-- ( dy , -dx) normalized

and L is the location of the line, given as a coordinate along the 2D rotated axis pointing out direction o.

The beauty of this approach is that every line may be represented numerically by two features, orientation o and location L, where both have a clear geometric meaning that can be sketched or visualized.

0
On

Getting back to the original question, there are other criteria for considering alternate representations of a 2D line, besides minimal representation (consuming the fewest scalar variables). Here are some to be considered:

a) Does the representation offer 1:1? (I.e., there is only one set of numbers possible for representing a unique 2D line, and only one 2D line specified by each unique ensemble of numbers). This feature is very important in algorithmic math, and streamlines the mental process in analysis.

b) Does the representation offer continuity? (I.e., two 2D lines that are very close to being identical have numbers representing them that are also very close). This is an essential aspect of algorithmic math, since the computer obliges us to work with a finite-precision real number system. Practically, when comparing two geometric objects for equality (and allowing some numerical computation in arriving at the object(s)), we have to allow a tiny epsilon when comparing for identical-objects. If the numerical representations embed discontinuities (two almost identical objects have widely divergent numbers representing them), then additional code must be designed to manage this discontinuity. A representation offering continuity streamlines the code development process.

c) Scale-up to higher dimensions. Does the manner of representing a 2D line suggest a similar representation for 3D (either for a plane or a 3D extended line)?

d) Completeness. Does the representation handle all cases uniformly (without exceptions)? The headline question appeals to this feature.

Once you survey all 4 of these criteria, minimalist representations lose their luster. For example, representing a 2D line as orientation angle theta and distance from the origin (theta, dist) comes up short. There is an unavoidable discontinuty regarding the orientation angle...if you force a 1:1 representation by insisting 0 <= theta < 2*pi, then you end up with two orientations very close to being equal, one with o = 0, the other with o = 6.28. The informatics perspective regards the directional information (the orientation of the 2D line) as being overcompressed forced to reside in just one scalar variable.

By comparison, if we allow the 2D line's orientation to be spread across two scalars, as is the case when using a direction vector (unit vector) to represent orientation, things improve. We achieve a 2:1 representation (where orientation angle left us with infinite:1 representation). This is because any 2D line has two possible orientation vectors (pointing opposite each other). The [ orientation , Location ] form suggests reuse in 3D, as a way to represent a plane:

vector equation of 2D line: p • o == L

vector equation of 3D plane: p • o == L

The essential problem in 21st century math is how best to use numbers to represent real world systems, suitable for implementation in reliable, robust, proveably-correct software code. From this standpoint, choosing a representation based on how tightly it compacts information is getting off on the wrong foot. The modern approach is to ask, which representation will support the most streamlined, simple algorithms, and offer cognitive extensibility.