1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.filterfw.geometry;
19
20import android.filterfw.geometry.Point;
21import android.filterfw.geometry.Quad;
22
23/**
24 * @hide
25 */
26public class Rectangle extends Quad {
27
28    public Rectangle() {
29    }
30
31    public Rectangle(float x, float y, float width, float height) {
32        super(new Point(x, y),
33              new Point(x + width, y),
34              new Point(x, y + height),
35              new Point(x + width, y + height));
36    }
37
38    public Rectangle(Point origin, Point size) {
39        super(origin,
40              origin.plus(size.x, 0.0f),
41              origin.plus(0.0f, size.y),
42              origin.plus(size.x, size.y));
43    }
44
45    public static Rectangle fromRotatedRect(Point center, Point size, float rotation) {
46        Point p0 = new Point(center.x - size.x/2f, center.y - size.y/2f);
47        Point p1 = new Point(center.x + size.x/2f, center.y - size.y/2f);
48        Point p2 = new Point(center.x - size.x/2f, center.y + size.y/2f);
49        Point p3 = new Point(center.x + size.x/2f, center.y + size.y/2f);
50        return new Rectangle(p0.rotatedAround(center, rotation),
51                             p1.rotatedAround(center, rotation),
52                             p2.rotatedAround(center, rotation),
53                             p3.rotatedAround(center, rotation));
54    }
55
56    private Rectangle(Point p0, Point p1, Point p2, Point p3) {
57        super(p0, p1, p2, p3);
58    }
59
60    public static Rectangle fromCenterVerticalAxis(Point center, Point vAxis, Point size) {
61        Point dy = vAxis.scaledTo(size.y / 2.0f);
62        Point dx = vAxis.rotated90(1).scaledTo(size.x / 2.0f);
63        return new Rectangle(center.minus(dx).minus(dy),
64                             center.plus(dx).minus(dy),
65                             center.minus(dx).plus(dy),
66                             center.plus(dx).plus(dy));
67    }
68
69    public float getWidth() {
70        return p1.minus(p0).length();
71    }
72
73    public float getHeight() {
74        return p2.minus(p0).length();
75    }
76
77    public Point center() {
78        return p0.plus(p1).plus(p2).plus(p3).times(0.25f);
79    }
80
81    @Override
82    public Rectangle scaled(float s) {
83        return new Rectangle(p0.times(s), p1.times(s), p2.times(s), p3.times(s));
84    }
85
86    @Override
87    public Rectangle scaled(float x, float y) {
88        return new Rectangle(p0.mult(x, y), p1.mult(x, y), p2.mult(x, y), p3.mult(x, y));
89    }
90
91    //public Rectangle rotated(float radians) {
92      // TODO: Implement this.
93    //}
94
95}
96