0

What is the maths behind drawing the grid lines of a soccer field observed in perspective?

Say I want to divide (equally in terms of the real soccer field) the width and length of the grid by 3 and 4, respectively.

How do I know the distance proportion between the grid lines? My input is only the four line equations that forms the field.

Input example: Input example

Desired output: enter image description here

(I'm sorry about the drawing, made in Paint but I hope you get the idea behind it)

Any help will be greatly appreciated!

Thank you.

  • The math is called "projective geometry" but you don't need that. Choose some point that is not in the final picture to be the eye of the viewer. Then draw the lines so they all meet in that point. (Of course, you'll only want portions of the the lines in the picture.) Google "perspective drawing" for more information. – saulspatz Jan 31 '18 at 23:55
  • @saulspatz thanks for your reply. I looked it up and it does solve my problem! However, it seems a lot of trouble knowing that the purpose of my question is to code the solution in a project I'm working on. Do you think there's a solution that involves an algebraic solution? Like using the angle/width/length. Thanks again – ihavenoidea Feb 01 '18 at 00:23
  • 1
    Of course there's a solution using algebra. Do you know about the equation of a straight line? – saulspatz Feb 01 '18 at 00:33
  • I do. What I meant is, instead of calculating countless lines and getting their intersection points (my quick search on perspective drawing gave me that) and so on, is there a simpler way to do that? I hope I don't sound lazy by asking this, it's just that my software will require a lot of requests on this solution, so the faster the better. – ihavenoidea Feb 01 '18 at 00:42
  • 1
    Whatever graphics package you’re using likely has a function to compute the planar perspective transformation between two quadrangles (or perhaps the unit square and a quadrangle). Before plunging into writing your own code for this, look for something along those lines in the libraries that you’re using. Drawing an evenly-spaced grid doesn’t really require the full transformation, but this will save you some work and let you draw whatever you want on the image of the field. – amd Feb 01 '18 at 01:15
  • Hi, I agree with @amd. I was going to suggest that you ask questions about computer graphics on a different site, but he's right. Your graphics package probably includes this already. (I remember when computer graphics was just starting out, and you had to do stuff like this yourself. Sometimes I have a tendency to live in the past.) – saulspatz Feb 01 '18 at 01:51
  • After some digging, I found out that JavaFX does have support for perspective transformation (https://docs.oracle.com/javafx/2/api/javafx/scene/effect/PerspectiveTransform.html). Haven't tested it yet but I'm pretty sure it will work. Thank you saulspatz and amd! – ihavenoidea Feb 01 '18 at 03:37
  • Looks like just the thing. – amd Feb 01 '18 at 03:38

1 Answers1

0

Generally speaking, what you’re looking for is a homography—a planar perspective transformation—between the field’s ideal rectangle and its image. I highly recommend that you look through the graphics libraries that you’re using. Many have built-in library functions for constructing such a transformation. That will save you some work and allow you to draw other things besides a grid onto the image as well as map points on the image back to the ideal rectangle, which is handy for selection. If you need to construct this for yourself, this answer has a very nice description of how to do so.

If all you really need to do is draw a grid, though, there’s a possibly simpler method that uses cross-ratios: given four points $a$, $b$, $c$ and $d$ on a line, the quantity $$(a,b;c,d) = {[a,c][b,d]\over[a,d][b,c]}$$ is invariant under projective transformations of the line. (Here, the notation $[p,q]$ denotes the determinant of the matrix with the homogeneous coordinates of points $p$ and $q$ as its rows or columns.) You can use this invariant to map the distance along the edge of the field into a distance in the image.

Let’s say $\lambda$ is the proportion of the distance along a side of the field at which you want to place the end of a grid line. Form the cross-ratio $$(\lambda,0;1,\infty) = {\begin{vmatrix}\lambda&1\\1&1\end{vmatrix} \begin{vmatrix}0&1\\1&0\end{vmatrix} \over \begin{vmatrix}\lambda&1\\1&0\end{vmatrix} \begin{vmatrix}0&1\\1&1\end{vmatrix}} = 1-\lambda.$$ Besides the two endpoints $P_0$ and $P_1$ of the corresponding line segment in the image, you’ll also need the vanishing point $P_\infty$ of the lines parallel to it. This will be the intersection of the extension of the image of the field edge with the extension of the image of the opposite edge. Call the distance between the endpoints $d$ and the distance from the image of the “start” corner to the vanishing point $v$. (Note that $v$ might be negative.) The corresponding cross-ratio for the image is $$(\mu,0;d,v) = {\begin{vmatrix}\mu&1\\d&1\end{vmatrix} \begin{vmatrix}0&1\\v&1\end{vmatrix} \over \begin{vmatrix}\mu&1\\v&1\end{vmatrix} \begin{vmatrix}0&1\\d&1\end{vmatrix}} = {v(\mu-d)\over d(\mu-v)}.$$ Equating the two cross-ratios and solving for $\mu$ gives $$\mu = {d v \lambda \over v+d(\lambda-1)}.$$ Dividing this by $d$ gives you the proportion of the distance between the two endpoints in the image, so the image point that corresponds to the proportion $\lambda$ is $$\left(1-\frac \mu d\right)P_0+\frac \mu d P_1.$$ In particular, if $\lambda=\frac k4$, then this becomes $${(k-4)(d-v) P_0 + k v P_1 \over (k-4)d+4v}.$$

amd
  • 53,693