# How would I determine which lines are closer for perspective projection from 2d to 1d

## Recommended Posts

I am creating a bit of a weird program, but the basic premise is that I have defined a 2 dimensional scenario and I want to project it (using perspective projection) onto a 1 dimensional viewing line that is directly in front of the player. This is shown in the first picture. I have this working for a single line, but I would like to add as many lines as I want and make them display in a way that makes sense. This means that lines that are in front of other lines need to appear that way. The problem I have is more of a math question, but basically the question is: given a location of the player, and some set of lines defined by 2 points each, how would I determine in which order to project the lines to the viewing line?

In the second image, the program needs to determine to project line A onto the viewing line first. This allows B, when projected, to cover up the part of A that doesn't need to be seen. I cannot think of a way to determine which lines to project first, because for every idea I think of, I can come up with an example that it will not work with. For example, if we just do some sort of distance formula to let's say, the midpoint of every line, that will not work for something like in the third picture. In this case, B should be projected first so A can cover up the part of B.

Whatever the solution is, it must also work for any amount of lines in front of the player, so I just need the order in which to display the lines. Thank you.

##### Share on other sites

In your last example, some parts of B are in front of A, depending on the section you're looking at. Also think of 2 lines that cross each other, there is no telling which one is in front of the other. So in the general case there is no absolute answer.

What is usually done (for example in BSP trees) is to split everything along the overlap and treat it like separate sections.

Hope this helps.

##### Share on other sites

You're working in 1D.  You shouldn't need to worry about performance, so you can just consider each pixel separately.  Just cast a ray for each pixel against each line segment, then sort your hits.  Sorting individual points is a lot easier than sorting line segments.

##### Share on other sites

Two other solutions:

1) If your eye is at E, the segment AB is "behind" the segment CD if the segment CD intersects the triangle ABE. If your segments don't intersect, that's all you need. If they do intersect, break them into smaller pieces at the intersection points.

2) Use a z-buffer.

##### Share on other sites

Thank you alvaro. That makes a lot of sense with the triangles and I will look into z buffers. And with how I'm defining the lines, they never need to intersect so that won't be a problem.

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26