5
$\begingroup$

Having 4 points A (ax, ay), B (bx, by), C (cx, cy) and D (dx, dy) that describe any quadrangular in 2D, with x and y in range [-100, 100] I would like to calculate point E (ex, ey), which would be a center of this quadrangular.

What algorithm can be used for such calculation and why?

$\endgroup$
8
$\begingroup$

The formula for a polygon's centroid is easily expressed in Mathematica:

PolygonCentroid[pts_?MatrixQ] := With[{dif = Map[Det, Partition[pts, 2, 1, {1, 1}]]},
                       ListConvolve[{{1, 1}}, Transpose[pts], {-1, -1}].dif/(3 Total[dif])]

This works for any Polygon[], not just quadrilaterals.

$\endgroup$
  • $\begingroup$ I'll note that I used this routine previously to help with coloring the proposed mathematica.SE logo... $\endgroup$ – J. M. is away Jun 30 '12 at 14:32
  • $\begingroup$ Thank you for prompt answer. Where can I find a algorithm/formula in simple math that can be used for coding in programming language such as C, Perl, etc.? $\endgroup$ – user1585 Jun 30 '12 at 14:33
  • 2
    $\begingroup$ @Ωmega: I linked to the formula already; you should not have any trouble translating the formula to your computing environment of choice. If you do have trouble, then this site is not the place to ask questions not related to Mathematica. $\endgroup$ – J. M. is away Jun 30 '12 at 14:35
  • 1
    $\begingroup$ @J.M. Ahhh! The infamous pointlike massless cow ... $\endgroup$ – Dr. belisarius Jun 30 '12 at 15:05
  • 1
    $\begingroup$ @belisarius I immediately thought of spherical horses in vacuum when I read J.M.'s comment. $\endgroup$ – Heike Jun 30 '12 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy