# Given a 3D grid-based map system, how to find the middle point of an edge of the mesh of a tile on the grid

I have a grid-based map system. The grid is aligned to the X and Z axes. The tiles on said map are 3-dimensional, but flat, and can be located anywhere on the Y-axis. If I need to get a Vector3 of the middle point of any extremity edge of the mesh of any given tile, how would I do that?

Update: adding further detail to elaborate further,

being as I am trying to find the middle point of 2 axes ON the edge of the mesh, it cannot be the average of all vertices on the edge. Example below: Taking the above polygon as the example, assume that we are trying to find the middle point of the left-most edge (the edge with the smallest x value).

There are 3 vertices on this edge: (0, 0, 0) (0, 0, -0.52) (0, 0.3, -1)

Being as we are trying to find the point on a given edge, we can solve for 2 axes fairly easily. The X-axis will be at 0, being as we have chosen this edge. The Z-axis will be the average of the smallest and the largest vertices divided by 2 for the middle point of this axis. But the Y axis is going to vary for every possible permutation. If we took the average of all of our given Y values in this example, the result would be 0.1 - however, looking at the example, I know that the desired result should be 0, because I am searching for the middle point on the mesh edge, and at 0x, -0.5z, the edge is at 0y.

How do I solve this for any given potential problem like this?

• This sounds like it would just be a matter of averaging the Vector3 values of the vertices. I assume there's some reason that's not giving you what you want? Can you elaborate on what makes this unsuitable? Dec 22, 2021 at 7:19
• To tell you the truth, I haven't tried that. Maybe I'm just mistaken but I had assumed that the average of all vertices on an edge would not necessarily be a point that is on the mesh at all Dec 22, 2021 at 19:38
• If your polygons are planar, then any weighted average of points on that polygon will also be in that polygon. And any 3 points of your quad will form a planar triangle, even if the quad itself is non-planar. Give it a try and if it works for you, write up your solution as an answer below. If it doesn't work, edit your question to explain how it departs from what you want and we can help you close the remaining gap. Dec 22, 2021 at 19:41
• Question updated Dec 22, 2021 at 22:00
• You say your grid is aligned to the X and Z axis. Presumably you can compute the X and Z of the middle of the axis, and then intersect a vertical line with those coordinates with the plane of the quad (since you say it is flat). Is that what you want? Edit: by the way, if the example in the second picture is a single quad subdivided, that does not seem flat. No, I don't mean horizontal, I mean the four vertex that made the perimeter of the quad do not seem coplanar. Dec 22, 2021 at 22:32

From the picture I understand you want the point highlighter in green. Yet the tile has three vertex on that side instead of two.

Taking your example data, the vertex coordinates are:

p1.x = 0;   p1.y = 0;   p1.z = 0;
p2.x = 0;   p2.y = 0;   p2.z = -0.52;
p3.x = 0;   p3.y = 0.3; p2.z = -1;


Since we are looking at a side where x = 0. We know the solution must also have x = 0. Alright, let us get rid of the x column:

p1.y = 0;   p1.z = 0;
p2.y = 0;   p2.z = -0.52;
p3.y = 0.3; p2.z = -1;


Now, the side we are looking at goes from z = 0 to z = -1. Thus, the solution must have z = -0.5, correct?

So, where does that fall? z = -0.5 is between p1.z = 0 and p2.z = -0.52. So, we can get rid of the rest of rows:

p1.y = 0;   p1.z = 0;
p2.y = 0;   p2.z = -0.52;


If you have more vertex along the side, organize them by the relevant axis (z in this case), find between which two the value you have is, and then get rid of the rest. The point we are looking for is between those two vertex.

Now, how far along is z = -0.5 is between p1.z = 0 and p2.z = -0.52?

Well, let us see:

w = (z - p1.z)/(p2.z - p1.z)

=>

w = (-0.5 - 0)/(-0.52 - 0)

=>

w = (-0.5)/(-0.52)

=>

w = 0.5/0.52 = 0.961538461538


Our z = -0.5 is about 96% along the way from p1.z = 0 to p2.z = -0.52. So we want a point that is about 96% between p1 and p2, we can do this with linear interpolation. Let us use that to interpolate the y values, and we have our y:

y = p1.y + w * (p2.y - p1.y)

=>

y = 0 + w * (0 - 0)

=>

y = 0


Yeah, it is y = 0, as expected.

• Thank you so much!! Dec 22, 2021 at 23:24