Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Call test:
  2.  
  3.     NSArray *points = @[[NSValue valueWithCGPoint:CGPointMake(40, 612)],
  4.                         [NSValue valueWithCGPoint:CGPointMake(379, 788)],
  5.                         [NSValue valueWithCGPoint:CGPointMake(0, 352)]];
  6.    
  7. //    NSArray *distances = @[@(13.535637), @(46.931522), @(51.585461)];
  8.     NSArray *distances = @[@(349.9057), @(352.84134), @(353.37515)];
  9.  
  10.     TriangularisationMapView *v = [[TriangularisationMapView alloc] initWithFrame:CGRectMake(0, 0, 600, 600) points:points andDistance:distances];
  11.     [[self view] addSubview:v];
  12.  
  13. ------------------------------------------------------------------------------------------------------------------------
  14.  
  15. @interface TriangularisationMapView : UIView
  16. -(id)initWithFrame:(CGRect)frame points:(NSArray *)points andDistance:(NSArray *)distance;
  17.  
  18. @end
  19.  
  20.  
  21.  
  22. ------------------------------------------------------------------------------------------------------------------------
  23.  
  24. #import "TriangularisationMapView.h"
  25.  
  26. @interface TriangularisationMapView ()
  27.  
  28. @property (nonatomic, assign) CGPoint  pointA;
  29. @property (nonatomic, assign) CGPoint  pointB;
  30. @property (nonatomic, assign) CGPoint  pointC;
  31. @property (nonatomic, assign) float    distanceToA;
  32. @property (nonatomic, assign) float    distanceToB;
  33. @property (nonatomic, assign) float    distanceToC;
  34.  
  35. @property (nonatomic, assign) CGPoint  finalPoint;
  36. @end
  37.  
  38. @implementation TriangularisationMapView
  39.  
  40. -(id)initWithFrame:(CGRect)frame points:(NSArray *)points andDistance:(NSArray *)distance
  41. {
  42.     self = [super initWithFrame:frame];
  43.     if (self)
  44.     {
  45.         [self setBackgroundColor:[UIColor whiteColor]];
  46.         _pointA = [[points objectAtIndex:0] CGPointValue];
  47.         _pointB = [[points objectAtIndex:1] CGPointValue];
  48.         _pointC = [[points objectAtIndex:2] CGPointValue];
  49.        
  50.         _distanceToA = [[distance objectAtIndex:0] floatValue];
  51.         _distanceToB = [[distance objectAtIndex:1] floatValue];
  52.         _distanceToC = [[distance objectAtIndex:2] floatValue];
  53.     }
  54.     return self;
  55. }
  56.  
  57. - (void)drawRect:(CGRect)rect
  58. {
  59.     [self addCircleNb:0 withCenter:_pointA andRadius:_distanceToA];
  60.     [self addCircleNb:1 withCenter:_pointB andRadius:_distanceToB];
  61.     [self addCircleNb:2 withCenter:_pointC andRadius:_distanceToC];
  62.    
  63.     [self calculatePosition];
  64. }
  65.  
  66. -(void)calculatePosition
  67. {
  68.     CGPoint P2MinusP1 = CGPointMake(_pointB.x - _pointA.x, _pointB.y - _pointA.y);
  69.     CGPoint P3MinusP1 = CGPointMake(_pointC.x - _pointA.x, _pointC.y - _pointA.y);
  70.     CGFloat magP2MinusP1 = sqrt(pow((P2MinusP1.x), 2) +  pow((P2MinusP1.y), 2));
  71.     CGPoint eX = CGPointMake(P2MinusP1.x / magP2MinusP1, P2MinusP1.y / magP2MinusP1);
  72.     CGFloat i = eX.x * P3MinusP1.x + eX.y * P3MinusP1.y;
  73.     CGPoint eXTimesI = CGPointMake(eX.x * i, eX.y * i);
  74.     CGPoint P3MinusP1MinusITimesEX = CGPointMake(P3MinusP1.x - eXTimesI.x, P3MinusP1.y - eXTimesI.y);
  75.     CGFloat magP3MinusP1MinusITimesEX = sqrt(pow(P3MinusP1MinusITimesEX.x, 2) + pow(P3MinusP1MinusITimesEX.y, 2));
  76.     CGPoint eY = CGPointMake(P3MinusP1MinusITimesEX.x / magP3MinusP1MinusITimesEX, P3MinusP1MinusITimesEX.y / magP3MinusP1MinusITimesEX);
  77.     CGFloat j = eY.x * P3MinusP1.x + eY.y * P3MinusP1.y;
  78.     CGFloat x = (pow(_distanceToA, 2) - pow(_distanceToB, 2) + pow(magP2MinusP1, 2)) / (2 * magP2MinusP1);
  79.     CGFloat y = (pow(_distanceToA, 2) - pow(_distanceToC, 2) + pow(i, 2) + pow(j, 2)) / (2 * j) - (i * x) / j;
  80.    
  81.     _finalPoint = CGPointMake(_pointA.x + x * eX.x + y * eY.x, _pointA.y + x * eX.y + y * eY.y);
  82.    
  83.     NSLog(@"%f %f", _finalPoint.x, _finalPoint.y);
  84.    
  85.     UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(_finalPoint.x-3, _finalPoint.y-3, 6, 6)];
  86.     [centerView setBackgroundColor:[UIColor orangeColor]];
  87.    
  88.     UILabel *label = [[UILabel alloc] init];
  89.     [label setBackgroundColor:[UIColor clearColor]];
  90.     [label setText:[NSString stringWithFormat:@"Final: {%d,%d}",(int)_finalPoint.x, (int)_finalPoint.y]];
  91.     [label sizeToFit];
  92.     [label setFrame:CGRectMake(_finalPoint.x - ([label frame].size.width/2.0),
  93.                                _finalPoint.y - ([label frame].size.height/2.0-20),
  94.                                [label frame].size.width,
  95.                                [label frame].size.height)];
  96.     [self addSubview:label];
  97.     [self addSubview:centerView];
  98.    
  99.  
  100.     [self checkFinalPoint];
  101. }
  102.  
  103. -(void)checkFinalPoint
  104. {
  105.     float distanceToA2 = [self distanceBetweenPoints:_pointA with:_finalPoint];
  106.     float distanceToB2 = [self distanceBetweenPoints:_pointB with:_finalPoint];
  107.     float distanceToC2 = [self distanceBetweenPoints:_pointC with:_finalPoint];
  108.  
  109.     float affordableError = 4;
  110.     if (fabs(distanceToA2-_distanceToA) >= affordableError || (fabs(distanceToB2-_distanceToB) >= affordableError) || (fabs(distanceToC2-_distanceToC) >= affordableError))
  111.     {
  112.         NSLog(@"Position Too Much Wrong");
  113.     }
  114.    
  115. }
  116.  
  117. -(float)distanceBetweenPoints:(CGPoint)pointA with:(CGPoint)pointB
  118. {
  119.    float square = (pointA.x-pointB.x)*(pointA.x-pointB.x)+ (pointA.y-pointB.y)*(pointA.y-pointB.y);
  120.  
  121.     return sqrtf(square);
  122. //    NSLog(@"Result: %@", @(sqrtf(square)));
  123. }
  124.  
  125. -(void)addCircleNb:(NSInteger)nb withCenter:(CGPoint)center andRadius:(CGFloat)radius
  126. {
  127.     //Global Circle
  128.     UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(center.x-radius, center.y-radius, radius*2, radius*2)];
  129.     [aView setBackgroundColor:[UIColor clearColor]];
  130.    
  131.     //Point Center
  132.     UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(radius-1, radius-1, 2, 2)];
  133.     [centerView setBackgroundColor:[UIColor blackColor]];
  134.     [[centerView layer] setCornerRadius:1];
  135.    
  136.     //Center Coordinate Label
  137.     UILabel *label = [[UILabel alloc] init];
  138.     [label setBackgroundColor:[UIColor clearColor]];
  139.     [label setFont:[UIFont systemFontOfSize:10]];
  140.     [label setText:[NSString stringWithFormat:@"{%d,%d}",(int)center.x, (int)center.y]];
  141.     [label sizeToFit];
  142.     [label setFrame:CGRectMake(([aView frame].size.width-[label frame].size.width)/2.0,
  143.                                ([aView frame].size.height-[label frame].size.height)/2.0,
  144.                                [label frame].size.width,
  145.                                [label frame].size.height)];
  146.     [aView addSubview:label];
  147.     [aView addSubview:centerView];
  148.    
  149.     switch (nb)
  150.     {
  151.         case 0:
  152.             [[aView layer] setBorderColor:[[UIColor blueColor] CGColor]];
  153.             [label setTextColor:[UIColor blueColor]];
  154.             break;
  155.         case 1:
  156.             [[aView layer] setBorderColor:[[UIColor redColor] CGColor]];
  157.             [label setTextColor:[UIColor redColor]];
  158.             break;
  159.         case 2:
  160.             [[aView layer] setBorderColor:[[UIColor greenColor] CGColor]];
  161.             [label setTextColor:[UIColor greenColor]];
  162.             break;
  163.         default:
  164.             break;
  165.     }
  166.     [[aView layer] setBorderWidth:2.0];
  167.     [[aView layer] setCornerRadius:[aView frame].size.width/2.0];
  168.    
  169.     [self addSubview:aView];
  170. }
  171.  
  172. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement