Point2i.mm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Point2i.m
  3. //
  4. // Created by Giles Payne on 2019/10/09.
  5. //
  6. #import "Point2i.h"
  7. #import "Rect2i.h"
  8. #import "CVObjcUtil.h"
  9. @implementation Point2i {
  10. cv::Point2i native;
  11. }
  12. - (int)x {
  13. return native.x;
  14. }
  15. - (void)setX:(int)val {
  16. native.x = val;
  17. }
  18. - (int)y {
  19. return native.y;
  20. }
  21. - (void)setY:(int)val {
  22. native.y = val;
  23. }
  24. - (cv::Point2i&)nativeRef {
  25. return native;
  26. }
  27. - (instancetype)init {
  28. return [self initWithX:0 y:0];
  29. }
  30. - (instancetype)initWithX:(int)x y:(int)y {
  31. self = [super init];
  32. if (self) {
  33. self.x = x;
  34. self.y = y;
  35. }
  36. return self;
  37. }
  38. - (instancetype)initWithVals:(NSArray<NSNumber*>*)vals {
  39. self = [super init];
  40. if (self) {
  41. [self set:vals];
  42. }
  43. return self;
  44. }
  45. + (instancetype)fromNative:(cv::Point2i&)point {
  46. return [[Point2i alloc] initWithX:point.x y:point.y];
  47. }
  48. - (void)update:(cv::Point2i&)point {
  49. self.x = point.x;
  50. self.y = point.y;
  51. }
  52. - (Point2i*) clone {
  53. return [[Point2i alloc] initWithX:self.x y:self.y];
  54. }
  55. - (double)dot:(Point2i*)point {
  56. return self.x * point.x + self.y * point.y;
  57. }
  58. - (void)set:(NSArray<NSNumber*>*)vals {
  59. self.x = (vals != nil && vals.count > 0) ? vals[0].doubleValue : 0;
  60. self.y = (vals != nil && vals.count > 1) ? vals[1].doubleValue : 0;
  61. }
  62. - (BOOL)isEqual:(id)other {
  63. if (other == self) {
  64. return YES;
  65. } else if (![other isKindOfClass:[Point2i class]]) {
  66. return NO;
  67. } else {
  68. Point2i* point = (Point2i*)other;
  69. return self.x == point.x && self.y == point.y;
  70. }
  71. }
  72. - (BOOL)inside:(Rect2i*)rect {
  73. return [rect contains:self];
  74. }
  75. - (NSUInteger)hash {
  76. int prime = 31;
  77. uint32_t result = 1;
  78. result = prime * result + self.x;
  79. result = prime * result + self.y;
  80. return result;
  81. }
  82. - (NSString *)description {
  83. return [NSString stringWithFormat:@"Point2i {%d,%d}", self.x, self.y];
  84. }
  85. @end