| 1 | // Copyright 2014 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | /* |
| 6 | Package geom defines a two-dimensional coordinate system. |
| 7 | |
| 8 | The coordinate system is based on an left-handed Cartesian plane. |
| 9 | That is, X increases to the right and Y increases down. For (x,y), |
| 10 | |
| 11 | (0,0) → (1,0) |
| 12 | ↓ ↘ |
| 13 | (0,1) (1,1) |
| 14 | |
| 15 | The display window places the origin (0, 0) in the upper-left corner of |
| 16 | the screen. Positions on the plane are measured in typographic points, |
| 17 | 1/72 of an inch, which is represented by the Pt type. |
| 18 | |
| 19 | Any interface that draws to the screen using types from the geom package |
| 20 | scales the number of pixels to maintain a Pt as 1/72 of an inch. |
| 21 | */ |
| 22 | package geom |
| 23 | |
| 24 | /* |
| 25 | Notes on the various underlying coordinate systems. |
| 26 | |
| 27 | Both Android and iOS (UIKit) use upper-left-origin coordinate systems |
| 28 | with for events, however they have different units. |
| 29 | |
| 30 | UIKit measures distance in points. A point is a single-pixel on a |
| 31 | pre-Retina display. UIKit maintains a scale factor that to turn points |
| 32 | into pixels. On current retina devices, the scale factor is 2.0. |
| 33 | |
| 34 | A UIKit point does not correspond to a fixed physical distance, as the |
| 35 | iPhone has a 163 DPI/PPI (326 PPI retina) display, and the iPad has a |
| 36 | 132 PPI (264 retina) display. Points are 32-bit floats. |
| 37 | |
| 38 | Even though point is the official UIKit term, they are commonly called |
| 39 | pixels. Indeed, the units were equivalent until the retina display was |
| 40 | introduced. |
| 41 | |
| 42 | N.b. as a UIKit point is unrelated to a typographic point, it is not |
| 43 | related to this packages's Pt and Point types. |
| 44 | |
| 45 | More details about iOS drawing: |
| 46 | |
| 47 | https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/GraphicsDrawingOverview/GraphicsDrawingOverview.html |
| 48 | |
| 49 | Android uses pixels. Sub-pixel precision is possible, so pixels are |
| 50 | represented as 32-bit floats. The ACONFIGURATION_DENSITY enum provides |
| 51 | the screen DPI/PPI, which varies frequently between devices. |
| 52 | |
| 53 | It would be tempting to adopt the pixel, given the clear pixel/DPI split |
| 54 | in the core android events API. However, the plot thickens: |
| 55 | |
| 56 | http://developer.android.com/training/multiscreen/screendensities.html |
| 57 | |
| 58 | Android promotes the notion of a density-independent pixel in many of |
| 59 | their interfaces, often prefixed by "dp". 1dp is a real physical length, |
| 60 | as "independent" means it is assumed to be 1/160th of an inch and is |
| 61 | adjusted for the current screen. |
| 62 | |
| 63 | In addition, android has a scale-indepdendent pixel used for expressing |
| 64 | a user's preferred text size. The user text size preference is a useful |
| 65 | notion not yet expressed in the geom package. |
| 66 | |
| 67 | For the sake of clarity when working across platforms, the geom package |
| 68 | tries to put distance between it and the word pixel. |
| 69 | */ |
| 70 | |
| 71 | import "fmt" |
| 72 | |
| 73 | // Pt is a length. |
| 74 | // |
| 75 | // The unit Pt is a typographical point, 1/72 of an inch (0.3527 mm). |
| 76 | // |
| 77 | // It can be converted to a length in current device pixels by |
| 78 | // multiplying with PixelsPerPt after app initialization is complete. |
| 79 | type Pt float32 |
| 80 | |
| 81 | // Px converts the length to current device pixels. |
| 82 | func (p Pt) Px(pixelsPerPt float32) float32 { return float32(p) * pixelsPerPt } |
| 83 | |
| 84 | // String returns a string representation of p like "3.2pt". |
| 85 | func (p Pt) String() string { return fmt.Sprintf("%.2fpt", p) } |
| 86 | |
| 87 | // Point is a point in a two-dimensional plane. |
| 88 | type Point struct { |
| 89 | X, Y Pt |
| 90 | } |
| 91 | |
| 92 | // String returns a string representation of p like "(1.2,3.4)". |
| 93 | func (p Point) String() string { return fmt.Sprintf("(%.2f,%.2f)", p.X, p.Y) } |
| 94 | |
| 95 | // A Rectangle is region of points. |
| 96 | // The top-left point is Min, and the bottom-right point is Max. |
| 97 | type Rectangle struct { |
| 98 | Min, Max Point |
| 99 | } |
| 100 | |
| 101 | // String returns a string representation of r like "(3,4)-(6,5)". |
| 102 | func (r Rectangle) String() string { return r.Min.String() + "-" + r.Max.String() } |