Grepping for Largest Face

I’m wondering if anyone has ever created a efficient algorithm for finding the largest face from a group of entities:

Here is my basic grep for faces:

group1faces = entities1.grep(Sketchup::Face)

I suppose whether I loop through all the faces comparing areas or I let some internal function of some sort do it it probably doesn’t make a whole lot of difference, but I thought I should ask before creating my own algorithm. Nine times out of ten someone else has already encountered a similar problem and has a solution that is usually better than what I will come up with on my first go around.

entities1.grep(Sketchup::Face).max_by &:area
ents.grep(Sketchup::Face).map{|f| [f.area, f]}.sort[-1][1]

edit: I grabbed the wrong one…


1 Like

Ruby 2.2 (shameless plug)

For 2.0, I use

1 Like

How about this:

@gross_roof_sheath_area_a = (entities31.grep(Sketchup::Face).max_by &:area).area

1 Like

well, if the result is the area, than that swings the it back towards :map,

(ents.grep(Sketchup::Face).map &:area).max



It’s more or less what you feel would be easiest to understand a few months from now.

One might decide that the fewest intermediate arrays (via grep or map) would be best, so something like:

ents.max_by { |e| e.is_a?(Sketchup::Face) ? e.area : -1 }.area

It’s been a while since I jammed SU with larger models, but generally intermediate arrays are not an issue, especially when done via .grep <some class>

1 Like