Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Raynoko · Sep 22, 2014 at 03:28 PM · vector3lerpdistancepointcenter

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. alt text

screenshot_2.png (12.2 kB)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raynoko · Sep 22, 2014 at 02:37 PM 0
Share

or how can i write some borders for this point, or define area when point can will be.

avatar image Andres-Fernandez · Sep 22, 2014 at 03:59 PM 0
Share

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)

avatar image robertbu · Sep 22, 2014 at 04:13 PM 0
Share

@Raynoko - is the centroid what you are seeking? Note that for a convex polygon, the centroid may be outside of the polygon.

avatar image Raynoko · Sep 23, 2014 at 07:32 AM 0
Share

Currently, i dont know how centroid works, but i need that the point will be always inside, never outside.

avatar image TheCatProblem · Sep 23, 2014 at 01:54 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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. alt text


screenshot_2.png (242.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

http://wiki.unity3d.com/index.php/3d_Math_functions

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mouurusai · Sep 23, 2014 at 03:24 PM 0
Share

Thanks for the link.

avatar image Raynoko · Sep 23, 2014 at 05:07 PM 0
Share

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

avatar image robertbu · Sep 23, 2014 at 05:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Finding Nearest Object Both Positive And Negative? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges