Gets the real center of a polygon Returns the size of the passed in stuff
| 751 | // Gets the real center of a polygon |
| 752 | // Returns the size of the passed in stuff |
| 753 | float vm_GetCentroid(vector *centroid, vector *src, int nv) { |
| 754 | ASSERT(nv > 2); |
| 755 | vector normal; |
| 756 | float area, total_area; |
| 757 | int i; |
| 758 | vector tmp_center; |
| 759 | |
| 760 | vm_MakeZero(centroid); |
| 761 | |
| 762 | // First figure out the total area of this polygon |
| 763 | vm_GetPerp(&normal, &src[0], &src[1], &src[2]); |
| 764 | total_area = (vm_GetMagnitude(&normal) / 2); |
| 765 | |
| 766 | for (i = 2; i < nv - 1; i++) { |
| 767 | vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]); |
| 768 | area = (vm_GetMagnitude(&normal) / 2); |
| 769 | total_area += area; |
| 770 | } |
| 771 | |
| 772 | // Now figure out how much weight each triangle represents to the overall |
| 773 | // polygon |
| 774 | vm_GetPerp(&normal, &src[0], &src[1], &src[2]); |
| 775 | area = (vm_GetMagnitude(&normal) / 2); |
| 776 | |
| 777 | // Get the center of the first polygon |
| 778 | vm_MakeZero(&tmp_center); |
| 779 | for (i = 0; i < 3; i++) { |
| 780 | tmp_center += src[i]; |
| 781 | } |
| 782 | tmp_center /= 3; |
| 783 | |
| 784 | *centroid += (tmp_center * (area / total_area)); |
| 785 | |
| 786 | // Now do the same for the rest |
| 787 | for (i = 2; i < nv - 1; i++) { |
| 788 | vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]); |
| 789 | area = (vm_GetMagnitude(&normal) / 2); |
| 790 | |
| 791 | vm_MakeZero(&tmp_center); |
| 792 | |
| 793 | tmp_center += src[0]; |
| 794 | tmp_center += src[i]; |
| 795 | tmp_center += src[i + 1]; |
| 796 | |
| 797 | tmp_center /= 3; |
| 798 | |
| 799 | *centroid += (tmp_center * (area / total_area)); |
| 800 | } |
| 801 | |
| 802 | return total_area; |
| 803 | } |
| 804 | |
| 805 | // Gets the real center of a polygon, but uses fast magnitude calculation |
| 806 | // Returns the size of the passed in stuff |
no test coverage detected