2 * Copyright 2009 (c) Florian Frankenberger (darkblue.de)
4 * This file is part of LEA.
6 * LEA is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU Lesser General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option) any
11 * LEA is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with LEA. If not, see <http://www.gnu.org/licenses/>.
24 public class ScanArea {
26 private Point fromPoint;
27 private Point toPoint;
31 * Imagine you want to classify an image with 100px x 100px what would be the
32 * scanarea in this kind of image. That size gets automatically scalled to fit
38 public ScanArea(Point fromPoint, Point toPoint) {
39 this.fromPoint = fromPoint;
40 this.toPoint = toPoint;
42 this.size = (this.toPoint.x - this.fromPoint.x) * (this.toPoint.y - this.fromPoint.y);
45 public ScanArea(Point fromPoint, Point toPoint, float size) {
46 this.fromPoint = fromPoint;
47 this.toPoint = toPoint;
51 public ScanArea(int fromX, int fromY, int width, int height) {
52 this(new Point(fromX, fromY), new Point(fromX + width, fromY + height));
55 public int getFromX(float scaleFactor) {
56 return (int) (this.fromPoint.x * scaleFactor);
59 public int getFromY(float scaleFactor) {
60 return (int) (this.fromPoint.y * scaleFactor);
63 public int getToX(float scaleFactor) {
64 return (int) (this.toPoint.x * scaleFactor);
67 public int getToY(float scaleFactor) {
68 return (int) (this.toPoint.y * scaleFactor);
71 public int getSize(float scaleFactor) {
72 return (int) (this.size * Math.pow(scaleFactor, 2));
76 public boolean equals(Object o) {
77 ScanArea other = (ScanArea) o;
79 return pointsWithin(other.fromPoint.x, other.toPoint.x, this.fromPoint.x, this.toPoint.x)
80 && pointsWithin(other.fromPoint.y, other.toPoint.y, this.fromPoint.y, this.toPoint.y);
83 private static boolean pointsWithin(int pointA1, int pointA2, int pointB1, int pointB2) {
84 boolean within = false;
85 within = within || (pointB1 >= pointA1 && pointB1 <= pointA2);
86 within = within || (pointB2 >= pointA1 && pointB2 <= pointA2);
87 within = within || (pointA1 >= pointB1 && pointA1 <= pointB2);
88 within = within || (pointA2 >= pointB1 && pointA2 <= pointB2);
93 // private boolean checkPoints(ScanArea a, ScanArea b) {
94 // Point[] pointsToCheck = new Point[] {
95 // a.fromPoint, a.toPoint,
96 // new Point (a.fromPoint.x, a.toPoint.y),
97 // new Point (a.toPoint.x, a.fromPoint.y)
99 // for (Point point: pointsToCheck) {
100 // if (point.x >= b.fromPoint.x && point.x <= b.toPoint.x &&
101 // point.y >= b.fromPoint.y && point.y <= b.toPoint.y) return true;
107 public String toString() {
109 str += "fromPoint=(" + fromPoint.x + "," + fromPoint.y + ")";
110 str += "toPoint=(" + toPoint.x + "," + toPoint.y + ")";
111 str += "size=" + size;