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 18 package android.filterfw.geometry; 19 20 import android.filterfw.geometry.Point; 21 22 import java.lang.Float; 23 import java.util.Arrays; 24 import java.util.Collections; 25 import java.util.List; 26 27 /** 28 * @hide 29 */ 30 public class Quad { 31 32 public Point p0; 33 public Point p1; 34 public Point p2; 35 public Point p3; 36 37 public Quad() { 38 } 39 40 public Quad(Point p0, Point p1, Point p2, Point p3) { 41 this.p0 = p0; 42 this.p1 = p1; 43 this.p2 = p2; 44 this.p3 = p3; 45 } 46 47 public boolean IsInUnitRange() { 48 return p0.IsInUnitRange() && 49 p1.IsInUnitRange() && 50 p2.IsInUnitRange() && 51 p3.IsInUnitRange(); 52 } 53 54 public Quad translated(Point t) { 55 return new Quad(p0.plus(t), p1.plus(t), p2.plus(t), p3.plus(t)); 56 } 57 58 public Quad translated(float x, float y) { 59 return new Quad(p0.plus(x, y), p1.plus(x, y), p2.plus(x, y), p3.plus(x, y)); 60 } 61 62 public Quad scaled(float s) { 63 return new Quad(p0.times(s), p1.times(s), p2.times(s), p3.times(s)); 64 } 65 66 public Quad scaled(float x, float y) { 67 return new Quad(p0.mult(x, y), p1.mult(x, y), p2.mult(x, y), p3.mult(x, y)); 68 } 69 70 public Rectangle boundingBox() { 71 List<Float> xs = Arrays.asList(p0.x, p1.x, p2.x, p3.x); 72 List<Float> ys = Arrays.asList(p0.y, p1.y, p2.y, p3.y); 73 float x0 = Collections.min(xs); 74 float y0 = Collections.min(ys); 75 float x1 = Collections.max(xs); 76 float y1 = Collections.max(ys); 77 return new Rectangle(x0, y0, x1 - x0, y1 - y0); 78 } 79 80 public float getBoundingWidth() { 81 List<Float> xs = Arrays.asList(p0.x, p1.x, p2.x, p3.x); 82 return Collections.max(xs) - Collections.min(xs); 83 } 84 85 public float getBoundingHeight() { 86 List<Float> ys = Arrays.asList(p0.y, p1.y, p2.y, p3.y); 87 return Collections.max(ys) - Collections.min(ys); 88 } 89 90 @Override 91 public String toString() { 92 return "{" + p0 + ", " + p1 + ", " + p2 + ", " + p3 + "}"; 93 } 94 } 95