1 | /**
|
---|
2 | * Constraint.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * This class represents a constraint between two Dots on the screen. A vertical
|
---|
23 | * constraint means the Dot's Y values will be equal, likewise a horizontal
|
---|
24 | * constraint means the Dot's X values will be equal.
|
---|
25 | *
|
---|
26 | * @author jdm18
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class Constraint {
|
---|
30 | public static final int VERTICAL = 2;
|
---|
31 |
|
---|
32 | public static final int HORIZONTAL = 3;
|
---|
33 |
|
---|
34 | public static final int DIAGONAL_POS = 4;
|
---|
35 |
|
---|
36 | public static final int DIAGONAL_NEG = 5;
|
---|
37 |
|
---|
38 | // the list of points that are constrained
|
---|
39 | private Item _start = null;
|
---|
40 |
|
---|
41 | private Item _end = null;
|
---|
42 |
|
---|
43 | // id and type info
|
---|
44 | private int _id;
|
---|
45 |
|
---|
46 | private int _type;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructs a constraint with the two given end points, id, and type. Type
|
---|
50 | * should be one of the constants defined in this class.
|
---|
51 | *
|
---|
52 | * @param a
|
---|
53 | * One of the points in this constraint.
|
---|
54 | * @param b
|
---|
55 | * The other point in this constraint.
|
---|
56 | * @param id
|
---|
57 | * The ID of this constraint item.
|
---|
58 | * @param type
|
---|
59 | * The type of this constraint (horizontal, vertical).
|
---|
60 | */
|
---|
61 | public Constraint(Item a, Item b, int id, int type) {
|
---|
62 | _start = a;
|
---|
63 | _end = b;
|
---|
64 |
|
---|
65 | a.addConstraint(this);
|
---|
66 | b.addConstraint(this);
|
---|
67 |
|
---|
68 | _id = id;
|
---|
69 | _type = type;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * @return The Item ID of this Constraint.
|
---|
74 | */
|
---|
75 | public int getID() {
|
---|
76 | return _id;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | *
|
---|
81 | * @return The type of this Constraint. The Type corresponds to the
|
---|
82 | * constants defined in this class.
|
---|
83 | */
|
---|
84 | public int getType() {
|
---|
85 | return _type;
|
---|
86 | }
|
---|
87 |
|
---|
88 | public Item getStart() {
|
---|
89 | return _start;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public Item getEnd() {
|
---|
93 | return _end;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public boolean contains(Item i) {
|
---|
97 | if (_start == i || _end == i)
|
---|
98 | return true;
|
---|
99 |
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * If there are only two points in this constraint, this method returns the
|
---|
105 | * Point not passed to it. If there are more than two points then the first
|
---|
106 | * encountered is returned.
|
---|
107 | *
|
---|
108 | * @return A Point in the constraint that is not the one given (or null if
|
---|
109 | * none is found).
|
---|
110 | */
|
---|
111 | public Item getOppositeEnd(Item from) {
|
---|
112 | if (from == _start)
|
---|
113 | return _end;
|
---|
114 |
|
---|
115 | if (from == _end)
|
---|
116 | return _start;
|
---|
117 |
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public void replaceEnd(Item toReplace, Item with) {
|
---|
122 | assert(_start != _end);
|
---|
123 | if (_start == toReplace) {
|
---|
124 | _start = with;
|
---|
125 | } else if (_end == toReplace) {
|
---|
126 | _end = with;
|
---|
127 | }
|
---|
128 | toReplace.removeConstraint(this);
|
---|
129 | with.addConstraint(this);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public String getLineEnds() {
|
---|
133 | String ends = "";
|
---|
134 |
|
---|
135 | if (_start != null)
|
---|
136 | ends += _start.getID();
|
---|
137 |
|
---|
138 | if (_end != null)
|
---|
139 | ends += " " + _end.getID();
|
---|
140 |
|
---|
141 | return ends;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public boolean isDiagonal() {
|
---|
145 | return _type == Constraint.DIAGONAL_NEG || _type == Constraint.DIAGONAL_POS;
|
---|
146 | }
|
---|
147 |
|
---|
148 | public Float getGradient() {
|
---|
149 | switch(_type){
|
---|
150 | case Constraint.DIAGONAL_NEG:
|
---|
151 | return -1.0F;
|
---|
152 | case Constraint.DIAGONAL_POS:
|
---|
153 | return 1.0F;
|
---|
154 | case Constraint.HORIZONTAL:
|
---|
155 | return 0.0F;
|
---|
156 | case Constraint.VERTICAL:
|
---|
157 | return Float.POSITIVE_INFINITY;
|
---|
158 | }
|
---|
159 | assert(false);
|
---|
160 | return null;
|
---|
161 | }
|
---|
162 |
|
---|
163 | }
|
---|