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/>.
25 public class ScanArea {
27 private Point fromPoint;
29 private Point toPoint;
34 * Imagine you want to classify an image with 100px x 100px what would be the scanarea in this
35 * kind of image. That size gets automatically scalled to fit bigger images
40 public ScanArea(Point fromPoint, Point toPoint) {
41 this.fromPoint = fromPoint;
42 this.toPoint = toPoint;
44 this.size = (this.toPoint.x - this.fromPoint.x) * (this.toPoint.y - this.fromPoint.y);
47 public ScanArea(Point fromPoint, Point toPoint, float size) {
48 this.fromPoint = fromPoint;
49 this.toPoint = toPoint;
53 public ScanArea(int fromX, int fromY, int width, int height) {
54 this(new Point(fromX, fromY), new Point(fromX + width, fromY + height));
57 public int getFromX(float scaleFactor) {
58 return (int) (this.fromPoint.x * scaleFactor);
61 public int getFromY(float scaleFactor) {
62 return (int) (this.fromPoint.y * scaleFactor);
65 public int getToX(float scaleFactor) {
66 return (int) (this.toPoint.x * scaleFactor);
69 public int getToY(float scaleFactor) {
70 return (int) (this.toPoint.y * scaleFactor);
73 public int getSize(float scaleFactor) {
74 return (int) (this.size * Math.pow(scaleFactor, 2));
78 public boolean equals(Object o) {
79 ScanArea other = (ScanArea) o;
81 return pointsWithin(other.fromPoint.x, other.toPoint.x, this.fromPoint.x, this.toPoint.x)
82 && pointsWithin(other.fromPoint.y, other.toPoint.y, this.fromPoint.y, this.toPoint.y);
85 private static boolean pointsWithin(int pointA1, int pointA2, int pointB1, int pointB2) {
86 boolean within = false;
87 within = within || (pointB1 >= pointA1 && pointB1 <= pointA2);
88 within = within || (pointB2 >= pointA1 && pointB2 <= pointA2);
89 within = within || (pointA1 >= pointB1 && pointA1 <= pointB2);
90 within = within || (pointA2 >= pointB1 && pointA2 <= pointB2);
95 // private boolean checkPoints(ScanArea a, ScanArea b) {
96 // Point[] pointsToCheck = new Point[] {
97 // a.fromPoint, a.toPoint,
98 // new Point (a.fromPoint.x, a.toPoint.y),
99 // new Point (a.toPoint.x, a.fromPoint.y)
101 // for (Point point: pointsToCheck) {
102 // if (point.x >= b.fromPoint.x && point.x <= b.toPoint.x &&
103 // point.y >= b.fromPoint.y && point.y <= b.toPoint.y) return true;
109 public String toString() {
111 str += "fromPoint=(" + fromPoint.x + "," + fromPoint.y + ")";
112 str += "toPoint=(" + toPoint.x + "," + toPoint.y + ")";
113 str += "size=" + size;