Find center between 4 points

Hi, im trying to find center point between 4 points. i was trying Vector3.Lerp, Distance, Cross, but nothing solved my problem. I need that center point will be everytime in center (between 4 points).
Anybody idea how to do this?
for better understading look at picture.

The centre of a group of points at arbitrary locations in 2D or 3D space is usually referred to as the centroid. This is computed by averaging the location of the points in each coordinate (x, y, and z).

Assuming you have N points (4 in your case) stored as Vector3s, just add them all together (vectors are added component-wise in Unity, as they should be) and divide the result by N to obtain the coordinates of the centroid point.

I’m not sure what to call the center you are looking for, so it is hard to search out answers. Here is a back-of-the-envelope calculation that gets you a point somewhere close to what you are looking for. Since it is heuristically derived, there may be situations that I have not envisioned.

Given points A,B,C, and D:

  • Find the midpoint of both AC and BD.
  • Test the midpoints to see which one is inside the polygon

At the end of this step, you will have something that works pretty well for the figures in your drawing, but it does not work as well for other cases.

  • Take the segment with with the point inside, and rotate it 90 degrees around its midpoint to produce segment EF (or as likely vector midpoint/F).
  • Find the intersection points between the line defined by EF and the lines formed by the other four segments.
  • Use the midpoint between the two intersection points.

While I’m not going to go into detail on how to handle them, there are three degenerate cases you need to plan for when using an arbitrary polygon. The first is when three points are on a line and therefore the figure is a triangle. The second is when all four points are on a line and therefore the resulting figure is a line. The third is when walking the points produces two areas instead of one (i.e. hourglass shape).

  • You can determine whether a point is inside by walking the segments in order and seeing what side of the line formed by the segment the point is on. If the point is on the same side for all segments, then it is inside. Different side for any segment, and it is outside.
  • A vector can easily be rotated by multiplying it by a Quaternion.
  • Mathf3D in the Unity Wiki has routines for figuring out line/line intersection.

my problem solved one package “Polydraw”, when i rewrite one of all skript and aplly to my project.
Im trying to create 4 wall with floor. But walls are moveable, and floor must be adaptable for this wall.
and “center point” means point, witch will be always between 4 walls, center point for floor.
But this package help me without center point, because plane recalculate polygons, and never happens error. Error i mean, that floor is always inside walls.