(vertices []B2Vec2, count int)
| 140 | } |
| 141 | |
| 142 | func (poly *B2PolygonShape) Set(vertices []B2Vec2, count int) { |
| 143 | B2Assert(3 <= count && count <= B2_maxPolygonVertices) |
| 144 | if count < 3 { |
| 145 | poly.SetAsBox(1.0, 1.0) |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | n := MinInt(count, B2_maxPolygonVertices) |
| 150 | |
| 151 | // Perform welding and copy vertices into local buffer. |
| 152 | ps := make([]B2Vec2, B2_maxPolygonVertices) |
| 153 | tempCount := 0 |
| 154 | |
| 155 | for i := 0; i < n; i++ { |
| 156 | v := vertices[i] |
| 157 | |
| 158 | unique := true |
| 159 | for j := 0; j < tempCount; j++ { |
| 160 | if B2Vec2DistanceSquared(v, ps[j]) < ((0.5 * B2_linearSlop) * (0.5 * B2_linearSlop)) { |
| 161 | unique = false |
| 162 | break |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if unique { |
| 167 | ps[tempCount] = v |
| 168 | tempCount++ |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | n = tempCount |
| 173 | if n < 3 { |
| 174 | // Polygon is degenerate. |
| 175 | B2Assert(false) |
| 176 | poly.SetAsBox(1.0, 1.0) |
| 177 | return |
| 178 | } |
| 179 | |
| 180 | // Create the convex hull using the Gift wrapping algorithm |
| 181 | // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm |
| 182 | |
| 183 | // Find the right most point on the hull |
| 184 | i0 := 0 |
| 185 | x0 := ps[0].X |
| 186 | for i := 1; i < n; i++ { |
| 187 | x := ps[i].X |
| 188 | if x > x0 || (x == x0 && ps[i].Y < ps[i0].Y) { |
| 189 | i0 = i |
| 190 | x0 = x |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | hull := make([]int, B2_maxPolygonVertices) |
| 195 | m := 0 |
| 196 | ih := i0 |
| 197 | |
| 198 | for { |
| 199 | B2Assert(m < B2_maxPolygonVertices) |
no test coverage detected