博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
寻找房间中心zz
阅读量:5030 次
发布时间:2019-06-12

本文共 4262 字,大约阅读时间需要 14 分钟。

Finding the Centroid of a Room Boundary

It's been a while since my last post and I'm sure most of you were like... "Where the hell is Don!".... it's ok! I'm still around. I've been busy working on stuff I can't talk about. Don't worry though, I'm not a good secret keeper.

So this post is going to explain something that a bunch of folks have issues with that involves finding the actual centroid of a polygon, or in this case a Room element. Now let's be careful not to confuse a centroid with a pair of midpoints taken from the furthest X and Y planes... a centroid is more closely described as the center of mass within a polygon.

Now this is done by taking a series of 2D points and running some tricky math on them and dividing the points by 6x the area of the polygon. So to make it simple for you guys, I've taken the liberty of sharing a couple of functions that makes this all possible. The samples here are in Revit 2011 format.

First you'll need a function that iterates through the boundary segments of a Room Element and builds up a series of 2D points taken from either endpoints of each segment (no need to worry about curved segments since they usually wont effect the centroid too much, or you can add the midpoint of the curve arc to get it closer).
This little Function will return a list of 2D PointF from a boundary of a Room element.

'''     ''' Extract a List of 2D Points from a Room's Boundary    '''     '''     ''' 
Private Sub ExtractBoundaryPointsFromRoom(p_room As Room) ' The Points List Dim m_pts As New List(Of PointF) ' The Z Height Dim m_z As Double = 0 ' Work with the Boundary Dim m_bsaa As Autodesk.Revit.DB.Architecture.BoundarySegmentArrayArray = m_room.Boundary ' Segment Array at Floor Level For Each bsa As Autodesk.Revit.DB.Architecture.BoundarySegmentArray In m_bsaa Try For Each bs As Autodesk.Revit.DB.Architecture.BoundarySegment In bsa Dim m_c As Curve = bs.Curve ' First Endpoint Dim m_EndPoint1 As XYZ = m_c.EndPoint(0) Dim m_PointF1 As New PointF(m_EndPoint1(0), m_EndPoint1(1)) m_pts.Add(m_PointF1) ' Second Endpoint Dim m_EndPoint2 As XYZ = m_c.EndPoint(1) Dim m_PointF2 As New PointF(m_EndPoint2(0), m_EndPoint2(1)) m_pts.Add(m_PointF2) ' The Height m_z = m_EndPoint1(2) Next Catch ex As Exception End Try Next ' Return the 2D Centroid Dim m_2Dcentroid As PointF = FindCentroid(m_pts.ToArray, m_room.Area) ' Add the Floor Level of Boundary for Z Elevation InsertionPoint = New XYZ(m_2Dcentroid.X, m_2Dcentroid.Y, m_z) End Sub
The Function below will take a list of points (first gathered from the segments array of a room) and convert them to a real life centroid in 2D format. The Z elevation is pretty easy to figure out for a room and what ever you're using this for is typically going to use 0 or a preset elevation for the result anyway.
'''     ''' Find 2D Centroid    '''     ''' Collection of Points Describing the Polygon    ''' The Area of the Polygon    ''' 
2D Point (Pointf)
'''
This Function Kicks Ass
Private Function FindCentroid(ByVal pts() As PointF, p_rmArea As Single) As PointF ' Add the First PT to the End of the Array (full circulation) ReDim Preserve pts(pts.Length) pts(pts.Length - 1) = New PointF(pts(0).X, pts(0).Y) ' Get the Centroid Dim X As Single = 0 Dim Y As Single = 0 Dim m_sf As Single ' This is Where the Magic Happens For i As Integer = 0 To pts.Length - 2 m_sf = pts(i).X * pts(i + 1).Y - pts(i + 1).X * pts(i).Y X += (pts(i).X + pts(i + 1).X) * m_sf Y += (pts(i).Y + pts(i + 1).Y) * m_sf Next i ' Divide by 6X the Are of the Polygon X /= (6 * p_rmArea) Y /= (6 * p_rmArea) ' This is the Final Result Return New PointF(X, Y) End Function
That's all until next time...

转载于:https://www.cnblogs.com/xpvincent/p/3842033.html

你可能感兴趣的文章
android中DatePicker和TimePicker的使用
查看>>
SpringMVC源码剖析(四)- DispatcherServlet请求转发的实现
查看>>
Android中获取应用程序(包)的大小-----PackageManager的使用(二)
查看>>
Codeforces Gym 100513M M. Variable Shadowing 暴力
查看>>
浅谈 Mybatis中的 ${ } 和 #{ }的区别
查看>>
CNN 笔记
查看>>
版本更新
查看>>
SQL 单引号转义
查看>>
start
查看>>
实现手机扫描二维码页面登录,类似web微信-第三篇,手机客户端
查看>>
PHP socket客户端长连接
查看>>
7、shell函数
查看>>
【转】Apache Jmeter发送post请求
查看>>
Nginx 基本 安装..
查看>>
【凸优化】保留凸性的几个方式(交集、仿射变换、投影、线性分式变换)
查看>>
NYOJ-613//HDU-1176-免费馅饼,数字三角形的兄弟~~
查看>>
TFS --- GrantBackup Plan Permissions Error
查看>>
傅里叶级数与积分方程
查看>>
软工作业3:用户体验分析——以“南通大学教务管理系统微信公众号”为例
查看>>
Css:背景色透明,内容不透明之终极方法!兼容所有浏览器
查看>>