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