Gets the real center of a polygon Returns the size of the passed in stuff
| 801 | // Gets the real center of a polygon |
| 802 | // Returns the size of the passed in stuff |
| 803 | float vm_GetCentroid(vector *centroid, vector *src, int nv) { |
| 804 | // ASSERT (nv>2); |
| 805 | vector normal; |
| 806 | float area, total_area; |
| 807 | int i; |
| 808 | vector tmp_center; |
| 809 | |
| 810 | vm_MakeZero(centroid); |
| 811 | |
| 812 | // First figure out the total area of this polygon |
| 813 | vm_GetPerp(&normal, &src[0], &src[1], &src[2]); |
| 814 | total_area = (vm_GetMagnitude(&normal) / 2); |
| 815 | |
| 816 | for (i = 2; i < nv - 1; i++) { |
| 817 | vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]); |
| 818 | area = (vm_GetMagnitude(&normal) / 2); |
| 819 | total_area += area; |
| 820 | } |
| 821 | |
| 822 | // Now figure out how much weight each triangle represents to the overall |
| 823 | // polygon |
| 824 | vm_GetPerp(&normal, &src[0], &src[1], &src[2]); |
| 825 | area = (vm_GetMagnitude(&normal) / 2); |
| 826 | |
| 827 | // Get the center of the first polygon |
| 828 | vm_MakeZero(&tmp_center); |
| 829 | for (i = 0; i < 3; i++) { |
| 830 | tmp_center += src[i]; |
| 831 | } |
| 832 | tmp_center /= 3; |
| 833 | |
| 834 | *centroid += (tmp_center * (area / total_area)); |
| 835 | |
| 836 | // Now do the same for the rest |
| 837 | for (i = 2; i < nv - 1; i++) { |
| 838 | vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]); |
| 839 | area = (vm_GetMagnitude(&normal) / 2); |
| 840 | |
| 841 | vm_MakeZero(&tmp_center); |
| 842 | |
| 843 | tmp_center += src[0]; |
| 844 | tmp_center += src[i]; |
| 845 | tmp_center += src[i + 1]; |
| 846 | |
| 847 | tmp_center /= 3; |
| 848 | |
| 849 | *centroid += (tmp_center * (area / total_area)); |
| 850 | } |
| 851 | |
| 852 | return total_area; |
| 853 | } |
| 854 | |
| 855 | // Gets the real center of a polygon, but uses fast magnitude calculation |
| 856 | // Returns the size of the passed in stuff |
nothing calls this directly
no test coverage detected