Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Call test:
- NSArray *points = @[[NSValue valueWithCGPoint:CGPointMake(40, 612)],
- [NSValue valueWithCGPoint:CGPointMake(379, 788)],
- [NSValue valueWithCGPoint:CGPointMake(0, 352)]];
- // NSArray *distances = @[@(13.535637), @(46.931522), @(51.585461)];
- NSArray *distances = @[@(349.9057), @(352.84134), @(353.37515)];
- TriangularisationMapView *v = [[TriangularisationMapView alloc] initWithFrame:CGRectMake(0, 0, 600, 600) points:points andDistance:distances];
- [[self view] addSubview:v];
- ------------------------------------------------------------------------------------------------------------------------
- @interface TriangularisationMapView : UIView
- -(id)initWithFrame:(CGRect)frame points:(NSArray *)points andDistance:(NSArray *)distance;
- @end
- ------------------------------------------------------------------------------------------------------------------------
- #import "TriangularisationMapView.h"
- @interface TriangularisationMapView ()
- @property (nonatomic, assign) CGPoint pointA;
- @property (nonatomic, assign) CGPoint pointB;
- @property (nonatomic, assign) CGPoint pointC;
- @property (nonatomic, assign) float distanceToA;
- @property (nonatomic, assign) float distanceToB;
- @property (nonatomic, assign) float distanceToC;
- @property (nonatomic, assign) CGPoint finalPoint;
- @end
- @implementation TriangularisationMapView
- -(id)initWithFrame:(CGRect)frame points:(NSArray *)points andDistance:(NSArray *)distance
- {
- self = [super initWithFrame:frame];
- if (self)
- {
- [self setBackgroundColor:[UIColor whiteColor]];
- _pointA = [[points objectAtIndex:0] CGPointValue];
- _pointB = [[points objectAtIndex:1] CGPointValue];
- _pointC = [[points objectAtIndex:2] CGPointValue];
- _distanceToA = [[distance objectAtIndex:0] floatValue];
- _distanceToB = [[distance objectAtIndex:1] floatValue];
- _distanceToC = [[distance objectAtIndex:2] floatValue];
- }
- return self;
- }
- - (void)drawRect:(CGRect)rect
- {
- [self addCircleNb:0 withCenter:_pointA andRadius:_distanceToA];
- [self addCircleNb:1 withCenter:_pointB andRadius:_distanceToB];
- [self addCircleNb:2 withCenter:_pointC andRadius:_distanceToC];
- [self calculatePosition];
- }
- -(void)calculatePosition
- {
- CGPoint P2MinusP1 = CGPointMake(_pointB.x - _pointA.x, _pointB.y - _pointA.y);
- CGPoint P3MinusP1 = CGPointMake(_pointC.x - _pointA.x, _pointC.y - _pointA.y);
- CGFloat magP2MinusP1 = sqrt(pow((P2MinusP1.x), 2) + pow((P2MinusP1.y), 2));
- CGPoint eX = CGPointMake(P2MinusP1.x / magP2MinusP1, P2MinusP1.y / magP2MinusP1);
- CGFloat i = eX.x * P3MinusP1.x + eX.y * P3MinusP1.y;
- CGPoint eXTimesI = CGPointMake(eX.x * i, eX.y * i);
- CGPoint P3MinusP1MinusITimesEX = CGPointMake(P3MinusP1.x - eXTimesI.x, P3MinusP1.y - eXTimesI.y);
- CGFloat magP3MinusP1MinusITimesEX = sqrt(pow(P3MinusP1MinusITimesEX.x, 2) + pow(P3MinusP1MinusITimesEX.y, 2));
- CGPoint eY = CGPointMake(P3MinusP1MinusITimesEX.x / magP3MinusP1MinusITimesEX, P3MinusP1MinusITimesEX.y / magP3MinusP1MinusITimesEX);
- CGFloat j = eY.x * P3MinusP1.x + eY.y * P3MinusP1.y;
- CGFloat x = (pow(_distanceToA, 2) - pow(_distanceToB, 2) + pow(magP2MinusP1, 2)) / (2 * magP2MinusP1);
- CGFloat y = (pow(_distanceToA, 2) - pow(_distanceToC, 2) + pow(i, 2) + pow(j, 2)) / (2 * j) - (i * x) / j;
- _finalPoint = CGPointMake(_pointA.x + x * eX.x + y * eY.x, _pointA.y + x * eX.y + y * eY.y);
- NSLog(@"%f %f", _finalPoint.x, _finalPoint.y);
- UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(_finalPoint.x-3, _finalPoint.y-3, 6, 6)];
- [centerView setBackgroundColor:[UIColor orangeColor]];
- UILabel *label = [[UILabel alloc] init];
- [label setBackgroundColor:[UIColor clearColor]];
- [label setText:[NSString stringWithFormat:@"Final: {%d,%d}",(int)_finalPoint.x, (int)_finalPoint.y]];
- [label sizeToFit];
- [label setFrame:CGRectMake(_finalPoint.x - ([label frame].size.width/2.0),
- _finalPoint.y - ([label frame].size.height/2.0-20),
- [label frame].size.width,
- [label frame].size.height)];
- [self addSubview:label];
- [self addSubview:centerView];
- [self checkFinalPoint];
- }
- -(void)checkFinalPoint
- {
- float distanceToA2 = [self distanceBetweenPoints:_pointA with:_finalPoint];
- float distanceToB2 = [self distanceBetweenPoints:_pointB with:_finalPoint];
- float distanceToC2 = [self distanceBetweenPoints:_pointC with:_finalPoint];
- float affordableError = 4;
- if (fabs(distanceToA2-_distanceToA) >= affordableError || (fabs(distanceToB2-_distanceToB) >= affordableError) || (fabs(distanceToC2-_distanceToC) >= affordableError))
- {
- NSLog(@"Position Too Much Wrong");
- }
- }
- -(float)distanceBetweenPoints:(CGPoint)pointA with:(CGPoint)pointB
- {
- float square = (pointA.x-pointB.x)*(pointA.x-pointB.x)+ (pointA.y-pointB.y)*(pointA.y-pointB.y);
- return sqrtf(square);
- // NSLog(@"Result: %@", @(sqrtf(square)));
- }
- -(void)addCircleNb:(NSInteger)nb withCenter:(CGPoint)center andRadius:(CGFloat)radius
- {
- //Global Circle
- UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(center.x-radius, center.y-radius, radius*2, radius*2)];
- [aView setBackgroundColor:[UIColor clearColor]];
- //Point Center
- UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(radius-1, radius-1, 2, 2)];
- [centerView setBackgroundColor:[UIColor blackColor]];
- [[centerView layer] setCornerRadius:1];
- //Center Coordinate Label
- UILabel *label = [[UILabel alloc] init];
- [label setBackgroundColor:[UIColor clearColor]];
- [label setFont:[UIFont systemFontOfSize:10]];
- [label setText:[NSString stringWithFormat:@"{%d,%d}",(int)center.x, (int)center.y]];
- [label sizeToFit];
- [label setFrame:CGRectMake(([aView frame].size.width-[label frame].size.width)/2.0,
- ([aView frame].size.height-[label frame].size.height)/2.0,
- [label frame].size.width,
- [label frame].size.height)];
- [aView addSubview:label];
- [aView addSubview:centerView];
- switch (nb)
- {
- case 0:
- [[aView layer] setBorderColor:[[UIColor blueColor] CGColor]];
- [label setTextColor:[UIColor blueColor]];
- break;
- case 1:
- [[aView layer] setBorderColor:[[UIColor redColor] CGColor]];
- [label setTextColor:[UIColor redColor]];
- break;
- case 2:
- [[aView layer] setBorderColor:[[UIColor greenColor] CGColor]];
- [label setTextColor:[UIColor greenColor]];
- break;
- default:
- break;
- }
- [[aView layer] setBorderWidth:2.0];
- [[aView layer] setCornerRadius:[aView frame].size.width/2.0];
- [self addSubview:aView];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement