| 32 | { |
| 33 | |
| 34 | void vertexLineToPolygon(Vec2* points, float stroke, Vec2* vertices, unsigned int offset, unsigned int nuPoints) |
| 35 | { |
| 36 | nuPoints += offset; |
| 37 | if (nuPoints <= 1) |
| 38 | return; |
| 39 | |
| 40 | stroke *= 0.5f; |
| 41 | |
| 42 | unsigned int idx; |
| 43 | unsigned int nuPointsMinus = nuPoints - 1; |
| 44 | |
| 45 | for (unsigned int i = offset; i < nuPoints; i++) |
| 46 | { |
| 47 | idx = i * 2; |
| 48 | Vec2 p1 = points[i]; |
| 49 | Vec2 perpVector; |
| 50 | |
| 51 | if (i == 0) |
| 52 | perpVector = (p1 - points[i + 1]).getNormalized().getPerp(); |
| 53 | else if (i == nuPointsMinus) |
| 54 | perpVector = (points[i - 1] - p1).getNormalized().getPerp(); |
| 55 | else |
| 56 | { |
| 57 | Vec2 p2 = points[i + 1]; |
| 58 | Vec2 p0 = points[i - 1]; |
| 59 | |
| 60 | Vec2 p2p1 = (p2 - p1).getNormalized(); |
| 61 | Vec2 p0p1 = (p0 - p1).getNormalized(); |
| 62 | |
| 63 | // Calculate angle between vectors |
| 64 | float angle = acosf(p2p1.dot(p0p1)); |
| 65 | |
| 66 | if (angle < AX_DEGREES_TO_RADIANS(70)) |
| 67 | perpVector = p2p1.getMidpoint(p0p1).getNormalized().getPerp(); |
| 68 | else if (angle < AX_DEGREES_TO_RADIANS(170)) |
| 69 | perpVector = p2p1.getMidpoint(p0p1).getNormalized(); |
| 70 | else |
| 71 | perpVector = (p2 - p0).getNormalized().getPerp(); |
| 72 | } |
| 73 | perpVector = perpVector * stroke; |
| 74 | |
| 75 | vertices[idx].set(p1.x + perpVector.x, p1.y + perpVector.y); |
| 76 | vertices[idx + 1].set(p1.x - perpVector.x, p1.y - perpVector.y); |
| 77 | } |
| 78 | |
| 79 | // Validate vertexes |
| 80 | offset = (offset == 0) ? 0 : offset - 1; |
| 81 | for (unsigned int i = offset; i < nuPointsMinus; i++) |
| 82 | { |
| 83 | idx = i * 2; |
| 84 | const unsigned int idx1 = idx + 2; |
| 85 | |
| 86 | Vec2 p1 = vertices[idx]; |
| 87 | Vec2 p2 = vertices[idx + 1]; |
| 88 | Vec2 p3 = vertices[idx1]; |
| 89 | Vec2 p4 = vertices[idx1 + 1]; |
| 90 | |
| 91 | float s; |
no test coverage detected