source: trunk/src/org/expeditee/items/Constraint.java@ 1102

Last change on this file since 1102 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File size: 3.7 KB
Line 
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
19package 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 */
29public class Constraint {
30
31 public static final int VERTICAL = 2;
32
33 public static final int HORIZONTAL = 3;
34
35 public static final int DIAGONAL_POS = 4;
36
37 public static final int DIAGONAL_NEG = 5;
38
39 // the list of points that are constrained
40 private Item _start = null;
41
42 private Item _end = null;
43
44 // id and type info
45 private int _id;
46
47 private int _type;
48
49 /**
50 * Constructs a constraint with the two given end points, id, and type. Type
51 * should be one of the constants defined in this class.
52 *
53 * @param a
54 * One of the points in this constraint.
55 * @param b
56 * The other point in this constraint.
57 * @param id
58 * The ID of this constraint item.
59 * @param type
60 * The type of this constraint (horizontal, vertical).
61 */
62 public Constraint(Item a, Item b, int id, int type) {
63 _start = a;
64 _end = b;
65
66 a.addConstraint(this);
67 b.addConstraint(this);
68
69 _id = id;
70 _type = type;
71 }
72
73 /**
74 * @return The Item ID of this Constraint.
75 */
76 public int getID() {
77 return _id;
78 }
79
80 /**
81 *
82 * @return The type of this Constraint. The Type corresponds to the
83 * constants defined in this class.
84 */
85 public int getType() {
86 return _type;
87 }
88
89 public Item getStart() {
90 return _start;
91 }
92
93 public Item getEnd() {
94 return _end;
95 }
96
97 public boolean contains(Item i) {
98 if (_start == i || _end == i)
99 return true;
100
101 return false;
102 }
103
104 /**
105 * If there are only two points in this constraint, this method returns the
106 * Point not passed to it. If there are more than two points then the first
107 * encountered is returned.
108 *
109 * @return A Point in the constraint that is not the one given (or null if
110 * none is found).
111 */
112 public Item getOppositeEnd(Item from) {
113 if (from == _start)
114 return _end;
115
116 if (from == _end)
117 return _start;
118
119 return null;
120 }
121
122 public void replaceEnd(Item toReplace, Item with) {
123 assert(_start != _end);
124 if (_start == toReplace) {
125 _start = with;
126 } else if (_end == toReplace) {
127 _end = with;
128 }
129 toReplace.removeConstraint(this);
130 with.addConstraint(this);
131 }
132
133 public String getLineEnds() {
134 String ends = "";
135
136 if (_start != null)
137 ends += _start.getID();
138
139 if (_end != null)
140 ends += " " + _end.getID();
141
142 return ends;
143 }
144
145 public boolean isDiagonal() {
146 return _type == Constraint.DIAGONAL_NEG || _type == Constraint.DIAGONAL_POS;
147 }
148
149 public Float getGradient() {
150 switch(_type){
151 case Constraint.DIAGONAL_NEG:
152 return -1.0F;
153 case Constraint.DIAGONAL_POS:
154 return 1.0F;
155 case Constraint.HORIZONTAL:
156 return 0.0F;
157 case Constraint.VERTICAL:
158 return Float.POSITIVE_INFINITY;
159 }
160 assert(false);
161 return null;
162 }
163
164}
Note: See TracBrowser for help on using the repository browser.