ios - Swift translating coordinates in UIView Coordinate System -


i building app controls robot joytstick interface; however, want plot lidar data; initially, did using spritekit realized performance slow:

how draw 720 data points using swift

now, using uikit

my lidar sensor returns 360 data points i.e. distances; these distances values of how far detected object

i trying plot circles on uiview coordinate system confusing me;

here doing currently: translating distances cartesian coordinates doesn't map nice uiview's coordinate system since origin (0,0) on top left

how can translate coordinate system origin @ "(bounds.width/2, bounds.height/2)"?

as can see in picture dots displaced

enter image description here

[![import foundation import uikit  @ibdesignable public class contours: uiview {      public var gridcolor: uicolor = uicolor.black // { didset { setneedsdisplay() }}     public var gridsize: cgsize = cgsize(width: 100, height: 100) // { didset { setneedsdisplay() }}     public var gridview = uiview(frame: cgrect(origin: .zero, size: .zero))      public var datasize: cgsize = cgsize(width: 10, height: 10) // { didset { setneedsdisplay() }}     public var datacolor: uicolor = uicolor.blue // { didset { setneedsdisplay() }}     public var dataview = uiview(frame: cgrect(origin: .zero, size: .zero))      public var fade: cgfloat = 0.5 { didset { setneedsdisplay() }}      public var distances: \[int\] = \[2601, 2600, 33, 2608, 2601, 2594, 2625, 2633, 2637, 2651, 2656, 2666, 2683, 2690, 2705, 2712, 2712, 2739, 2752, 53, 1103, 1060, 1019, 980, 944, 911, 33, 851, 826, 801, 777, 757, 737, 718, 701, 683, 667, 654, 53, 486, 470, 457, 448, 440, 432, 424, 416, 409, 403, 396, 389, 383, 378, 372, 367, 362, 357, 353, 348, 344, 340, 336, 333, 329, 326, 323, 319, 317, 314, 311, 309, 307, 305, 303, 301, 299, 298, 297, 295, 294, 293, 292, 291, 290, 290, 290, 290, 291, 293, 295, 303, 386, 383, 53, 350, 53, 53, 53, 53, 53, 3, 364, 362, 360, 356, 358, 355, 353, 351, 350, 349, 347, 345, 53, 53, 53, 308, 3, 309, 309, 312, 313, 315, 316, 319, 321, 53, 329, 332, 335, 332, 53, 53, 53, 53, 53, 53, 53, 3, 53, 53, 53, 53, 53, 53, 670, 33, 706, 728, 750, 772, 799, 825, 856, 887, 925, 755, 749, 744, 739, 734, 730, 727, 722, 719, 716, 714, 634, 709, 706, 704, 703, 701, 699, 700, 695, 697, 697, 338, 696, 697, 696, 698, 698, 700, 702, 703, 705, 707, 710, 712, 714, 718, 53, 53, 53, 53, 53, 53, 53, 53, 3, 53, 53, 53, 53, 53, 53, 53, 53, 3, 53, 53, 53, 53, 53, 53, 53, 3412, 3397, 3384, 53, 3538, 3603, 53, 2426, 2412, 2363, 2330, 2288, 2262, 2214, 2190, 2162, 2130, 53, 53, 53, 2807, 2631, 2408, 2638, 2607, 2601, 2562, 2534, 2515, 2496, 2478, 2462, 2445, 53, 53, 53, 1751, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 3, 53, 53, 53, 53, 53, 2643, 2643, 53, 2660, 53, 2528, 2523, 2221, 1955, 1684, 1677, 1684, 1694, 1705, 1202, 1204, 1211, 1217, 1225, 1233, 1239, 1249, 1259, 1270, 1280, 1289, 1280, 1011, 991, 977, 968, 966, 964, 968, 975, 984, 1003, 2, 53, 1297, 53, 53, 33, 1321, 1299, 1274, 1254, 1233, 1232, 1264, 1293, 3802, 53, 53, 53, 53, 53, 53, 53, 3, 53, 2878, 2870, 2848, 2821, 2803, 2788, 2769, 2753, 2740, 2708, 2711, 2701, 2682, 2656, 2664, 2644, 2644, 2631, 2626, 2616, 2610, 2604, 2605, 2605, 2600, 2598, 2603\]  //    public var circleviews: \[uiview\] = \[\]     public var distx: \[double\] = \[\]     public var disty: \[double\] = \[\]     public var scalex: \[double\] = \[\]     public var scaley: \[double\] = \[\]      override public init(frame: cgrect) {         super.init(frame: frame)     }      public required init?(coder adecoder: nscoder) {         super.init(coder: adecoder)     }      public override func draw(_ rect: cgrect) {          alpha = 1         layer.backgroundcolor = gridcolor.cgcolor          index in 0...356 {             let radians = double(index) * m_pi / 180.0             let x = double(distances\[index\]) * cos(double(radians))             let y = double(distances\[index\]) * sin(double(radians))             distx.append(x)             disty.append(y)         }          let xmin: double = double(distx.min()!)         let xmax: double = double(distx.max()!)         let ymin: double = double(disty.min()!)         let ymax: double = double(disty.max()!)          let scale = max(xmax - xmin, ymax - ymin)          var circles = \[uiview\]()         circles.reservecapacity(360)         _ in 0...356 {             circles.append(uiview(frame: cgrect(origin: .zero, size: .zero)))         }  //        cgsize(width: 30, height: 30) //        circles\[index\].alpha = 1 //        circles\[index\].frame = cgrect(origin: cgpoint(x: bounds.width/2, y: bounds.height/2), size: datasize) //        circles\[index\].center = cgpoint(x: x, y: y) //        circles\[index\].layer.backgroundcolor = datacolor.cgcolor //        circles\[index\].layer.cornerradius = circles\[index\].bounds.width / 2          print("width: \(bounds.width), height: \(bounds.height)")          index in 0...356 {             let radians = double(index) * m_pi / 180.0             let x = ((double(distances\[index\]) * cos(double(radians)) / scale) * double(bounds.width))             let y = (double(distances\[index\]) * sin(double(radians)) / scale) * double(bounds.height)              circles\[index\].alpha = 1             circles\[index\].frame = cgrect(origin: .zero, size: datasize)  //            circles\[index\].frame = cgrect(origin: cgpoint(x: bounds.width/2, y: bounds.height/2), size: datasize)             circles\[index\].center = cgpoint(x: x, y: y)             circles\[index\].layer.backgroundcolor = datacolor.cgcolor             circles\[index\].layer.cornerradius = circles\[index\].bounds.width / 2              print("x: \(x), y: \(y)") //            print("zero: \(layer.anchorpoint)")              if let superview = circles\[index\].superview {                 superview.bringsubview(tofront: circles\[index\])             } else {                 addsubview(circles\[index\])             }  //            scalex.append(x) //            scaley.append(y)         }      }      private func reset() {         uiview.animate(withduration: 0.25) { () -> void in             self.gridview.center = cgpoint(x: self.bounds.width / 2, y: self.bounds.height / 2)         }     }  }][1]][1] 

your code using addsubview within contours class. therefore circles relative bounds of contours view. assume contours view has origin @ top left of screen, whereas need set frame you're drawing red rectangle.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -