Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > center og polygon!! ahhhhhh!!!


Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2005, 06:19 PM
obot74639 obot74639 is offline
Junior Contributor
 
Join Date: Oct 2002
Location: Arizona
Posts: 308
Default center og polygon!! ahhhhhh!!!


yet another reason why AutoCAD should cease to exist, j/k...
does anyone know how to find the center of a 2d polygon, not only rectangle, but an irregularly shaped, multipoint ploygon. Like I need to send a list or array of polygonal points like x=this and y=that (1,23)(2,45)(1.5,77.3) for many many many points, and get the center of the polygonal blob which the points will define. The point list will be from a plyline or collection of line segments, (which are changing, and always different from eachother). What math would I need, please super brains- help!!!!!!!!! thanks
Reply With Quote
  #2  
Old 05-12-2005, 06:45 PM
DaftasBrush's Avatar
DaftasBrush DaftasBrush is offline
Senior Contributor

* Expert *
 
Join Date: Jun 2003
Location: A room without Windows
Posts: 896
Default

Depends what you mean by "CENTRE"...

You can find the Max and Min, X and Y values.... essentially draw a rectangle around the whole shape... then find the centre of that

centre.x = (max.x + min.x)/2
centre.y = (max.y + min.y)/2

Or there's the "Centroid".. which is effectively the Centre of Gravity
which is a more complicated calculation... but I've got it around somewhere.... I can look it out if that's what you need.

HTH
__________________
Dan B
"Don't anthropomorphize computers. They don't like it."
Trig is for wuss's - Coordinate Geometry RULES!
Reply With Quote
  #3  
Old 05-12-2005, 07:21 PM
obot74639 obot74639 is offline
Junior Contributor
 
Join Date: Oct 2002
Location: Arizona
Posts: 308
Default that may be it

the center of gravity, I think that would be it- if you could post that- thanks alot, I can't wait to start testing it- thanks again
Reply With Quote
  #4  
Old 05-12-2005, 07:41 PM
DaftasBrush's Avatar
DaftasBrush DaftasBrush is offline
Senior Contributor

* Expert *
 
Join Date: Jun 2003
Location: A room without Windows
Posts: 896
Arrow

Just to be clear... with certain shapes you will look at the Centroid Position and say "That's not the centre"... so just be sure what you want.

The advantage of the Centroid
Its position always remains constant with respect to the vertices through rotation operations.... this is not true of the "Bounding Rectangle" centre point.

See the theory

Anyway, this was written using the UDT coord I like so much...
It's also written to handle LOTS of coords (ie. everything's in longs)
Code:
Public Type coord x as Double y as Double End Type Public Function Centroid(ByRef c() As coord) as coord ' Computes the Centroid (Centre of Gravity) of the polygon ' described by the array of coords c() ' Note : c(ubc) need not be equal to c(0) - "Closing Line Implicit" Dim i As Long, i1 as Long, ubc as long Dim Area As Double, b As Double Dim Cx As Double, Cy As Double ubc = Ubound(c) i1 = ubc For i = 0 To ubc b = (c(i1).x * c(i).y) - (c(i).x * c(i1).y) Area = Area + b Cx = Cx + (c(i).x + c(i1).x) * b Cy = Cy + (c(i).y + c(i1).y) * b i1 = i Next i If Area <> 0 Then Centroid.x = Cx / (Area * 3) Centroid.y = Cy / (Area * 3) End If End Function

One final note... if the vertexes describe a shape with 0 Area.. the function will return (0,0) for the centroid.

Have fun.
__________________
Dan B
"Don't anthropomorphize computers. They don't like it."
Trig is for wuss's - Coordinate Geometry RULES!

Last edited by DaftasBrush; 05-13-2005 at 05:07 AM. Reason: Added link to the theory
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->