| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of html; | |
| 6 | |
| 7 /** | |
| 8 * A base class for representing two-dimensional rectangles. This will hopefully | |
| 9 * be moved merged with the dart:math Rect. | |
| 10 */ | |
| 11 // TODO(efortuna): Merge with Math rect after finalizing with Florian. | |
| 12 abstract class RectBase { | |
| 13 // Not used, but keeps the VM from complaining about Rect having a const | |
| 14 // constructor and this one not. | |
| 15 const RectBase(); | |
| 16 | |
| 17 num get left; | |
| 18 num get top; | |
| 19 num get width; | |
| 20 num get height; | |
| 21 | |
| 22 num get right => left + width; | |
| 23 num get bottom => top + height; | |
| 24 | |
| 25 // NOTE! All code below should be common with Rect. | |
| 26 | |
| 27 String toString() { | |
| 28 return '($left, $top, $width, $height)'; | |
| 29 } | |
| 30 | |
| 31 bool operator ==(other) { | |
| 32 if (other is !Rect) return false; | |
| 33 return left == other.left && top == other.top && width == other.width && | |
| 34 height == other.height; | |
| 35 } | |
| 36 | |
| 37 int get hashCode => JenkinsSmiHash.hash4(left.hashCode, top.hashCode, | |
| 38 width.hashCode, height.hashCode); | |
| 39 | |
| 40 /** | |
| 41 * Computes the intersection of this rectangle and the rectangle parameter. | |
| 42 * Returns null if there is no intersection. | |
| 43 */ | |
| 44 Rect intersection(Rect rect) { | |
| 45 var x0 = max(left, rect.left); | |
| 46 var x1 = min(left + width, rect.left + rect.width); | |
| 47 | |
| 48 if (x0 <= x1) { | |
| 49 var y0 = max(top, rect.top); | |
| 50 var y1 = min(top + height, rect.top + rect.height); | |
| 51 | |
| 52 if (y0 <= y1) { | |
| 53 return new Rect(x0, y0, x1 - x0, y1 - y0); | |
| 54 } | |
| 55 } | |
| 56 return null; | |
| 57 } | |
| 58 | |
| 59 | |
| 60 /** | |
| 61 * Returns whether a rectangle intersects this rectangle. | |
| 62 */ | |
| 63 bool intersects(Rect other) { | |
| 64 return (left <= other.left + other.width && other.left <= left + width && | |
| 65 top <= other.top + other.height && other.top <= top + height); | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Returns a new rectangle which completely contains this rectangle and the | |
| 70 * input rectangle. | |
| 71 */ | |
| 72 Rect union(Rect rect) { | |
| 73 var right = max(this.left + this.width, rect.left + rect.width); | |
| 74 var bottom = max(this.top + this.height, rect.top + rect.height); | |
| 75 | |
| 76 var left = min(this.left, rect.left); | |
| 77 var top = min(this.top, rect.top); | |
| 78 | |
| 79 return new Rect(left, top, right - left, bottom - top); | |
| 80 } | |
| 81 | |
| 82 /** | |
| 83 * Tests whether this rectangle entirely contains another rectangle. | |
| 84 */ | |
| 85 bool containsRect(Rect another) { | |
| 86 return left <= another.left && | |
| 87 left + width >= another.left + another.width && | |
| 88 top <= another.top && | |
| 89 top + height >= another.top + another.height; | |
| 90 } | |
| 91 | |
| 92 /** | |
| 93 * Tests whether this rectangle entirely contains a point. | |
| 94 */ | |
| 95 bool containsPoint(Point another) { | |
| 96 return another.x >= left && | |
| 97 another.x <= left + width && | |
| 98 another.y >= top && | |
| 99 another.y <= top + height; | |
| 100 } | |
| 101 | |
| 102 Rect ceil() => new Rect(left.ceil(), top.ceil(), width.ceil(), height.ceil()); | |
| 103 Rect floor() => new Rect(left.floor(), top.floor(), width.floor(), | |
| 104 height.floor()); | |
| 105 Rect round() => new Rect(left.round(), top.round(), width.round(), | |
| 106 height.round()); | |
| 107 | |
| 108 /** | |
| 109 * Truncates coordinates to integers and returns the result as a new | |
| 110 * rectangle. | |
| 111 */ | |
| 112 Rect toInt() => new Rect(left.toInt(), top.toInt(), width.toInt(), | |
| 113 height.toInt()); | |
| 114 | |
| 115 Point get topLeft => new Point(this.left, this.top); | |
| 116 Point get bottomRight => new Point(this.left + this.width, | |
| 117 this.top + this.height); | |
| 118 } | |
| 119 | |
| 120 | |
| 121 | |
| 122 /** | |
| 123 * A class for representing two-dimensional rectangles. | |
| 124 * | |
| 125 * This class is distinctive from RectBase in that it enforces that its | |
| 126 * properties are immutable. | |
| 127 */ | |
| 128 class Rect extends RectBase { | |
| 129 final num left; | |
| 130 final num top; | |
| 131 final num width; | |
| 132 final num height; | |
| 133 | |
| 134 const Rect(this.left, this.top, this.width, this.height): super(); | |
| 135 | |
| 136 factory Rect.fromPoints(Point a, Point b) { | |
| 137 var left; | |
| 138 var width; | |
| 139 if (a.x < b.x) { | |
| 140 left = a.x; | |
| 141 width = b.x - left; | |
| 142 } else { | |
| 143 left = b.x; | |
| 144 width = a.x - left; | |
| 145 } | |
| 146 var top; | |
| 147 var height; | |
| 148 if (a.y < b.y) { | |
| 149 top = a.y; | |
| 150 height = b.y - top; | |
| 151 } else { | |
| 152 top = b.y; | |
| 153 height = a.y - top; | |
| 154 } | |
| 155 | |
| 156 return new Rect(left, top, width, height); | |
| 157 } | |
| 158 } | |
| OLD | NEW |