home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.graphics
- Path: sparky!uunet!munnari.oz.au!uniwa!cujo!cc.curtin.edu.au!crod
- From: crod@cc.curtin.edu.au (Scott Kevill)
- Subject: Re: Centroid of a triangle
- Message-ID: <1992Sep8.192356.1@cc.curtin.edu.au>
- Lines: 43
- Sender: news@cujo.curtin.edu.au (News Manager)
- Organization: Curtin University of Technology
- References: <1992Sep8.064202.24022@monu6.cc.monash.edu.au>
- Date: Tue, 8 Sep 1992 10:23:56 GMT
-
- In article <1992Sep8.064202.24022@monu6.cc.monash.edu.au>, int565c@ccds.cc.monash.edu.au writes:
- > Given a triangle with vertices A(x1,y1), B(x2,y2) and C(x3,y3), what is the
- > most efficient algorithm to calculate the centroid of the triangle?
- > Calculation needs not to be exact but the key is efficiency/fast.
- >
- > EA
- >
- >
-
- This may be simpler than you realise, I had almost forgotten this.
- I think for any polygon, or even polyhedron in 3D, all you have to
- do is average all the points to get the centroid.
-
- So in the case of a triangle, it would be : A + B + C
- -----------
- 3
- or more efficiently (A + B + C) * 1/3.
- Store 1/3 as a constant, ie 0.333333333, then the operation is just
- a multiply.
- Pseudocoded it would be something like this :
-
- Somewhere in beginning of program or constant declaration ...
- oneThird = 0.3333333333
-
- Later ...
- cx = (x1 + x2 + x3) * oneThird
- cy = (y1 + y2 + y3) * oneThird
-
- Storing 1/3 as a constant could be a time saver because it is used
- twice. That makes the algorithm 6 additions and 2 multiplies.
- Shortening the constant to 0.33 or 0.3 may even speed up the
- multiplies depending on your accuracy needs, I'm not sure.
-
- Has anyone else found anything better ?
-
- Hope this helps,
-
- Scott Kevill
-
- +-----------------------+--------------------------------------+
- I Scott Kevill I "Be like me, be original" I
- I crod@cc.curtin.edu.au I -- Me I
- +-----------------------+--------------------------------------+
-