You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZXResultPoint.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXMathUtils.h"
  17. #import "ZXResultPoint.h"
  18. @implementation ZXResultPoint
  19. - (id)initWithX:(float)x y:(float)y {
  20. if (self = [super init]) {
  21. _x = x;
  22. _y = y;
  23. }
  24. return self;
  25. }
  26. + (id)resultPointWithX:(float)x y:(float)y {
  27. return [[self alloc] initWithX:x y:y];
  28. }
  29. - (id)copyWithZone:(NSZone *)zone {
  30. return [[ZXResultPoint allocWithZone:zone] initWithX:self.x y:self.y];
  31. }
  32. - (BOOL)isEqual:(id)other {
  33. if ([other isKindOfClass:[ZXResultPoint class]]) {
  34. ZXResultPoint *otherPoint = (ZXResultPoint *)other;
  35. return self.x == otherPoint.x && self.y == otherPoint.y;
  36. }
  37. return NO;
  38. }
  39. - (NSUInteger)hash {
  40. return 31 * *((int *)(&_x)) + *((int *)(&_y));
  41. }
  42. - (NSString *)description {
  43. return [NSString stringWithFormat:@"(%f,%f)", self.x, self.y];
  44. }
  45. + (void)orderBestPatterns:(NSMutableArray *)patterns {
  46. float zeroOneDistance = [self distance:patterns[0] pattern2:patterns[1]];
  47. float oneTwoDistance = [self distance:patterns[1] pattern2:patterns[2]];
  48. float zeroTwoDistance = [self distance:patterns[0] pattern2:patterns[2]];
  49. ZXResultPoint *pointA;
  50. ZXResultPoint *pointB;
  51. ZXResultPoint *pointC;
  52. if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) {
  53. pointB = patterns[0];
  54. pointA = patterns[1];
  55. pointC = patterns[2];
  56. } else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) {
  57. pointB = patterns[1];
  58. pointA = patterns[0];
  59. pointC = patterns[2];
  60. } else {
  61. pointB = patterns[2];
  62. pointA = patterns[0];
  63. pointC = patterns[1];
  64. }
  65. if ([self crossProductZ:pointA pointB:pointB pointC:pointC] < 0.0f) {
  66. ZXResultPoint *temp = pointA;
  67. pointA = pointC;
  68. pointC = temp;
  69. }
  70. patterns[0] = pointA;
  71. patterns[1] = pointB;
  72. patterns[2] = pointC;
  73. }
  74. + (float)distance:(ZXResultPoint *)pattern1 pattern2:(ZXResultPoint *)pattern2 {
  75. return [ZXMathUtils distance:pattern1.x aY:pattern1.y bX:pattern2.x bY:pattern2.y];
  76. }
  77. /**
  78. * Returns the z component of the cross product between vectors BC and BA.
  79. */
  80. + (float)crossProductZ:(ZXResultPoint *)pointA pointB:(ZXResultPoint *)pointB pointC:(ZXResultPoint *)pointC {
  81. float bX = pointB.x;
  82. float bY = pointB.y;
  83. return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX));
  84. }
  85. @end