Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: sky/engine/core/painting/Size.dart

Issue 1214833004: Split Size into Size and Offset. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: add the new files also Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 part of dart.sky; 5 part of dart.sky;
6 6
7 /// Holds a 2D floating-point size. 7 /// Holds a 2D floating-point size.
8 /// Think of this as a vector from Point(0,0) to Point(size.width, size.height) 8 /// Think of this as a vector from Point(0,0) to Point(size.width, size.height)
9 class Size { 9 class Size extends OffsetBase {
10 const Size(this.width, this.height); 10 const Size(double width, double height) : super(width, height);
11 Size.copy(Size source) : width = source.width, height = source.height; 11 Size.copy(Size source) : super(source.width, source.height);
12 const Size.fromWidth(this.width) : height = double.INFINITY; 12 const Size.fromWidth(double width) : super(width, double.INFINITY);
13 const Size.fromHeight(this.height) : width = double.INFINITY; 13 const Size.fromHeight(double height) : super(double.INFINITY, height);
14 14
15 final double width; 15 double get width => _dx;
16 final double height; 16 double get height => _dy;
17 17
18 static const Size zero = const Size(0.0, 0.0); 18 static const Size zero = const Size(0.0, 0.0);
19 static const Size infinite = const Size(double.INFINITY, double.INFINITY); 19 static const Size infinite = const Size(double.INFINITY, double.INFINITY);
20 20
21 bool operator ==(other) => other is Size && width == other.width && height == other.height; 21 dynamic operator -(dynamic other) {
22 Size operator +(Size other) => new Size(width + other.width, height + other.he ight); 22 if (other is Size)
23 Size operator -(Size other) => new Size(width - other.width, height - other.he ight); 23 return new Offset(width - other.width, height - other.width);
24 if (other is Offset)
25 return new Size(width - other.dx, height - other.dy);
26 throw new ArgumentError(other);
27 }
28 Size operator +(Offset other) => new Size(width + other.dx, height + other.dy) ;
29 Size operator *(double operand) => new Size(width * operand, height * operand) ;
30 Size operator /(double operand) => new Size(width / operand, height / operand) ;
31 Size operator ~/(double operand) => new Size((width ~/ operand).toDouble(), (h eight ~/ operand).toDouble());
32 Size operator %(double operand) => new Size(width % operand, height % operand) ;
24 33
25 bool get isInfinite => width >= double.INFINITY || height >= double.INFINITY; 34 double get shortestSide {
35 double w = width.abs();
36 double h = height.abs();
37 return w < h ? w : h;
38 }
39 Size get square {
abarth-chromium 2015/06/26 22:04:42 inscribedSquare? Another square is the boundingSq
40 double scale = shortestSide;
41 return new Size(width.sign * scale, height.sign * scale);
42 }
26 43
27 // does the equivalent of "return new Point(0,0) + this" 44 // Convenience methods that do the equivalent of calling the similarly named
28 Point toPoint() => new Point(this.width, this.height); 45 // methods on a Rect constructed from the given origin and this size.
46 Point center(Point origin) => new Point(origin.x + width / 2.0, origin.y + hei ght / 2.0);
47 Point topLeft(Point origin) => origin;
48 Point topRight(Point origin) => new Point(origin.x + width, origin.y);
49 Point bottomLeft(Point origin) => new Point(origin.x, origin.y + height);
50 Point bottomRight(Point origin) => new Point(origin.x + width, origin.y + heig ht);
29 51
30 int get hashCode {
31 int result = 373;
32 result = 37 * result + width.hashCode;
33 result = 37 * result + height.hashCode;
34 return result;
35 }
36 String toString() => "Size($width, $height)"; 52 String toString() => "Size($width, $height)";
37 } 53 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698