| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.gesture; |
| 18 |
|
| 19 |
import android.graphics.Matrix; |
| 20 |
import android.graphics.Path; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class OrientedBoundingBox { |
| 26 |
public final float squareness; |
| 27 |
|
| 28 |
public final float width; |
| 29 |
public final float height; |
| 30 |
|
| 31 |
public final float orientation; |
| 32 |
|
| 33 |
public final float centerX; |
| 34 |
public final float centerY; |
| 35 |
|
| 36 |
OrientedBoundingBox(float angle, float cx, float cy, float w, float h) { |
| 37 |
orientation = angle; |
| 38 |
width = w; |
| 39 |
height = h; |
| 40 |
centerX = cx; |
| 41 |
centerY = cy; |
| 42 |
float ratio = w / h; |
| 43 |
if (ratio > 1) { |
| 44 |
squareness = 1 / ratio; |
| 45 |
} else { |
| 46 |
squareness = ratio; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public Path toPath() { |
| 56 |
Path path = new Path(); |
| 57 |
float[] point = new float[2]; |
| 58 |
point[0] = -width / 2; |
| 59 |
point[1] = height / 2; |
| 60 |
Matrix matrix = new Matrix(); |
| 61 |
matrix.setRotate(orientation); |
| 62 |
matrix.postTranslate(centerX, centerY); |
| 63 |
matrix.mapPoints(point); |
| 64 |
path.moveTo(point[0], point[1]); |
| 65 |
|
| 66 |
point[0] = -width / 2; |
| 67 |
point[1] = -height / 2; |
| 68 |
matrix.mapPoints(point); |
| 69 |
path.lineTo(point[0], point[1]); |
| 70 |
|
| 71 |
point[0] = width / 2; |
| 72 |
point[1] = -height / 2; |
| 73 |
matrix.mapPoints(point); |
| 74 |
path.lineTo(point[0], point[1]); |
| 75 |
|
| 76 |
point[0] = width / 2; |
| 77 |
point[1] = height / 2; |
| 78 |
matrix.mapPoints(point); |
| 79 |
path.lineTo(point[0], point[1]); |
| 80 |
|
| 81 |
path.close(); |
| 82 |
|
| 83 |
return path; |
| 84 |
} |
| 85 |
} |