Reply-To: "Paul Nicholls" From: "Paul Nicholls" Newsgroups: borland.public.delphi.graphics Subject: For anyone who is interested - PolygonArea algorithm for you :) Date: Thu, 21 Jun 2001 10:47:22 +1000 Lines: 62 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 NNTP-Posting-Host: 203.39.181.43 Message-ID: <3b314418_2@dnews> X-Trace: dnews 993084440 203.39.181.43 (20 Jun 2001 17:47:20 -0700) Path: dnews Xref: dnews borland.public.delphi.graphics:39925 This algorithm only works for concave? polygons with no holes in them - eg. triangles, quads, etc... const x = 0; y = 1; z = 2; type TReal = Single; TVector = array[0..3] of TReal; function Vector(ax,ay,az: TReal): TVector; begin Result[x] := ax; Result[y] := ay; Result[z] := az; end; function Subtract(v1,v2: TVector): TVector; begin Result := Vector([v1[x]-v2[x],v1[y]-v2[y],v1[z]-v2[z]]); end; function Magnitude(v: TVector): TReal; begin Result := Sqrt(Sqr(v[x])+Sqr(v[y])+Sqr(v[z])); end; function Cross(u,v: TVector): TVector; begin Result[X] := u[y] * v[z]-u[z] * v[y]; Result[Y] := u[z] * v[x]-u[x] * v[z]; Result[Z] := u[x] * v[y]-u[y] * v[x]; end; function PolygonArea(Polygon: array of TVector): TReal; var i,N: Integer; u,v: TVector; begin Result := 0; N := High(Polygon)+1; for i := 0 to N - 3 do begin u := VectorSubtract(Polygon[i+1],Polygon[i]); v := VectorSubtract(Polygon[i+2],Polygon[i]); Result := Result + Magnitude(Cross(u,v)); end; Result := Result/2; end; -- Paul Nicholls (Delphi 5) "The plastic veneer of civilized man is easily melted in the heat of the moment" - Paul Nicholls Home Page: www.southcom.com.au/~phantom < IF YOU WANT TO EARN MONEY WHILE YOU SURF ON THE NET GO HERE: > < http://www.alladvantage.com/go.asp?refid=BEM-274 >