- Home /
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.
or how can i write some borders for this point, or define area when point can will be.
You are probably looking for the centroid. Did a fast google check and found some answers that I think may help you. Check here, here, here and here (and also here for triangles).
[Edit] Although it seems to me that you just want the middle point of the segment that divides the 4 points into 2 triangles... (but that's just a thought out of yout picture)
@Raynoko - is the centroid what you are seeking? Note that for a convex polygon, the centroid may be outside of the polygon.
Currently, i dont know how centroid works, but i need that the point will be always inside, never outside.
If the centre point always needs to be inside the polygon then the centroid won't work for you in some cases (as robertbu pointed out). Are there any limitations on how the four points might be arranged? If so, it's possible an "external centroid" case will never occur.
Otherwise, you'll need to define what you mean by centre point. You might consider the suggestion made by Andres Fernandez, although the midpoint of the interior edge will be biased for dart-shaped polygons (by which I mean those similar in form to the second and third shapes in your drawing) with one prong that's substantially longer and/or thicker than the other.
Answer by Raynoko · Sep 23, 2014 at 05:40 PM
my problem solved one package "Polydraw", when i rewrite one of all skript and aplly to my project. Polydraw 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.
Answer by TheCatProblem · Sep 22, 2014 at 04:02 PM
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 Vector3
s, 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.
Answer by robertbu · Sep 23, 2014 at 02:35 PM
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.
thank you for very helpful advice, but i was used some package, which solved my problem. This link i probably use in near future. Thank you
If you found a package that solved your problem, consider mentioning the package and how you used it as an answer and mark your answer as answered (click the checkmark on the left of your answer).
Your answer
Follow this Question
Related Questions
Closest point on multiple lines? 2 Answers
Vector3.Lerp - Constant speed between distance changes 3 Answers
How to update interval between objects in run time? 0 Answers
Fixed distance from point 2 Answers