# Determining the diameter or radius of a Cylindrical Solid

I’m kind of scratching my head right now on this problem so that is why I’m putting it out on the forum.

If one has a cylinder modeled (no specific modeling method per say) and it is a solid group or component how would one programmatically determine the radius or diameter of this solid?

Calculating the volume is a no brainer, but since the circular faces could be actual circles or exploded curves it seems to me that it is a bit more tricky trying to figure out the diameter of the cylinder.

I guess if the object has not had its ArcCurve’s exploded the problem is simple enough, but how do I work around this if the ArcCurve no longer exists?

Assuming the segments that were once an arc or circle haven’t been distorted or their lengths changed, then you can find a suspected segment and from its ends its plane, then you can find its mid-point, then strike a line through there on the plane; repeat for another suspected segment, the two lines should intersect. That intersection-point is the center of the former arc or circle…

1 Like

Guessing. Could you use the area of the intact adjacent plane? A= π r2 right? so devide the plane area by pi and then square root for a radius? I guess the faceted nature of circles will make this slightly inaccurate.

1 Like

I am finding with the follow me tool that sometimes a circle can be used to create the cylinder however if certain conditions are not met the resulting solid no longer has a circle definition but its edges are all exploded, hence the problem I have described. In other words you can’t guarantee that the curve or arccurve will exist.

Yes, follow me explodes circles, you can re-weld the resulting geometry but it becomes a curve. I feel like there was an extension that could find and re-make true circles including the meta data but I can’t remember it, perhaps I am wishful remembering, @TIG would know better than I.

When faced with this problem I generally draw a line or tape measure between nodes across the center as mentioned, or perpendicular to two segments center if only part of the arc is available. Clunky but it works.

1 Like

Once one has determined which face constitutes the circle it probably wouldn’t be too hard to determine the number of segments in that face and then a little bit of math can be used to calculate the theoretical diameter (faceted circle).

1 Like

True, armed with the surface area and the number of segments it seems there should be a way to come very close to a radius/diameter.

1 Like

I think the easiest way to determine the “circular” face would be just to assume that it has six or more edges. All other faces will generally have three or four. I’m sure this isn’t bullet proof but it seems sensible enough.

1 Like

If the Follow Me geometry is all selected and “Welded” the circles become curves, which do not display radius info but DO display the number of facets in entity info. So after welding, the surface area, and number of segments in the edge are both available.

1 Like

select a solid cylinder before run

``````def mid(p1, p2)
Geom.linear_combination(0.5, p1, 0.5, p2)
end

m = Sketchup.active_model
s = m.selection
cyl = s.first
base_face = cyl.entities.grep(Sketchup::Face).find{|f| f.vertices.size > 4 }
norm = base_face.normal
verts_pos = base_face.outer_loop.vertices.map(&:position)
v0 = verts_pos[1].vector_to(verts_pos[0])
v2 = verts_pos[1].vector_to(verts_pos[2])
v0_per = v0 * norm
v2_per = v2 * norm
m0 = mid( verts_pos[1], verts_pos[0] )
m2 = mid( verts_pos[1], verts_pos[2] )
line0 = [ m0, v0_per ]
line2 = [ m2, v2_per ]
center = Geom.intersect_line_line(line0, line2)
verts_pos[2].distance(center)
end
``````

5 Likes

Wow, nice.

1 Like

Nailed it!

1 Like

Each of the circle’s segments represents a chord drawn across the outside of the imaginary true circle.
A line of radius drawn from the center so it bisects the chord creates the adjacent side of a right triangle.
The opposite side of the triangle is formed by the right angle bisecting the chord (i.e., half the length of a segment.)
The hypotenuse of the triangle is one of two lines of radius drawn from the center to each vertex of the chord segment.
The angle between the two radius lines to the segment vertices is the included angle.
The line of radius which bisects the edge segment also bisects the included angle forming the angle AH for the triangle (opposite the half length of the edge chord segment.)

So, simple trigimometry … sin of angle = opposite length / hypotenuse length
We know the angle AH and the opposite length. We must find the hypotenuse length which is the radius.

``````  segment  = face.edges.first
num_segs = face.edges.count
included_angle = 360.0 / num_segs
half_included_angle = included_angle / 2.0
opposite_length = segment.length / 2.0
# sin of angle AH = opposite_length / hypotenuse_length
# Math.sin(half_included_angle.degrees) = opposite_length / hypotenuse_length
# Rearrange to:
hypotenuse_length = opposite_length / Math.sin(half_included_angle.degrees)
# The hypotenuse IS the circle radius
``````

EDIT: The `Math` trig functions want radian arguments.

3 Likes

This was the little bit of math of which I spoke. Trigonometry is sure a useful tool. I suppose one doesn’t even need to bother finding the center of the circle. Maybe this problem is much simpler than I originally thought.

I also just had another potential idea on how to verify that the face is a circle. Since you know the hypotenuse and the number of sides one can easily calculate the area of the polygon and compare it with the actual area of the face. If they agree within a certain tolerance then you know the face is circular or at least a regular polygon representing a circle.

With regards to the API method that calculates the volume of a solid, does it calculate the actual faceted volume or the theoretical volume when you are dealing with arc curves?

Also the same question for the area of a face if the face is a circle/arccurve?

I would appear that SketchUp is calculating the actual (faceted) area of the circle:

There is about a 4.5% difference in area (from theoretical) with a 12 sided circle, of course a 24 sided circle will be even closer to perfect.

The error can be calculated with this formula:

A 24 sided polygon is almost 99% the theoretical area.

I think you meant it to be the height of the triangle created by the included angle.

It is actually r that is the hypotenuse.

And yes, that sketch is exactly what I described above, i.e., the included angle and it’s bisector creating triangles.

We can also use the Law of Sines.

`r / sin of ((360-theta) / 2) = C / sin of theta`

… rearrage for r:

`r = C * sin of ((360-theta) / 2) / sin of theta`

1 Like