# Determine if polygon corner is convex or concave

Hello Dynamo users.
I’m stuck on my project on determining witch polygon corner is convex or concave. Tried already measuring vector angles, calculating point distances, but there is always an exception for that approach.
Did anyone faced similar problem. Example in screenshot. One good thing is, angle is always 90.

1 Like

@mutalisk ,hi

i think you have to add some “clever” geometry for detecting it…

KR
Andreas
ConvexConcav_V01.dyn (39.6 KB)

2 Likes

Yes I tried like it. But the polygon outline is random. It can be more complicated and have “bays” then the result would be not true. I mean using middle point will not always work. Example:

Sorry I’m new I can’t upload script.

1 Like

Hi @mutalisk ,

Woulnd’t all corners where the line goes to the left always be concave (and vice versa)?
Assuming we are looking at a clockwise polycurve. Otherwise its just the other way around.

Also, why didnt this work?

2 Likes

Left and right is a good idea, but no idea how to implement it, how to check if it left or right turn? . With vectors maybe it didin’t work because I’m not master of it. It was hard to determine witch angle is measured, exterior or interior angle, and it can be 90 and be convex or concave, or I get it wrong.

Hello,
if you project a circle at each corner and you get the length of the resulting curve on the surface of the polygon
then you compare to the lenght of 1/4 of the arc.

Cordially
christian.stan

Good idea, but now struggling with dynamo logic of splitting circles…

or use geometry does intersect!

1 Like

hello

with project curve

script:
forum anglais 29 septembre.dyn (26.8 KB)

cordially
christian.stan

7 Likes

Now I get it. That’s brilliant, its like containment test but with curves and splitting it in the same time thanks a lot. Worked!

1 Like

Just a FYI about the angle approach:

1. Get all the curves from the polygon in order.
2. Shift all indices in the list by 1, such that you can easily compare the curve against the next one.
3. Measure each curve direction against the x-axis, this will result in angles.
4. Do a simple subtraction, if the value is negative/ positive you know if the angle is convex or concave.
2 Likes

Thank you all, this is used for random room layout generator, you can set room size. This is for testing the generative design feature in Revit.

Script:
room generator.dyn (223.1 KB)

2 Likes

another workflow using python:

3 Likes

Since you’re working with planar geometry we can simplify things a bit and remove the computational difficult geometry calculations. Assuming you start with a polycurve:

1. Take the polycurve and explode it so you have a list of all curves
2. Shift that list by 1 unit (so the second curve in the list is now first, and the first curve in the list is now last) with a List.ShiftIndices node.
3. For the original curve list, get the tangent direction at the end
4. For the shifted list get the tangent direction at the start
5. Build a surface of the polycurve, and pull it’s normal
6. Use a Vector.AngleAboutAxis to get the angle from the original curves end tangent vectors (#3) and the shifted curves tangent start vectors about the normal.
7. Values over 180 are convex, less than 180 concave, and if 180 it’s colinear (and likely you want to combine the angles)
1 Like

Actually… played around with this and got an even better (and faster) solution.

Starting with the polygon:

1. Explode the polygon
2. For each curve, find a point just before it’s start (`Curve.PointAtParmater(crv, -0.0001)`)
3. Check if that point is inside the polygon (`Polygon.ContainmentTest(polygon,pnts)`)
4. Get the points of the polygon, and filter them by the containment test results.
5. If inside, the corner is concave. otherwise they are convex.
2 Likes

Awesome.