11

Given an image of a 2-dimensional irregular (non-convex) shape, how would I able to compute all the ways in which it could lie stable on a flat surface? For example, if the shape is a perfect square rectangle, then it will surely have 4 ways in which it is stable. A circle on the other hand either has no stable orientation or every point is a stable orientation.

EDIT: There's this nice little game called Splitter (Beware, addictive game ahead) that seems close to what I want. Noticed that you cut a piece of the wood out it would fall to the ground and lay in a stable manner.

EDIT: In the end, the approach I took is to compute center of mass (of the shape) and compute the convex hull (using OpenCV), and then loop through every pair of vertices. If the center of mass falls on top of the line formed by the 2 vertices, it is deemed stable, else, no.

Hao Wooi Lim
  • 3,928
  • 4
  • 29
  • 35
  • What does "stable" mean? – James McNellis Jun 19 '10 at 03:43
  • That's the tricky part. I've been thinking about this as well. I could only give examples. For a perfectly round circle, basically you have infinite number of ways in which it's "stable", but for certain shapes, say a triangle, there would be only 3 because it has only 3 flat surfaces. – Hao Wooi Lim Jun 19 '10 at 03:48
  • It may be fun to consider it from a physics point of view. Consider your surface, find the density of your object, then find it's center of gravity, and then decide how gravity (and friction) will affect it as it is placed. – Noon Silk Jun 19 '10 at 03:52
  • @Hao: So, the irregular shape (polygon?) is placed perpendicular to the flat surface? You need to define the expected results, including the tricky corner cases (pun intended), otherwise answers will inevitably not meet your requirements. – James McNellis Jun 19 '10 at 03:54
  • I think what the poster means is, if you take the shape and set it down, will it fall over? If not, it's stable. The question is, how many ways can you set it down such that it doesn't fall over? – Joey Adams Jun 19 '10 at 04:08
  • 2
    I defined "stable" as "if you perturb it, it will return". Not every triangle will have three stable positions, some have only two. And in my analysis, I said that that a circle had no stable positions, but that's just a matter of nomenclature; change my "greater than" to "greater than or equal to", and there are infinitely many "stable" positions. – Beta Jun 19 '10 at 04:09
  • @Beta: If by "perturb", you mean an infinitesimal nudge, then the only triangles with 2 stable positions would be those with a side of zero length and two sides of non-zero length. Right? – Joey Adams Jun 19 '10 at 04:15
  • @Joey Adams: nope, try (2,9,10). – Beta Jun 19 '10 at 05:07
  • 2
    After someone's answer gets the big green checkmark, can we make this a code golf question? =D – Tyler Jun 19 '10 at 05:22

3 Answers3

10

First find its center of mass (CM). A stable position is one in which the CM will be higher if you make a slight rotation. Now look at the hull, the smallest convex region that encloses the shape:

Convex Hull and Centre of Mass
(source: walkytalky.net)

If the hull is a polygon, then a stable position is one in which the shape is resting on one of the sides, and the CM is directly over that side (not necessarily over the midpoint of the side, just somewhere over it.

If the hull has curves (that is, if the shape has curves which touch the hull), they must be give special treatment. The shape will be stable when resting on a curved edge iff the CM is directly above the lowest point of the curve, and the radius of the curve at that point is greater than the height of the CM.

Examples:

  1. A rectangle. The hull is simply the rectangle, and the CM is at the center. The shape is stable on each of the four sides.
  2. A rectangle with the sides hollowed, but the corners still intact. The hull is still the original rectangle, and the CM is close to where it used to be. All four sides of the hull are still stable (that is, you can still rest the shape on any two corners).
  3. A circle. The CM is in the center, the hull is the circle. There are no stable positions, since the radius of the curve is always equal to the height of the CM. Give it a slight touch, and it will roll.
  4. An ellipse. The CM is at the center, the hull is the shape. Now there are two stable positions.
  5. A semicircle. The CM is somewhere on the axis of symmetry, the hull is the shape. Two stable positions.
  6. A narrow semicircular crescent. The hull is a semicircle, the CM is outside the shape (but inside the hull). Two stable positions.

Illustration of the examples
(source: walkytalky.net)

(The ellipse position marked with an X is unstable, because the curvature is smaller than the distance to the centre of mass.)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Beta
  • 96,650
  • 16
  • 149
  • 150
  • 1
    Since I *do* have some drawing tools, I've run up a couple of quick figures to illustrate the [convex hull](http://walkytalky.net/extern/hull.png) and the stable positions (plus one unstable, for the little end of an ellipse) from [your examples](http://walkytalky.net/extern/stable.png). BTW, you take CM and hull calculations as read. This may be fair enough for polygons, but I'm curious as to whether there are efficient ways to calculate the CM and hull for shapes including curves such as Beziers? – walkytalky Jun 19 '10 at 23:17
  • @tom10: well, no, look at my ellipse example. Also, if the resting points are *all on one side* of the CM, then the position is not stable. – Beta Jun 19 '10 at 23:26
  • @walkytalky: oh, *very* nice! (Although I think the dotted circles are swapped for the two ellipses.) May I embed these? As for finding the CM and hull of complex shapes, I'd fall back on integral calculus and number-crunching. Bezier curves may have tidy exact solutions for all I know, but there are certainly shapes that don't. – Beta Jun 19 '10 at 23:43
  • 1
    @tom10 - Beta was talking about the (convex) hull. If you take the hull of the original shape, all "resting points" get converted into sides. There are no special cases--Beta covered _all_ cases by first performing the hull operation. – Rex Kerr Jun 20 '10 at 05:04
  • @Beta: You may certainly embed. The dotted circles on the ellipses were an attempt to illustrate where the curvature lies with respect to the distance from the CM. Treating the latter as a radius seemed easier to depict, but I can change it. Actually, perhaps having both would be preferable. I'll give it a go. – walkytalky Jun 20 '10 at 09:28
  • @Beta and Rex - I either misread the question or it was edited, either way, my previous comments are simply a distraction, so I've removed them. – tom10 Jun 20 '10 at 23:47
  • Can you explain the physics of this? Why is it like this? – user3180 Jan 21 '21 at 17:42
  • @user3180: Where do you want to start? Are you familiar with the ideas of energy and the CM? Good at math? – Beta Jan 21 '21 at 20:33
3

note: this answer assumes your shape is a proper polygon.

For our purposes, we'll define an equilibrium position as one where the Center of Mass is directly above a point that is between the leftmost and rightmost ground-contact points of the object (assuming the ground is a flat surface perpendicular to the force of gravity). This will work in all cases, for all shapes.

Note that, this is actually the physical definition of rotational equilibrium, as a consequence of Newtonian Rotational Kinematics.

For a proper polygon, if we eliminate cases where they stand on a sole vertex, this definition is equivalent to a stable position.

So, if you have a straight downward gravity, first find the left-most and right-most parts of it that are touching the ground.

Then, calculate your Center of Mass. For a polygon with known vertices and uniform density, this problem is reduced to finding the Centroid (relevant section).

Afterwards, drop a line from your CoM; if the intersection of the CoM and the ground is between those two x values, it's at equilibrium.

If your leftmost point and rightmost point match (ie, in a round object), this will still hold; just remember to be careful with your floating point comparisms.

Note that this can also be used to measure "how stable" an object is -- this measure is the maximum y-distance the Center of Mass can move before it is no longer within the range of the two contact points.

EDIT: nifty diagram made hastily

Diagram

So, how can you use this to find all the ways it can sit on a table? See:


EDIT

The programmable approach

Instead of the computationally expensive task of rotating the shape, try this instead.

Your shape's representation in your program should probably have a list of all vertices.

Find the vertices of your shape's convex hull (basically, your shape, but with all concave vertices -- vertices that are "pushed in" -- eliminated).

Then Iterate through each of pair of adjacent vertices on your convex hull (ie, if I had vertices A, B, C, D, I'd iterate through AB, BC, CD, DA)

Do this test:

  1. Draw a line A through the two vertices being tested
  2. Draw a line perpendicular to A, going through CoM C.
  3. Find the intersection of the two lines (simple algebra)
  4. If the intersection's y value is in between the y value of the two vertices, it stable. If the y values are all equal, compare the x values.

That should do the trick.

Here is an example of the test being running on one pair of vertices:

Example test

If your shape is not represented by its vertices in your data structure, then you should try to convert them. If it's something like a circle or an ellipse, you may use heuristics to guess the answer (a circle has infinite equilibrium positions; an ellipse 4, albeit only two "stable" points). If it's a curved wobbly irregular shape, you're going to have to supply your data structure for me be able to help in a program-related way, instead of just providing case-by-case heuristics.

Community
  • 1
  • 1
Justin L.
  • 13,510
  • 5
  • 48
  • 83
  • An ellipse does *not* have 4 stable positions. – Beta Jun 19 '10 at 18:57
  • An ellipse has four stable positions; it's just that in one of them, the center of mass has only to move an infinitesimal distance before it is outside of the left and right contact points (which are only one point). However, if he is defining the ellipse by vertices, there are no infinitesimal points, and he will definitely have a stable point there (unless it's a vertex). For a physical, real-world demonstration of how an ellipse can have four stable positions, see [this image](http://bit.ly/c1eHHT); however, I will edit my point to emphasize the more precarious stability. – Justin L. Jun 19 '10 at 19:19
  • 1
    @Justin - You're confusing "equilibrium" with "stable equilibrium". Google the later and you'll see. You do a good job in defining equilibrium, but these aren't necessarily stable. (Your answer makes this mistake clear when you say "we'll define a 'stable position'...", but stable already has a clear physical definition, and is about how the system responds to perturbations, and you're redefining it to mean something else.) – tom10 Jun 19 '10 at 21:28
  • Thanks; I think I got it now. I did misunderstand the question; I'll amend that. – Justin L. Jun 19 '10 at 22:31
  • I've noticed that my answer still applies to stable position if we assume a polygon with nonzero area. – Justin L. Jun 20 '10 at 04:17
  • Off-topic question, I'm curious as to what software did you use to draw such pretty illustrations? – Hao Wooi Lim Jun 20 '10 at 10:44
  • Any vector art program will do; I'm sure there are a lot of free ones out there. I've even worked once with a browser-based one called Raven, of the [Aviary](http://aviary.com/) online suite. The first diagram was on Aviary (I was playing around with it). The second, more serious one, was on Adobe Illustrator. – Justin L. Jun 21 '10 at 06:28
0

I'm sure this is not the most efficient algorithm, but it's an idea.

If you can order the verticles of the polygon (assuming it has a finite number of vertices), then just iterate over adjacent pairs of vertices and record the angle it rests at through some form of simulation. There will be duplicate orientations for it to sit on in the case of weird shapes, like stars, but you can accomodate for that by keeping track of the resting rotation.

Jamie Wong
  • 18,104
  • 8
  • 63
  • 81