Permalink
Browse files

straighten lines when possible

  • Loading branch information...
1 parent 7b97e46 commit be953ebd51e9a0f970f3a3916a29e816d3ef8569 @adamwulf committed Mar 13, 2012
@@ -70,37 +70,58 @@ Public Function GetAngle(ByVal Ax As Single, ByVal Ay As _
+-(void) cleanPointsALot{
+ if([points count] < 3) return;
+ debug_NSLog(@"===== cleaning %d points", [points count]);
+
+ for(int i=0;i<[points count];i++){
+ int prev = ([points count] + i - 1) % [points count];
+ NSValue* v1 = [points objectAtIndex:prev];
+ CGPoint p1 = [v1 CGPointValue];
+ NSValue* v2 = [points objectAtIndex:i];
+ CGPoint p2 = [v2 CGPointValue];
+
+ // check how far away the point is from straight across
+ CGPoint p3 = CGPointMake(p2.x, p1.y);
+ CGFloat angle = GetAngle(p1, p2, p3);
+ if(angle / M_PI < 0.08){
+ // pretty straight!
+ [points removeObjectAtIndex:i];
+ [points insertObject:[NSValue valueWithCGPoint:p3] atIndex:i];
+ }
+
+ // check up/down
+ p3 = CGPointMake(p1.x, p2.y);
+ angle = GetAngle(p1, p2, p3);
+ if(angle / M_PI < 0.08){
+ // pretty straight!
+ [points removeObjectAtIndex:i];
+ [points insertObject:[NSValue valueWithCGPoint:p3] atIndex:i];
+ }
+ }
+ [self cleanPoints];
+}
+
-(IBAction) cleanPoints{
if([points count] < 3) return;
debug_NSLog(@"===== cleaning %d points", [points count]);
- CGFloat prevSlope;
- for(int i=1;i<[points count];i++){
- NSValue* v1 = [points objectAtIndex:i-1];
+ for(int i=2;i<[points count];i++){
+
+ NSValue* v1 = [points objectAtIndex:i-2];
CGPoint p1 = [v1 CGPointValue];
- NSValue* v2 = [points objectAtIndex:i];
+ NSValue* v2 = [points objectAtIndex:i-1];
CGPoint p2 = [v2 CGPointValue];
- CGFloat slope = (p2.y - p1.y) / (p2.x - p1.x);
- if(slope < .1 && slope > -.1) slope = 0;
- if(slope < -20) slope = -20;
- if(slope > 20) slope = 20;
- if(i != 1){
- CGFloat diff = ABS(slope) - ABS(prevSlope);
-// if(prevSlope == 0){
-// diff = slope;
-// }
- if(diff > -5 && diff < 5){
- // same slope, basically
- // remove a point
- [points removeObjectAtIndex:i-1];
- i--;
- }else if(DistanceBetweenTwoPoints(p1, p2) < 30){
- [points removeObjectAtIndex:i-1];
- i--;
- }
+ NSValue* v3 = [points objectAtIndex:i];
+ CGPoint p3 = [v3 CGPointValue];
+
+ CGFloat angle = GetAngle(p2, p1, p3);
+
+ if(angle / M_PI > 0.80){
+ [points removeObjectAtIndex:i-1];
+ i--;
}
- prevSlope = slope;
}
debug_NSLog(@"===== done cleaning %d points", [points count]);
[self setNeedsDisplay];
@@ -227,7 +248,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[pointsInThisLine removeAllObjects];
debug_NSLog(@"touch ended");
[super touchesEnded:touches withEvent:event];
- [self cleanPoints];
+ [self cleanPointsALot];
[self setNeedsDisplay];
}
@@ -13,7 +13,6 @@
<array key="IBDocument.IntegratedClassDependencies">
<string>IBProxyObject</string>
<string>IBUIView</string>
- <string>IBUIButton</string>
</array>
<array key="IBDocument.PluginDependencies">
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
@@ -34,46 +33,9 @@
<object class="IBUIView" id="766721923">
<reference key="NSNextResponder"/>
<int key="NSvFlags">274</int>
- <array class="NSMutableArray" key="NSSubviews">
- <object class="IBUIButton" id="574845952">
- <reference key="NSNextResponder" ref="766721923"/>
- <int key="NSvFlags">292</int>
- <string key="NSFrame">{{0, 20}, {72, 37}}</string>
- <reference key="NSSuperview" ref="766721923"/>
- <reference key="NSWindow"/>
- <string key="NSReuseIdentifierKey">_NS:9</string>
- <bool key="IBUIOpaque">NO</bool>
- <string key="targetRuntimeIdentifier">IBIPadFramework</string>
- <int key="IBUIContentHorizontalAlignment">0</int>
- <int key="IBUIContentVerticalAlignment">0</int>
- <int key="IBUIButtonType">1</int>
- <object class="NSColor" key="IBUIHighlightedTitleColor">
- <int key="NSColorSpace">3</int>
- <bytes key="NSWhite">MQA</bytes>
- </object>
- <object class="NSColor" key="IBUINormalTitleColor">
- <int key="NSColorSpace">1</int>
- <bytes key="NSRGB">MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA</bytes>
- </object>
- <object class="NSColor" key="IBUINormalTitleShadowColor">
- <int key="NSColorSpace">3</int>
- <bytes key="NSWhite">MC41AA</bytes>
- </object>
- <object class="IBUIFontDescription" key="IBUIFontDescription">
- <int key="type">2</int>
- <double key="pointSize">15</double>
- </object>
- <object class="NSFont" key="IBUIFont">
- <string key="NSName">Helvetica-Bold</string>
- <double key="NSSize">15</double>
- <int key="NSfFlags">16</int>
- </object>
- </object>
- </array>
<string key="NSFrame">{{0, 20}, {768, 1004}}</string>
<reference key="NSSuperview"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView"/>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MQA</bytes>
@@ -97,15 +59,6 @@
</object>
<int key="connectionID">3</int>
</object>
- <object class="IBConnectionRecord">
- <object class="IBCocoaTouchEventConnection" key="connection">
- <string key="label">cleanPoints</string>
- <reference key="source" ref="574845952"/>
- <reference key="destination" ref="766721923"/>
- <int key="IBEventType">7</int>
- </object>
- <int key="connectionID">5</int>
- </object>
</array>
<object class="IBMutableOrderedSet" key="objectRecords">
<array key="orderedObjects">
@@ -129,16 +82,9 @@
<object class="IBObjectRecord">
<int key="objectID">2</int>
<reference key="object" ref="766721923"/>
- <array class="NSMutableArray" key="children">
- <reference ref="574845952"/>
- </array>
+ <array class="NSMutableArray" key="children"/>
<reference key="parent" ref="0"/>
</object>
- <object class="IBObjectRecord">
- <int key="objectID">4</int>
- <reference key="object" ref="574845952"/>
- <reference key="parent" ref="766721923"/>
- </object>
</array>
</object>
<dictionary class="NSMutableDictionary" key="flattenedProperties">
@@ -148,7 +94,6 @@
<string key="-2.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="2.CustomClassName">TCChartView</string>
<string key="2.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
- <string key="4.IBPluginDependency">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</dictionary>
<dictionary class="NSMutableDictionary" key="unlocalizedProperties"/>
<nil key="activeLocalization"/>

0 comments on commit be953eb

Please sign in to comment.