Consider this ASCII drawing:
A _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ D
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|
B C
Points A, B, C, and D are known CGPoints
within an NSMutableArray
and have been used to create a filled CGPath
. Now consider this drawing:
A _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ D
| |
| |
| |
| |
| H _ _ _ I |
| | | |
| | | |
| F _ _ _| | |
| | G | |
| | |_ _ _ K |
| | J | |
| | | |
|_ _ _ _| _ _ _ _ _ _ _ _ |_ _ _|
B E L C
CGPoints
E, F, G, H, I, J, K, and L are known and have been appended to the end of the NSMutableArray
.
The Question
How can I rearrange all the points within the array to create a CGPath
that looks like the drawing below?
A _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ D
| |
| |
| |
| |
| H _ _ _ I |
| | | |
| | | |
| F _ _ _| | |
| | G | |
| | |_ _ _ K |
| | J | |
| | | |
|_ _ _ _| |_ _ _|
B E L C
Currently I have no trouble creating a CGPath
- if I know the order of the CGpoints
- by looping through them:
CGPoint firstPoint = [[points objectAtIndex:0] CGPointValue];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, firstPoint.x, firstPoint.y);
for (int i = 0; i < [points count]; i++)
{
CGPathAddLineToPoint(path, NULL, [[points objectAtIndex:i] CGPointValue].x, [[points objectAtIndex:i] CGPointValue].y);
}
CGPathCloseSubpath(path);
... but this assumes that a line should be drawn from each point in the array i
to the following point i + 1
. In the drawing above, lines would have to be drawn from A → B
, B → E
, E → F
... K → L
, L → C
, C → D
. If E is not after B and C is not after L in the array (which they won't be), then this obviously won't draw correctly.
More Information
- All lines are drawn perpendicular to each other, so all
CGPoints
should share anx
ory
coordinate with theCGPoint
before and after them. - (An extension of #1) A point will always be at right angles to the points before and after it.
- It is not possible to predict where the points will occur within the square, and the new set of points may or may not be in order when appended to the end of the array.
- ...
Other Possible Layouts
A _ _ _ _ _ _ _ K P _ _ _ D
| | | |
| | | |
| | N _ _ _| |
| | | O |
| |_ _ _| |
| L M |
| |
| F _ _ _ G |
| | | |
| | |_ _ _ I |
| | H | |
| | | |
|_ _ _ _ _ _ _| |_ _ _|
B E J C
A _ _ _ _ _ _ _ _ _ _ M
| |
| |
| |_ _ _ _ _ _ O
| N |
| H _ _ _ I |
| | | |
| | | |
| F _ _ _| | |
| | G | |
| | |_ _ _ K |
| | J | |
| | | |
|_ _ _ _| |_ _ _|
B E L C